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
The simplest way is to indent your code block by 4 spaces: To get the output
\documentclass[paper=a5,pagesize]{scrbook}
just type
\documentclass[paper=a5,pagesize]{scrbook}
(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).
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 -->
(J:\MiKTeX2.9\tex\latex\elsarticle\elsarticle.cls
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,
<pre>
\documentclass[paper=a5,<i>pagesize</i>]{scrbook}
\usepackage<b>[draft]</b>{graphicx}
</pre>
gives
\documentclass[paper=a5,pagesize]{scrbook} \usepackage[draft]{graphicx}
(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 <
and >
if you want <
and >
in <pre>
blocks; you may also need &
for &
.
If you want syntax highlighting and additional emphasis in bold or slanted, use <code>
inside the <pre>
block:
<pre>
<code>\documentclass[paper=a5,<i>pagesize</i>]{scrbook}
\usepackage<b>[draft]</b>{graphicx}</code>
</pre>
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
gives
The first point I want to make
This is the code
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
gives
- 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
gives
This is (not) a quote from the TeXbook, and here's some code:
\relax
-
@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. Commented Mar 22, 2011 at 20:41
-
-
@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. Commented Mar 23, 2011 at 9:09 -
-
@TH: Tim Stone just gave an excellent explanation. (I see you're still located in Changdad :-)) Commented 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.Commented Jul 31, 2011 at 20:40