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?

lockstep
  • 250,273

3 Answers3

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.

David Carlisle
  • 757,742
Ulrike Fischer
  • 327,261
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|
Martin Scharrer
  • 262,582
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 \}}.
Jakob
  • 1,234