1

Problem

I am trying to colour brackets in listings using the \lst@ProcessOther command. My code works perfectly fine when using the breaklines=false option. When using breaklines=true however, \lst@ProcessOther {"29} or \lst@ProcessOther {`)} stops recognising ) characters. How can I use the breaklines feature while still processing ) characters using \lst@ProcessOther?

Example showcasing the problem

\documentclass{letter}
\usepackage{listings}
\usepackage{color}

\makeatletter
\lst@CCPutMacro
    \lst@ProcessOther {"28}{% process "(" character
        \textcolor{red}{(}
    }
    \lst@ProcessOther {"29}{% process ")" character
    \textcolor{red}{)}
    } 
    \lst@ProcessOther {"5B}{% process "[" character
        \textcolor{green}{[}
    }
    \lst@ProcessOther {"5D}{% process "]" character
        \textcolor{green}{]}
    } 
    \lst@ProcessOther {"7B}{% process "{" character
        \textcolor{cyan}{\{}
    }
    \lst@ProcessOther {"7D}{% process "}" character
    \textcolor{cyan}{\}}
    } 
    \@empty\z@\@empty
\makeatother  

\lstdefinestyle{breakable}{breaklines=true}
\lstdefinestyle{unbreakable}{breaklines=false}
\lstset{frame=single,basicstyle=\ttfamily}

\begin{document}
\begin{lstlisting}[style=breakable,title={\ttfamily breaklines = true}]
([({([()])})])
\end{lstlisting}
\begin{lstlisting}[style=breakable,title={\ttfamily breaklines = true  without )}]
[{[{[{[]}]}]}]
\end{lstlisting}
\begin{lstlisting}[style=unbreakable,title={\ttfamily breaklines = false}]
([({([()])})])
\end{lstlisting}


\end{document}

Output output

2
  • Unrelated, but better not use the minimal class for examples. Commented Oct 10, 2024 at 11:55
  • My bad! Changed it to letter. Commented Oct 10, 2024 at 12:05

1 Answer 1

1

Well listings adds special processing instructions for ) to a hook and that breaks special processing of the char:

\lst@AddToHook{SelectCharTable}
    {\lst@ifbreaklines \lst@Def{`)}{\lst@breakProcessOther)}\fi}

You could remove that again and then your code works, but quite probably there is a reason for this and it will change line breaking so you should better report that to the maintainer.

\documentclass{article}
\usepackage{listings}
\usepackage{color}

\makeatletter
\lst@CCPutMacro
    \lst@ProcessOther {"28}{% process "(" character
        \textcolor{red}{(}
    }
    \lst@ProcessOther {"29}{% process ")" character
    \textcolor{red}{)}
    } 
    \lst@ProcessOther {"5B}{% process "[" character
        \textcolor{green}{[}
    }
    \lst@ProcessOther {"5D}{% process "]" character
        \textcolor{green}{]}
    } 
    \lst@ProcessOther {"7B}{% process "{" character
        \textcolor{cyan}{\{}
    }
    \lst@ProcessOther {"7D}{% process "}" character
    \textcolor{cyan}{\}}
    } 
    \@empty\z@\@empty

\usepackage{etoolbox}
\ExpandArgs{c}\patchcmd{\@lst hk@SelectCharTable}
   {\lst@ifbreaklines \lst@Def {`)}{\lst@breakProcessOther )}\fi}
   {}{}{\fail}

\makeatother  

\lstdefinestyle{breakable}{breaklines=true}
\lstdefinestyle{unbreakable}{breaklines=false}
\lstset{frame=single,basicstyle=\ttfamily,}


\begin{document}

\begin{lstlisting}[style=breakable,title={\ttfamily breaklines = true}]
([({([()])})])
\end{lstlisting}

\begin{lstlisting}[style=breakable,title={\ttfamily breaklines = true  without )}]
[{[{[{[]}]}]}]
\end{lstlisting}

\begin{lstlisting}[style=unbreakable,title={\ttfamily breaklines = false}]
([({([()])})])
\end{lstlisting}


\end{document}

enter image description here

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.