33

In this SO answer, I wanted to use the header Script component code in C# using the section title. However, the markdown syntax removed the hash sign (#) and it currently displays the text as Script component code in C. The only option that worked was ending the text with some character other than hash sign (#) like period (.).

Things I have tried:

Here are the list of different combinations I have tried based on the escape characters I found on this MSO post. I couldn't make any of these work as I expected it to.

###Script component code in C\#
###Script component code in C`#
###Script component code in C*#
###Script component code in C_#
###Script component code in C{#}
###Script component code in C[#]
###Script component code in C(#)
###Script component code in C##
###Script component code in C+#
###Script component code in C-#
###Script component code in C.#
###Script component code in C!#
###Script component code in C ##
###Script component code in C#.

Results:

Result screenshot

Searches on MSO:

https://meta.stackexchange.com/questions/33986/how-do-i-escape-in-code

This question seems to be about escaping hash sign in code and not the section title. I could not find other references when I searched for markdown hash sign.

Question:

How do I escape the hash sign if I want to use it in the section title?

Reason why I am asking now:

To answer @Cyborgx37's comment:

I rewrote the answer on February 8, 2013 completely with the latest release of the software to keep the answer up to date and also added more details to be more useful.

0

4 Answers 4

39

You could use the alternative HTML syntax. Markdown parsing is disabled within HTML elements.

<h3>Script component code in C#</h3>

Or you could write the # as an HTML entity. They're allowed too.

###Script component code in C&#35;
0
29

The closing hashes can't have spaces, so if you do this:

###Script component code in C# #

...it will do what you want. The last hash is considered the close, and the one before is interpreted literally:

Script component code in C#

1
  • While typing the example given here in MarkdownPad2, which gives a live preview, I noticed that the desired hash is displayed as soon as the space after it is typed. So it doesn't seem necessary to add the final hash or escape the desired one. It seems that it just needs to to be at the end of the line. Commented Jun 28, 2016 at 8:35
6

You can use the other format for producing headers, by "underlining" the heading in question:

Script component code in C#
---------------------------- 

or

Script component code in C#
=============================

this produces

Script component code in C#

Underlining with - produces a level 2 h2 heading, while = produces a level 1 h1 heading. Admittedly neither of these are the h3 you're looking for, but they're all headings, you know. Semantically it's more accurate to use h2 as your first level subheading, since your question title is a h1.

Or you could just use HTML:

<h3>Script component code in C#</h3>

which produces

Script component code in C#

0
5

The markdown parser allows for any number of closing hash characters right before the newline, which are ignored.

You could simply add a space after the hash but before the newline, which is practically indistinguishable from your desired output:

Script component code in C#

In case you want to try figuring out a different way to skin this cat, you can have a look at the regex being used in the PageDown source:

/
    ^(\#{1,6})      // $1 = string of #'s
    [ \t]*
    (.+?)           // $2 = Header text
    [ \t]*
    \#*             // optional closing #'s (not counted)
    \n+
/gm

You must log in to answer this question.