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?
3 Answers
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|
-
1@Peteris: All spaces behind all macros are always ignored. You need to load the
xspace
package to getxspace
and it must be the very last of the macro replacement text. Alternatively you can write\n{}
instead. May 2, 2011 at 11:10 -
-
@lockstep: Uuh, yes of course. Thank you for pointing it out. May 2, 2011 at 15:43
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 \}}.
\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.
https://texfaq.org/FAQ-verbwithin.