5

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.

1 Answer 1

6

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

7
  • 1
    I just looked up the keyword literate in the listings' document(it is mainly stated in 6.4 Literate programming), but I don't quit understand the means of the option literate=::1, can you give some explanation?
    – ollydbg23
    Aug 23, 2015 at 14:15
  • @ollydbg23 I believe the syntax is <find>:<replace>:<replacelength>. Why replacing nothing with nothing is supposed to fix this issue, I don't know...
    – Zero3
    Jan 3, 2016 at 3:59
  • 2
    @ollydbg23 @Zero3 a bit late to the party, but the syntax is {<find>}{<replace>}{<replacelength} without the colons. For single-characters you can omit braces, so ::1 is short for {:}{:}{1}, ie. replace the code-character : with the literate character : of character-length 1. A total hack, but works in this case, since : appear quite often in the columns.
    – ljrk
    Feb 27, 2020 at 15:02
  • 1
    Hi, @larkey, many thanks for the explanation. And yes, the jub0bs's answer can be improved by this explanation.
    – ollydbg23
    Feb 27, 2020 at 23:29
  • @larkey, I didn't see your edit, maybe your edit is not saved?
    – ollydbg23
    Mar 4, 2020 at 11:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .