# Macro with \n doesn't work, not sure why.

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?

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.

-

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

-
Trying '\textbackslash{n} –  Peteris Krumins May 1 '11 at 12:57
Or use \lstinline|\n| –  Seamus May 1 '11 at 12:58
It worked. I probably want to alias it as \bs or \s or something so I don't go crazy typing it out every time. ;) –  Peteris Krumins May 1 '11 at 12:59
@Seamus, oh, let me try that, too! –  Peteris Krumins May 1 '11 at 12:59
@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 '11 at 13:01

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

-
I tried this for some of the code but immediately ran into problems - the code for some reason it went over the book's margins. Please take a look here: i.imgur.com/7S9cE.png Any ideas how to make it wrap nicely? –  Peteris Krumins May 1 '11 at 13:06
@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 '11 at 13:48
It would be an interesting question indeed if it has not yet been asked. –  Bruno Le Floch May 1 '11 at 19:31