2

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?

0

3 Answers 3

3

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|
3
  • 1
    @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. May 2, 2011 at 11:10
  • Shouldn't your first sentence read "you can't"?
    – lockstep
    May 2, 2011 at 15:39
  • @lockstep: Uuh, yes of course. Thank you for pointing it out. May 2, 2011 at 15:43
3

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

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

0

You must log in to answer this question.

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