3

How can I automatically break long numbers in a listing? Redefining \lst@lettertrue doesn't work.

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

\usepackage{listings}
\lstset{
  breaklines=true
}
\makeatletter
\def\lst@lettertrue{\let\lst@ifletter\iffalse}
\makeatother

\begin{document}
\begin{lstlisting}
  5646534535677654567453241456765467863576465467345675856382436457687534678665425769888843222222222587
\end{lstlisting}
\end{document}
3

You can use a literate one-to-one replacement of all numerals (as reference, see How can I format all numbers in listings?):

enter image description here

\documentclass{article}

\usepackage{listings}% http://ctan.org/pkg/listings

\lstset{
    breaklines=true,
}

\begin{document}

\begin{lstlisting}
  5646534535677654567453241456765467863576465467345675856382436457687534678665425769888843222222222587
\end{lstlisting}

\lstset{% Literate one-to-one mapping of numerals
    literate={0}{0}{1}%
             {1}{1}{1}%
             {2}{2}{1}%
             {3}{3}{1}%
             {4}{4}{1}%
             {5}{5}{1}%
             {6}{6}{1}%
             {7}{7}{1}%
             {8}{8}{1}%
             {9}{9}{1}%
}

\begin{lstlisting}
  5646534535677654567453241456765467863576465467345675856382436457687534678665425769888843222222222587
\end{lstlisting}

\end{document}

Of course, this may break regular numerals that you don't want to break as well. However, I'm not sure whether this might be an issue.

  • Thx, this does the trick! – user4811 Jan 9 '14 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.