How do I mark code blocks, and how do I adjust syntax highlighting? Can I emphasize pieces of my code block? And how do code blocks in lists and blockquotes work?


1 Answer 1


The simplest way is to indent your code block by 4 spaces: To get the output


just type


(The first empty line is essential, the last one is recommended.) If you have more than one line, then it's simpler to highlight the code block and hit Ctrl+K (or click the "code" button, with "{}" on it). This auto-indents the highlighted code by 4 spaces (and also adds the empty lines in the beginning and in the end).

enter image description here

See below for code blocks in lists and blockquotes.

Syntax highlighting

By default, your code gets some LaTeX syntax highlighting. (This doesn't work on meta.tex.sx, so unfortunately you can't see it here.) If you don't want this syntax highlighting, you can use a special HTML comment to specify the language. For no highlighting, use

<!-- language: lang-none -->

    Document Class: elsarticle 2009/09/17, 1.2.0: Elsevier Ltd

(You wouldn't want \MiKTeX to be highlighted as a control sequence, right?) Note that the HTML comment must not be indented, and that the blank line between the <!-- language: ... --> and the indented code block is required. For a list of supported languages have a look here: Manually specify language for syntax highlighting.

Emphasizing pieces of the code

If you want to highlight parts of your code using bold or slanted text, or if you want to strike something out, you have to use the HTML <pre> tag instead of 4 space indentation. For example,




(Note that the <pre> and </pre> tags should be on separate lines, without indentation.) For striking something out, use <s>...</s>. Using just <pre> you don't get syntax highlighting, so you can use this as an alternative to <!-- language: lang-none -->. Be careful, however, to use &lt; and &gt; if you want < and > in <pre> blocks; you may also need &amp; for &.

If you want syntax highlighting and additional emphasis in bold or slanted, use <code> inside the <pre> block:


Note that there are no line breaks after <code> and before </code>; otherwise the code block would start and end with an empty line (as the second code block in this very answer).

Code blocks in lists

If your code appears inside a list, you must indent a further four spaces for every level of nesting. An example:

1. The first point I want to make

        This is the code

1. The second point I want to make


  1. The first point I want to make

     This is the code
  2. The second point I want to make

So you need 8 spaces in the first level, 12 spaces in the second level and so on.

Code blocks after lists

If you want a code block immediately after a list, you need to put an HTML comment between the list and the code block:

1. This is part of a list

<!-- an HTML comment -->

    This is the code


  1. This is part of a list
This is the code

Code blocks in blockquotes

A blockquote is started with , and a code block with 4 spaces, so for a code block within a blockquote you need 5 spaces after the >. An example:

> This is (not) a quote from the TeXbook, and here's some code:

>     \relax


This is (not) a quote from the TeXbook, and here's some code:

  • @TH: Oh my, thanks for the correction. I managed to delete the spaces in one of the edits. Probably I was confused by the nasty preview. Mar 22, 2011 at 20:41
  • Any idea why the preview is so horrible for this post?
    – TH.
    Mar 22, 2011 at 23:23
  • @TH: Yes. But there's no satisfactory answer yet. Probably I'll remove the line break before </pre> and maybe even mention the problem in the post here, but first I'll wait some more time for an answer. Mar 23, 2011 at 9:09
  • Ah. Okay.
    – TH.
    Mar 23, 2011 at 16:16
  • @TH: Tim Stone just gave an excellent explanation. (I see you're still located in Changdad :-)) Jul 24, 2011 at 10:28
  • Interesting. Thanks. As for Changdad, it's close enough to "here" for me. This is the Internet, no need for meatspace coordinates.
    – TH.
    Jul 31, 2011 at 20:40

You must log in to answer this question.

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