6

I want to get colorful prebreak and postbreak tokens with listings package, but the following code doesn't work:

\documentclass{article}
\usepackage{xcolor,listings}
\begin{document}
\begin{lstlisting}[basicstyle=\ttfamily,breaklines,prebreak={\space\color{teal}!},postbreak={\color{blue}!\space}]
This is a long line. This is a long line. This is a long line. This is a long line. This is a long line.
This is another long line. This is another long line. This is another long line. This is another long line.
\end{lstlisting}
\end{document}

How could I resolve this problem?

Z.H.
  • 3,346
  • 21
  • 39

1 Answers1

6

enter image description here

\documentclass{article}
\usepackage{xcolor,listings}
\begin{document}
\begin{lstlisting}[basicstyle=\ttfamily,breaklines,prebreak={\space\hbox{\textcolor{teal}{!}}},postbreak={\hbox{\textcolor{blue}{!}}\space}]
This is a long line. This is a long line. This is a long line. This is a long line. This is a long line.
This is another long line. This is another long line. This is another long line. This is another long line.
\end{lstlisting}
\end{document}
David Carlisle
  • 757,742
  • Why is there a need to wrap the \textcolor in a \hbox, here? Why is this error occurring? – jub0bs Apr 19 '15 at 09:57
  • 3
    @Jubobs because if you don't You get an error message from tex telling you to do it:-) Remove the \hbox and you get ! Improper discretionary list. which is a primitive error in a \discretionary texbook says: more precisely, the horizontal list formed by each discretionary general text must consist only of characters, ligatures, kerns, boxes, and rules; there should be no glue or penalty items, etc – David Carlisle Apr 19 '15 at 10:05