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:

overlapping code

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?
  • 7
    add breaklines=true to the options of the lstlisting environment – clemens May 28 '13 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? – MemLeak May 28 '13 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. – clemens May 28 '13 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. – dustin May 28 '13 at 15:45
up vote 74 down vote accepted

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}

enter image description here


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}

enter image description here

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.