2

I cannot get listings package to break lines with any of the methods described in previous questions: Listings package does not break, https://stackoverflow.com/questions/981020/how-to-force-line-wrapping-in-listings-package

This problem appears only when using mwrep document class. In report or article line-breaks work just fine.

Here is my MWE:

\documentclass{mwrep}
\usepackage{listings}

\begin{document}
\begin{lstlisting}[ breaklines=true ]
Some text with a long line that needs to be broken but is not with mwrep documentclass. What should I do about it?
\end{lstlisting}
\end{document}

1 Answer 1

3

I identified that the culprit is mwrep document class and the fact that it sets \exhyphenpenalty=10000 in its source, which interferes with listings package ability to break lines.

The solution I found is changing it back to 100 before the listing and restoring the changed value at the end of the listing:

\documentclass{mwrep}
\usepackage{listings}
\usepackage{etoolbox}
\BeforeBeginEnvironment{lstlisting}{\exhyphenpenalty=100}
\AfterEndEnvironment{lstlisting}{\exhyphenpenalty=10000}

\begin{document}
\begin{lstlisting}[ breaklines=true ]
Some text with a long line that needs to be broken but is not with mwrep documentclass. What should I do about it?
\end{lstlisting}
\end{document}

Source of the solution (in Polish)

4
  • You can use the advantages of etoolbox to set \exhyphenpenalty at the beginning of each environment. Aug 10, 2012 at 11:50
  • Good point, I'll modify my solution.
    – stanwise
    Aug 10, 2012 at 14:58
  • Your example only seems to work if the code line starting with \AtEndEnvironment is dropped.
    – lockstep
    Aug 10, 2012 at 15:07
  • OK, it seems the right hook to use is \AfterEndEnvironment then.
    – stanwise
    Aug 10, 2012 at 15:22

You must log in to answer this question.

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