# Listings breaklines at predefined places

The code lines I write within a lstlisting sometimes are too long. So at the end a line break occurs. But sometimes these breaks are not well chosen and can make the code much more unreadable.

Is there a way to predefine the places where a break should appear if it is necessary?

My document:

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\usepackage{listings}

\lstdefinestyle{General} {
basicstyle=\small\ttfamily,
breaklines=true
}

\lstset{style=General}

\begin{document}

\begin{lstlisting}
This is a very long listing which eventually needs a break at the end of this beautiful line.
\end{lstlisting}

\end{document}

In this case the line break occurs after "break". It would be great to define the break after "listing" for example. Or maybe a second one after "beautiful" if there is enough space (maybe after changing some document settings as borders or format). In that case the first one should be ignored.

• Feb 25 '19 at 14:40
• My guess: No, there is no way. For this kind of content (verbatim) you have to take care manually. Feb 25 '19 at 14:42

• Maybe I misunderstand your question, "sometimes these breaks are not well chosen" does not make much sense for typical listing material.
• The content of a listing is treated a verbatim (aka: literally, as it is).
• Therefore you can add manual line breaks where ever you want.
• In addition, the listings package offers automatic line breaks (you enabled it). I marked the automatic line break with an $\hookrightarrow$ in addition the default indention.
• in addition, you can change the linewidth as an option (not shown).

BTW: I do not know why I need to use \mbox in \mbox{{$\hookrightarrow$}\space}. Also ~ instead of \space caused an error.

\documentclass{book}
\usepackage{listings}

\lstdefinestyle{myListingStyle}
{
basicstyle = \small\ttfamily,
breaklines = true,
postbreak = \mbox{{$\hookrightarrow$}\space} % See https://tex.stackexchange.com/questions/116534 for example
}

\begin{document}

\begin{lstlisting}[
style = myListingStyle,
caption = {Natural/Automatic line break.}
]
This is a very long listing which eventually needs a break at the end of this beautiful line.
\end{lstlisting}

\begin{lstlisting}[
style = myListingStyle,
caption = {Manual line break.}
]
This is a very long listing which eventually
needs a break at the end of this beautiful line.
\end{lstlisting}

\end{document}

• Thanks for your answer but unfortunately this does not help. To clarify: If you have a long code line with, e.g. a long mathematical term, I don't want the break to be between operation sign and variable. If it don't fit then the break should be at one point before (e.g. before beginning of the term, something I define). But I don't want to break it a priori. I just want to say: "Break here IF NECESSARY." Furthermore: A manual line break as yours creates a new line (if you enumerize the listing), the automatic before does not. Feb 25 '19 at 14:38