# What, in the custom class I use, prevents line breaking in listings?

I have found how to wrap code inside lstlisting block. When my document has article class, everything works as expected.

However, when I change the document's class to one I need in my work, lines do not break anymore. I'm not an author of this class and I have not a clue where the problem can be burried. Can you help me?

Minimal Working Example:

\documentclass{templates/mgragh}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage[polish]{babel}
\usepackage[OT4]{fontenc}
\usepackage{color}
\lstset{
frame=single,
breaklines=true,
postbreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\color{red}\hookrightarrow\space}}
}
\begin{document}
\begin{lstlisting}
public void createVoiceComment(File recodedFile) throws CommentNotAddedException;
\end{lstlisting}
\end{document}


Document class available here: https://copy.com/sijTRKuBQ9L2

-
Please edit your question to add a link to the class in question and post a minimal working example (MWE) here. We don't trust zip files too much... –  Jubobs May 6 '14 at 20:34
I don't know what, in those two classes you link to, prevents line breaking in listings. Because those classes are not mainstream, you may have better luck contacting the author directly. Have you tried that? –  Jubobs May 6 '14 at 21:58
No, I didn't. I believed I would have bigger chance to solve the problem by asking here. Anyway, thanks for looking into it! –  fracz May 6 '14 at 22:09
That link gets me a completely blank page. There's code for the html but I don't get anything displayed. I guess it wants javascript or some such but I don't see any reason that should be necessary to display a text file. –  cfr May 8 '14 at 22:09

Replacing line 172 in mbwk.cls,

\exhyphenpenalty=10000


by

\exhyphenpenalty=9999


does the trick.

The mwbk.cls class, which is loaded internally by the class you use (mgraph), sets, at lines 171-172,

\hyphenpenalty=100
\exhyphenpenalty=10000


Contrast these values to those used by Plain TeX (and by the LaTeX kernel):

\hyphenpenalty=50
\exhyphenpenalty=50


What are those macros used for? Here is a relevant passage from the TeXbook (bottom of page 96, chapter 14):

Each potential breakpoint has an associated “penalty,” which represents the “aesthetic cost” of breaking at that place. [...] Plain TeX sets \hyphenpenalty=50 and \exhyphenpenalty=50.

Exercise 14.6...

Explain how you could instruct TeX not to make any breaks after explicit hyphens and dashes. (This is useful in lengthy bibliographies.)

and its solution...

\exhyphenpenalty=10000 prohibits all such breaks, according to the rules found later in this chapter. Similarly, \hyphenpenalty=10000 prevents breaks after implicit (discretionary) hyphens.

... are also relevant.

Apparently, the author of those classes (mwbk and mgraph) wanted, by setting \exhyphenpenalty to such a high value (10000 is considered by TeX as infinitely bad), to prevent any breaks after explicit hyphens and dashes. However, this also seems to prevent line breaking in listings. In fact, simply decrementing the \exhyphenpenalty value by one on line 172 in mwbk.cls

\exhyphenpenalty=9999


seems to solve the problem.

\documentclass{templates/mgragh}
\usepackage[utf8]{inputenc}
\usepackage{listings}

\lstset{
frame=single,
breaklines=true,
}
\begin{document}
\begin{lstlisting}
public void createVoiceComment(File recodedFile) throws CommentNotAddedException;
\end{lstlisting}
\end{document}

-
Thank you so much, problem solved indeed. I would have never found it! –  fracz May 9 '14 at 7:33