TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I created a new macro \newcommand{\n}{\lstinline|\n|} but for some reason when I use it, all I get is bold 'n' letter and not a nice \n. Any ideas?

share|improve this question
up vote 4 down vote accepted

As Ulrike already posted you can't use verbatim macros or environments inside a macro argument or replacement text. To make \n typeset \n as verbatim you could use:


or use the verbdef package:

share|improve this answer
Martin, your command seems to ignore space after \n. For example, foo \n boo gets typed in the docuemnt as foo \nboo. I had heard about using \xspace but I can't quite get it in the command, check this out: \newcommand{\n}{\texttt{\string\n}\xspace} fails to compile when I run pdflatex with Undefined control sequence. Same for \newcommand{\n}{\texttt{\string\n\xspace}} – Peteris Krumins May 2 '11 at 10:18
@Peteris: All spaces behind all macros are always ignored. You need to load the xspace package to get xspace and it must be the very last of the macro replacement text. Alternatively you can write \n{} instead. – Martin Scharrer May 2 '11 at 11:10
Thank you Martin, I had forgotten to load xspace package! – Peteris Krumins May 2 '11 at 11:17
Shouldn't your first sentence read "you can't"? – lockstep May 2 '11 at 15:39
@lockstep: Uuh, yes of course. Thank you for pointing it out. – Martin Scharrer May 2 '11 at 15:43

\lstinline is a verbatim-like command. It has to do a lot of \catcode-magic to disable commands and parse its argument. You can't use it in another command. http://www.tex.ac.uk/cgi-bin/texfaq2html?label=verbwithin.

share|improve this answer
I went with \verbdef\n|\n| for now. – Peteris Krumins May 2 '11 at 10:58

In fact, you can use lstinline to define custom commands. I define short commands to include snippets in a specific formal language like this:


If the language foo is not defined, you must first define it with

\lstdefinelanguage{foo}{ ... }

In your code you can then use the custom command like this. Some special characters like {,}, and ^ must be escaped:

Blabla \foo{this is syntax highlighted} blabla,
bla \foo{This contains \{ braces \}}.
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.