[Tex/LaTex] Why doesn’t listings’ breaklines work on the colon character

line-breakinglistings

see the sample code and the screen shot:

\documentclass[UTF8]{article}
\usepackage{listings}
\usepackage{color}

\begin{document}
\lstset{
    basicstyle=\ttfamily\footnotesize,
    language=Python,
    breaklines=true,
    frame=single,
    breakatwhitespace=false, 
    numbers=left
}

%\lstinputlisting{code/test.py}
\begin{lstlisting}
class MyClass:
    def __init__(self):
    #revolve_map: Rotation invariant mode  36 characteristic values. Get a dictionary from small to large numbers.
    self.revolve_map={0:0,1:1,3:2,5:3,7:4,9:5,11:6,13:7,15:8,17:9,19:10,21:11,23:12,
              25:13,27:14,29:15,31:16,37:17,39:18,43:19,45:20,47:21,51:22,53:23,55:24,
              59:25,61:26,63:27,85:28,87:29,91:30,95:31,111:32,119:33,127:34,255:35}
    #uniform_map: Equivalent model 58 characteristic values. Get a dictionary from small to large numbers.
    self.uniform_map={0:0,1:1,2:2,3:3,4:4,6:5,7:6,8:7,12:8,
              14:9,15:10,16:11,24:12,28:13,30:14,31:15,32:16,
              48:17,56:18,60:19,62:20,63:21,64:22,96:23,112:24,
              120:25,124:26,126:27,127:28,128:29,129:30,131:31,135:32,
              143:33,159:34,191:35,192:36,193:37,195:38,199:39,207:40,
              223:41,224:42,225:43,227:44,231:45,239:46,240:47,241:48,
              243:49,247:50,248:51,249:52,251:53,252:54,253:55,254:56,
              255:57}

\end{lstlisting}

\end{document}

When building, I see many warnings like Overfull \hbox (42.20813pt too wide) in paragraph at lines 20--21
The result screen shot are show below.

screen shot

I have already use the breaklines=true option, I don't know how to wrap the long source code correctly.

Best Answer

This is due to a shortcoming of listings, which prevents it from breaking long sequences of digits and other symbol characters (even when breaklines is set and breakatwhitespace is cleared).

Here, an ad-hoc hack is to add

literate={:}{:}{1},

in the list of key-value pairs passed to \lstset in order to replace any colon : in the source, with the literate colon : of length 1; this will allow linebreaks after any colon as it is not treated as a "normal" symbol of sequence anymore.

enter image description here

Related Question