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

6

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

7
  • Trying '\textbackslash{n}` May 1, 2011 at 12:57
  • 1
    Or use \lstinline|\n|
    – Seamus
    May 1, 2011 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. ;) May 1, 2011 at 12:59
  • @Seamus, oh, let me try that, too! May 1, 2011 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, 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...

3
  • 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? May 1, 2011 at 13:06
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.