# Listings does not break at pure hyphen lines

I have the issue, that I need listings package to break at a hyphen (-). I will give a MWE below.

According to https://tex.stackexchange.com/a/140725/7166 the hyphen is a character of type "other", which should allow a line break if breakatwhitespace=false (the default setting). In my code to be typeset (included file generated by other software), long separation lines of pure hyphens are given. These seem not to break as expected.

Instead, only at the boundary letter/other character a possible linebreak might be done. Adding \allowbreak as in https://tex.stackexchange.com/a/208254/7166 does not help.

Is this a bug in listings or am I missing some option?

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\lstset{
basicstyle=\ttfamily\small,
breaklines=true,
frame=single
}
\begin{document}
\begin{lstlisting}
----------------------------------------------------a-------------------------------------------
-----------------------------------------------------------------------------------------------
\end{lstlisting}
\end{document}

• listings' tokenization mechanism is a bit more complicated, I think the exact rules aren't even mentioned in the user documentation but only in source code comments. However, adding literate={-}{{-\allowbreak}}{1}, as proposed in the linked answer, fixes the problem for me. – siracusa Dec 7 '18 at 16:46
• Interesting.... When I pot the literate=... inside a \lstset{} it works (also in my main document). When I put it in the optional agrument of the lstlisting environment, this is no longer true. – Christian Wolf Dec 10 '18 at 8:23
• Strange, using \begin{lstlisting}[literate={-}{{-\allowbreak}}{1}] in your above sample document works fine for me – siracusa Dec 10 '18 at 9:16
• Very strange. After playing around with this for some time now, I can no longer reproduce it. Either it was something in an update I just installed or I did not reach exactly the same code I tried with the optional argument. Maybe I was just dump. Thanks for helping anyways. Would you mind putting together a short answer, then I can accept? – Christian Wolf Dec 11 '18 at 10:20

Using the literate option to allow breaks after - works in your example. Make sure to have the latest version of the listings package installed. You can set the option globally with

\lstset{literate={-}{{-\allowbreak}}{1}}


or selectively for single listings with

\begin{lstlisting}[literate={-}{{-\allowbreak}}{1}]


A word on why breaking doesn't work here by default. Section 9.5 of the extended listings documentation describes how character sequences are built:

How do these 'classes' work together? Let's say that the current character string is tr. Then letter y simply appends the letter and we get try. The next nonletter (and nondigit) causes the output of the characters. Then we collect all coming nonletters until reaching a letter again. This causes the output of the nonletters, and so on.

That means all the hyphen characters are glued together into a single, token-like sequence and are not broken at the end of the line, unless you use the literate option to explicitly allow breaks inside it.