Take the 2-minute tour ×
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.

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

2 Answers 2

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
1  
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
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 '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

 
discard

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.