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}
stanwise
  • 151

1 Answers1

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)

stanwise
  • 151