# How to prevent line breaks in code listings?

I am adding a big Linux terminal output as a listing and would like to prevent these line breaks from happening:

e.g.

user@pc ~ \$ top
PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM
TIME+ COMMAND
19173 user      20   0   26720   1572   1048 R   6.4  0.0
0:00.01 top
1 root      20   0   33904   3200   1496 S   0.0  0.0
0:06.67 init


EDIT: Working solution

\documentclass{article}
\usepackage{linegoal,listings}
\lstdefinestyle{Bash}
{
language=bash,
breaklines=false,
showspaces=true,
}
\makeatletter
\def\lst@visiblespace{\lst@ttfamily{\char32} } % <-- hack
\makeatother

\begin{document}

\begin{lstlisting}[style=Bash, caption={bash output}]
PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
19173 user      20   0   26720   1572   1048 R   6.4  0.0   0:00.01 top
1 root      20   0   33904   3200   1496 S   0.0  0.0   0:06.67 init
2 root      20   0       0      0      0 S   0.0  0.0   0:00.01 kthreadd
3 root      20   0       0      0      0 S   0.0  0.0   0:04.00 ksoftirqd/0
\end{lstlisting}

\end{document}


Looks like a bug of listings package, because no line breaks should be produced unless you explicitly request them with breaklines=true.

Moreover, if you use showspaces=true, then line breaks are not produced (but you have all spaces visible as ␣, of course).

This suggested me the following hack: use showspaces=true, but redefine the "visible space" to be a blank space instead of ␣.

This is the code:

\documentclass{article}
\usepackage{linegoal,listings}
\lstdefinestyle{Bash}
{
language=bash,
breaklines=false,
showspaces=true,
}
\makeatletter
\def\lst@visiblespace{\lst@ttfamily{\char32} } % <-- hack
\makeatother

\begin{document}

\begin{lstlisting}[style=Bash, caption={bash output}]
PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
19173 user      20   0   26720   1572   1048 R   6.4  0.0   0:00.01 top
1 root      20   0   33904   3200   1496 S   0.0  0.0   0:06.67 init
2 root      20   0       0      0      0 S   0.0  0.0   0:00.01 kthreadd
3 root      20   0       0      0      0 S   0.0  0.0   0:04.00 ksoftirqd/0
\end{lstlisting}
\end{document}


Result:

This works but of course produces a listing which is too wide for the text area and overlaps the right margin. You can reduce the font in the listing, or change the page layout, etc.

• Nice hack! Is there no way to change the size to \textwidth in a scaled minipage? – RootRaven Jul 22 '15 at 10:12
• Minipages only reduce the space allowed for their contents, not automatically rescale them. If the contents don't fit, the minipage is overfull and its contents "spill out". So the result would be the same. You have to find (by trial and error) an appropiate font size for that listing. – JLDiaz Jul 22 '15 at 10:14
• Adjustbox did the trick, see the edited question. Thank you! – RootRaven Jul 22 '15 at 10:18
• I wouldn't recommend to use adjustbox. The resulting font size will not probably match any other font size in the document. Moreover, the listing caption is also reduced, and since it was centered on the whole box before scaling, it will be not centered in the margins. Compare your solution with the one using basicstyle=\scriptsize and no adjustbox. – JLDiaz Jul 22 '15 at 10:23
• I'd suggest changing the font as well -- look at "COMMAND" in the image above. I used latin modern tt condensed: usepackage{lmodern} and lstset{basicstyle=\footnotesize\ttfamily\fontseries{lc}\selectfont} should be enough I think. This will deal with the width issue as well. – Chris H Jul 22 '15 at 11:11