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.

Does anyone know how to make inline listings wrap nicely? Take a look at the screenshot that I attached below. The code example in the second line goes outside the text margin.

Also, any ideas why inline listing \lstinline|{ print "" }| on the 3rd line of text removes the space after the quotes? It shows as { print ""} in the book, even though my listing is { print "" }.

enter image description here

Here is the whole LaTeX for the document you see in the screenshot,

\documentclass[11pt]{memoir} 

\usepackage{listings}
\usepackage{xcolor}
\usepackage{textcomp}
\usepackage[T1]{fontenc}

\begin{document}

\newcommand{\bb}[1]{\textbf{#1}}
\newcommand{\code}[1]{\emph{#1}}
\newcommand{\n}{\lstinline|\n|}

\definecolor{codebg}{HTML}{EEEEEE}
\definecolor{codeframe}{HTML}{CCCCCC}

\lstset{language=Awk}
\lstset{backgroundcolor=\color{codebg}}
\lstset{frame=single}
\lstset{framesep=10pt}
\lstset{rulecolor=\color{codeframe}}
\lstset{upquote=true}
\lstset{basicstyle=\ttfamily}

\lstset{emph={awk}, emphstyle=\textbf}

\chapter{Line Spacing}
\label{linespacing}    % So I can \ref{filespacing} later.

\section{Double-space a file}
\label{doublespacefile}

\begin{lstlisting}
awk '1; { print "" }'
\end{lstlisting}

So how does this one-liner work? A one-liner is an Awk program and every Awk program consists of a sequence of pattern-action statements \lstinline|pattern { action statements }|. In this case there are two statements \lstinline|1| and \lstinline|{ print "" }|. In a pattern-action statement either the pattern or the action may be missing. If the pattern is missing, the action is applied to every single line of input. A missing action is equivalent to \lstinline|{ print }|. Thus, this one-liner translates to:

\begin{lstlisting}
awk '1 { print }; { print "" }'
\end{lstlisting}

An action is applied only if the pattern matches, i.e., pattern is true. Since \code1 is always true, this one-liner translates further into two print statements:

\begin{lstlisting}
awk '{ print }; { print "" }'
\end{lstlisting}

Every print statement in Awk is silently followed by the \code{ORS} - Output Record Separator variable, which is a newline by default. The first print statement with no arguments is equivalent to \lstinline{print \$0}, where \code{\$0} is the variable holding the entire line (including the newline at the end). The second print statement seemingly prints nothing, but knowing that each print statement is followed by \code{ORS}, it actually prints a newline. So there we have it, each line gets double-spaced.

We can also drop the semicolon and write it as:

\end{document}
share|improve this question
    
Oh sorry. Editing my question right now. –  Peteris Krumins May 2 '11 at 9:37
    
@Ulrike, I have added a complete LaTeX document, if you pdflatex it, it will produce a document that matches the image that I posted. –  Peteris Krumins May 2 '11 at 9:41
    
@PeterisKrumins, the second part of your question seems to be a duplicate of tex.stackexchange.com/q/41934/1340 (still unsolved, unfortunately). –  Blaisorblade Feb 13 '12 at 14:18

1 Answer 1

up vote 8 down vote accepted

You can enable line breaking with the option breaklines, but I doubt that you will like the result (e.g. there will be line breaks after the braces) as listings can't know what you find "nice looking". I would probably split the code in two or more \lstinline commands.

The missing space is at my opinion a bug. The parsing for string delimiters loose the space (but only for fixed-width fonts). You can either use two spaces after a ", or remove the quote as string delimiter with \lstset{deletestring=[b]{"}}

share|improve this answer

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.