我怎样才能让 listings 包将一个单词分成多行?

我怎样才能让 listings 包将一个单词分成多行?

即使设置了 breaklines 和 breakatwhitespace,以下代码也不会中断:

\lstset{
  language=XML,
  numbers=left,        
  numberstyle=\tiny\color{gray},
  stepnumber=1,                   
  numbersep=5pt,                  % how far the line-numbers are from the code
  backgroundcolor=\color{white},      % choose the background color. You must
  showspaces=false,               % show spaces adding
  showstringspaces=false,         % underline spaces
  showtabs=false,                 % show tabs within strings
  frame=single,                   % adds a frame
  rulecolor=\color{black},        % if not set, the frame-color
  tabsize=2,                      % sets default tabsize to 2 spaces
  captionpos=b,                   % sets the caption-position to bottom
  breaklines=true,                % sets automatic line breaking
  breakatwhitespace=false,        % sets if automatic breaks should only happen
  title=\lstname,                   % show the filename of files
                                  % also try caption instead of title
  keywordstyle=\color{blue},          % keyword style
  commentstyle=\color{dkgreen},       % comment style stringstyle=\color{mauve},
  breakautoindent=true
          % string literal style
}

\lstset{caption={Results of the example},label=lst:results}
\begin{lstlisting}
  <message>
    <value>c4cf0c02c2fa9386651535b2d2658129f10ac013fcc4e490b0ebd1cb006827f09ad15719b43f85d1e0ae4258a75ae4e6ad2ca5q9b5f93c149692ea9a194d7e245b27ae4e95ef538c1ee51f5b18b1a74878d66814b67bd1e308cc69d269f896b336a15e5dd5f56faaf0959d344af238be5b15abb0291851e53b6d67a5d2ff9b915d0f75800a8483c5d4f1ae9906f4702f2e0ceee87f6184c79b31648df414a49c4927591cd1a0bd44e74e834e06ca3c9f551b8b689a70672c833704960e17c74558099c99e4ac4ce3f01cad625a985737eb275433e0361ea5492a2bbb1f0058ff30191c1f1960327b0039de5a4cc05483ab55558cc91b440c807a25e373ddf7ad3c8a4d7e0c327e41e887bd668fd3ea96f</value>
  </message>
\end{lstlisting}

上述代码的结果

有什么想法我可以设置什么来使其在值部分自动中断?

答案1

正如您的问题所述,该包listings可以自动中断长行。 是有限的。该包仅在定义为 的字符上中断others。要允许在每个点处中断,您必须说listings字母不是字母。这可以通过重新定义以下定义来实现:

原来的:

\def\lst@lettertrue{\let\lst@ifletter\iftrue}

重新定义

\def\lst@lettertrue{\let\lst@ifletter\iffalse}

完整代码如下:

\documentclass{article}
\usepackage[T1]{fontenc}

\usepackage{xcolor}
\usepackage{listings}
\lstset{
  breaklines=true,                % sets automatic line breaking
}
\makeatletter
\def\lst@lettertrue{\let\lst@ifletter\iffalse}
\makeatother
\begin{document}


\begin{lstlisting}[caption={Results of the example},label=lst:results,]
  <message>
    <value>c4cf0c02c2fa9386651535b2d2658129f10ac013fcc4e490b0ebd1cb006827f09ad15719b43f85d1e0ae4258a75ae4e6ad2ca5q9b5f93c149692ea9a194d7e245b27ae4e95ef538c1ee51f5b18b1a74878d66814b67bd1e308cc69d269f896b336a15e5dd5f56faaf0959d344af238be5b15abb0291851e53b6d67a5d2ff9b915d0f75800a8483c5d4f1ae9906f4702f2e0ceee87f6184c79b31648df414a49c4927591cd1a0bd44e74e834e06ca3c9f551b8b689a70672c833704960e17c74558099c99e4ac4ce3f01cad625a985737eb275433e0361ea5492a2bbb1f0058ff30191c1f1960327b0039de5a4cc05483ab55558cc91b440c807a25e373ddf7ad3c8a4d7e0c327e41e887bd668fd3ea96f</value>
  </message>
\end{lstlisting}
\end{document}

要突出显示自动换行符,您可以使用选项postbreakprebreak

在此处输入图片描述

相关内容