# Any ideas why this \newcommand{\n}{\lstinline|\n|} macro doesn't work?

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?

-

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:

\newcommand{\n}{\texttt{\string\n}}


or use the verbdef package:

\verbdef\n|\n|

-
@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.

-
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:

\newcommand\foo[1]{\lstinline[language=foo]{#1}}


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 \}}.

-