3

I created a macro called \code to wrap all the code fragments in my book,

\newcommand{\code}[1]{\emph{#1}}

But for some reason when I use this \code{\\n}, I never get the \n displayed. For some reason it turns it into a real new-line and text gets wrapped at that point.

Here is a screenshot that illustrates it. Any ideas what is going on?

enter image description here

And here is LaTeX code that creates this:

\begin{lstlisting}
awk '{ print $0 "\n" }'
\end{lstlisting}

This one-liner appends the newline symbol \code{\\n} to the
whole line \code{\$0} and prints it.

2 Answers 2

5

\\ is a TeX macro that creates a new line. To typeset a backslash outside a verbatim environment like lstlisting, use \textbackslash.

2
  • 1
    Or use \lstinline|\n|
    – Seamus
    May 1, 2011 at 12:58
  • @Peteris You could make \n a macro that writes \lstinline|\n| to save yourself some time. I added an answer to this effect...
    – Seamus
    May 1, 2011 at 13:01
3

Since you are writing code, I'd recommend putting it in a verbatim like environment, rather than just \emph: use \lstinline|\n| to have your \n stand out. You can set up listings to have the inline code appear italicised if that's what you want...

2
  • 1
    @Peteris No. I don't know how to make inline listings wrap nicely. I guess you'd need to explicitly add hyphenations... No I'm not sure. That's a separate question, really, though...
    – Seamus
    May 1, 2011 at 13:48
  • It would be an interesting question indeed if it has not yet been asked. May 1, 2011 at 19:31

You must log in to answer this question.