Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I cannot get listings package to break lines with any of the methods described in previous questions: Listings package does not break, http://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}
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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)

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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