# lstlisting line wrapping

How is it possible to let lstlisting wrap lines?

I've troubles with following block:

\begin{lstlisting}[language=java]
public class MeasureStationControllerV0Test {
MeasureStationControllerV0 controller;
MeasureStation ms = new MeasureStation();
MeasureStationConfiguration config = new MeasureStationConfiguration();
DataServer dataserver = new DataServer();


In the PDF output there is an overflow:

Other code-snippets look horrible too.

• So have I to manually wrap them or is listings able to handle that?
• Why isn't syntax highlighting working?
• add breaklines=true to the options of the lstlisting environment May 28, 2013 at 15:16
• breaklines=true worked for me, but either it doesn't look nice. howerver, can you post als answer so i can accept? May 28, 2013 at 15:18
• For your second question (Why does Syntax hilightening isn't working?) we'd need to see a minimal working example (MWE) and explain what's wring. My guess would be that you have to define additional keywords. May 28, 2013 at 15:22
• @cgnieder I am curious on how do we define additional keywords. In my case, as is never highlighted when I use this package with Python. May 28, 2013 at 15:45
• @clemens That's what I needed! Thanks you so much! I spent an hour trying to fix this... Jun 6, 2019 at 1:50

Use the options breaklines=true and postbreak=\mbox{\textcolor{red}{$\hookrightarrow$}\space} for placing a red arrow at the beginning of the broken line to emphasize the line break.

\documentclass{article}
\usepackage{lmodern}  % for bold teletype font
\usepackage{amsmath}  % for \hookrightarrow
\usepackage{xcolor}   % for \textcolor
\usepackage{listings}
\lstset{
basicstyle=\ttfamily,
columns=fullflexible,
frame=single,
breaklines=true,
postbreak=\mbox{\textcolor{red}{$\hookrightarrow$}\space},
}
\begin{document}
\begin{lstlisting}[language=java]
public class MeasureStationControllerV0Test {
MeasureStationControllerV0 controller;
MeasureStation ms = new MeasureStation();
MeasureStationConfiguration config = new MeasureStationConfiguration();
DataServer dataserver = new DataServer();
\end{lstlisting}
\end{document}


With the minted package you get nice line breaking and syntax highlighting out-of-the-box. Simply specify the breaklines option on your snippet. The downside is that you have to process the document with the --shell-escape option because the external program pygmentize is used to format the source code.

\documentclass{article}
\usepackage{lmodern} % for bold teletype font
\usepackage{minted}
\begin{document}
\begin{minted}[breaklines,frame=single]{java}
public class MeasureStationControllerV0Test {
MeasureStationControllerV0 controller;
MeasureStation ms = new MeasureStation();
MeasureStationConfiguration config = new MeasureStationConfiguration();
DataServer dataserver = new DataServer();
\end{minted}
\end{document}


To complement the answer from @henri-menke, besides the option breaklines=true and a postbreak arrow, I find it useful to make the arrow non-selectable in text (works for pdflatex and most pdf readers). This advice comes from another answer using accsupp package: Is there a way to make a "non-selectable" space in LaTeX?

To summarize the solution:

\usepackage{accsupp}
% ...
\lstset{
basicstyle=\ttfamily,
columns=fullflexible,
postbreak=\raisebox{0ex}[0ex][0ex]{\BeginAccSupp{ActualText={}}\ensuremath{\color{gray}\hookrightarrow\space}\EndAccSupp{}},
breaklines=true
}


A typical arrow could be \raisebox{0ex}[0ex][0ex]{\ensuremath{\color{gray}\hookrightarrow\space}}, but in this case, we put it between \BeginAccSupp{ActualText={}} and \EndAccSupp{}, so that its symbol becomes non-existent for copy and paste.

• I assume this is to make it possible to copy-paste code from the PDF. Unfortunately this is of very limited usefulness, because the PDF does not preserve indentation which is crucial in some programming languages and extra precautions have to be taken to treat some special characters like ' correctly. Jul 14 at 18:41
• That's true @HenriMenke , my use-case was for C#, which can survive the loss of indenting... but for python or haskell, that would be fatal. I couldn't find a better solution to keep indent, as my editors refuse to keep it, even when replacing space chars with other non-visible alternatives... maybe replace with a special symbol (such as §) and then replace it manually, could be a possibility. Jul 18 at 22:36