TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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:

awk '{ print $0 "\n" }'

This one-liner appends the newline symbol \code{\\n} to the
whole line \code{\$0} and prints it.
share|improve this question
up vote 6 down vote accepted

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

share|improve this answer
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...

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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