How do I mark my inline code in questions and answers, and how can I mark code in comments?

share
add comment

1 Answer 1

up vote 23 down vote accepted

The simplest way is to surround the inline code by backticks `: To get the output \section, just type `\section`. If you don't like typing backticks, you can also highlight the inline code and hit Ctrl+K (or click the "code" button, with "{ }" on it), but this doesn't work in comments. In questions and answers, there are only two (quite rare) cases where things might go wrong (see below for backslash escaping of backticks in comments):

  1. You need a backtick in the inline code. Then you can either use double backticks `` or  <code>...</code>  to mark your code. For example, both

    • ``\catcode`@=11`` and
    • <code>\catcode`@=11</code>

    give \catcode`@=11. Another example: `` "` `` gives "` (note the spaces!).

  2. You want spaces in the beginning and/or the end of the inline code, or you need multiple spaces within. Then you have to use <code>...</code>:

    <code>&nbsp;a &nbsp; test&nbsp;</code>
    

    gives  a   test , which has 3 spaces between "a" and "test".

Thus, <code>...</code> is an alternative for backticks, but it behaves differently: You can use HTML entities such as &nbsp; inside. For example, <code>&nbsp;</code> gives  , whereas `&nbsp;` gives &nbsp;.

As a rule of thumb, use <code>...</code> only as a last resort. And then be aware that you can run into trouble if you try to use < inside: <code>a<b</code> gives a! The way out: <code>a&lt;b</code> gives a<b. Moreover, <code>a < b</code> gives a < b.


Until 20 February 2011, markdown in comments was quite a hassle. Now the rules are almost the same as for questions and answers. These are the differences:

  1. <code>...</code> to mark inline code doesn't work in comments.

  2. If you need a backtick in inline code in a comment, you can (and must) escape it (doesn't work in questions/answers!): `\catcode\`@=11` gives \catcode`@=11.

  3. #2 means that `\\` will not give \\ because the second backslash is interpreted as an escape for the subsequent backtick. This can be solved by using double backticks `` if the inline code ends with a backslash: ``\\`` gives \\.

  4. It is not possible to have spaces in the beginning and/or the end of inline code in comments.

One more important recent change: Inline code in comments can now be followed by a letter. For example, `\vspace`s now renders as \vspaces.

share
    
@Hendrik: Can you tell me how to backtick-quote something like "` (German opening quote). No combinations so far work. See this answer where I had this issue. –  Martin Scharrer Feb 11 '11 at 18:19
    
Found it. HTML came to the rescue: <code>"&#96;</code> –  Martin Scharrer Feb 11 '11 at 18:28
2  
@Martin: Easy, use double backticks, but with spaces: `` "` ``. –  Hendrik Vogt Feb 11 '11 at 18:45
add comment

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .