为什么列表的断线对冒号字符不起作用

为什么列表的断线对冒号字符不起作用

查看示例代码和屏幕截图:

\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}

在构建时,我看到许多警告,例如Overfull \hbox (42.20813pt too wide) in paragraph at lines 20--21 结果屏幕截图如下所示。

截屏

我已经使用了该breaklines=true选项,但我不知道如何正确包装长源代码。

答案1

这是由于 的缺点listings,导致它无法破坏长序列的数字和其他符号字符(即使breaklines设置并breakatwhitespace清除了)。

这里,一个临时的 hack 是添加

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

在传递给的键值对列表中,\lstset以便用长度为的:文字冒号替换源中的任何冒号;这将允许任何冒号后出现换行符,因为它不再被视为“正常”的序列符号。:1

在此处输入图片描述

相关内容