14

The following code does not break even though the breaklines and breakatwhitespace is set:

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

result of the code above

Any ideas what I can set in order to make it break automatically on the value part?

1
  • @user202729 That’s a great suggestion. Done.
    – Davislor
    Nov 10, 2021 at 18:03

1 Answer 1

13

As described in your question the package listings can automatically break long lines. The is limited. The package breaks only on characters which are defined as others. To allow the break at every point you must say listings that a letter isn't a letter. This can be done be redefining the following definition:

Original:

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

Redefinition

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

The complete code is:

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

To highlight the automatic line breaks you can work with the options postbreak and prebreak.

enter image description here

3

You must log in to answer this question.

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