4

A lstlisting environment with prebreak may break lines unnecessarily. Here is an MWE:

\documentclass[draft]{article}
\usepackage[final]{listings}
\begin{document}

\begin{lstlisting}
1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7
1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9
\end{lstlisting}

\begin{lstlisting}[breaklines=true,prebreak=\space...]
1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7
1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9
\end{lstlisting}

\end{document}

With breaklines=false, the line with 57 characters fits within the text block, while the line with 59 characters causes an overfull \hbox. Therefore, I would like lstlisting to leave the 57-character line alone but break the 59-character line. However, with breaklines=true, both lines are broken.

Is it possible to automatically break a line only if it exceeds the margins without the prebreak tokens?

5
  • Welcome to TeX.SE. Oct 16, 2014 at 5:15
  • No offense, but using prebreak=\space... is a really bad idea, if only because listings may introduce line breaks at places where the MATLAB syntax doesn't allow it (e.g. inside a string literal). That would make your MATLAB listings look incorrect.
    – jub0bs
    Oct 16, 2014 at 13:07
  • I have few string literals or comments. I would rather fix them manually rather than breaking every line manually. Oct 16, 2014 at 16:32
  • I am also curious why the behavior occurs. My understanding is that the TeX linebreaking algorithm is essentially hyphenating my code from the use of \discretionary in the listings package. Why would TeX invoke a discretionary when the line fits fine without it? Oct 16, 2014 at 16:34
  • @BrianSutton String literals are just one example of many cases where listings may introduce syntactically incorrect line breaks. Also, the line breaking performed by listings has nothing to do with the line breaking normally performed by TeX.
    – jub0bs
    Oct 17, 2014 at 8:27

2 Answers 2

2

This is not really an answer to your question, but a case against your approach; if you take heed of it, your problem will become irrelevant.

Don't rely on listings to introduce syntactically correct line breaks

My understanding is that you're typesetting MATLAB listings and you want the line-continuation token (...) to be introduced wherever listings' line-breaking mechanism (breaklines) introduces a line break. That's a really bad idea, because, that way, listings may introduce line breaks (and ...) at places where the MATLAB syntax doesn't allow it.

Consider the example below. The typeset listing shows ... occurring inside the string literal. However, in MATLAB, a string literal must end on the same line it began, and any ... occurring within it is treated as just part of the string, not as a line continuation. Therefore, the output (i.e. the typeset listing) is misleading and shows incorrect MATLAB syntax, even though the source written inside the lstlisting environment is syntactically correct. Surely, that's not what you want...

enter image description here

This example should be enough to convince you that you (and your readers) should understand any line break that listings introduces as a typesetting convenience, not as a syntactically correct line break in the language considered.

\documentclass{article}

\usepackage{listings}

\lstset{
  language=Matlab,
  breaklines=true,
  prebreak=\space...,
  showstringspaces=false,
}

\begin{document}

\begin{lstlisting}
disp('Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper')
\end{lstlisting}

\end{document}

An alternative approach

If what you want is mark occurences of line breaks introduced by listings but without compromising the syntactic validity of your listings, I recommend using line numbers as cues. See the example below. For any given line, you can easily tell whether listings introduces a line break on the preceding line or not, simply by checking whether the current line has a line number (on the left).

enter image description here

\documentclass{article}

\usepackage[numbered,framed]{matlab-prettifier}

\lstset{style=Matlab-editor}

\begin{document}

\begin{lstlisting}
disp('Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper')
x = linspace(1,4,11) 
\end{lstlisting}

\end{document}

Edit: for information, matlab-prettifier is just a little package, built on top of listings, that is meant to typeset MATLAB code as nicely as possible.

0

Use

\begin{lstlisting}[columns=flexible]
1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7
1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9
\end{lstlisting}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .