# How to make my keywords still get highlighted no matter the breaklines is set to true?

I got a bit of trouble with the listings package. I am trying to highlight a custom language with lstdefinelanguage but for reasons I can't understand, highlighting for my ) keyword, and only the ) keyword, breaks as soon as I enable breaklines in lstset.

I'd expect the keyword ) to get highlighted just like (, +and so on even if breaklines is set to true.

Here's a minimal example to reproduce the issue:

    \documentclass[10pt, leqno]{article}
\usepackage{listings}
\usepackage[usenames,dvipsnames]{color}

\lstdefinelanguage{Test}
{
morekeywords = {), +, (},
alsoletter={), +, (},
}

\lstset{
keywordstyle=\color{red},
breaklines=true % As soon as this is true highlighting for ) breaks
}

\begin{document}
\begin{lstlisting}[language=Test]
)
+
(
A very long line that will get broken A very long line that will get broken A very long line that will get broken A very long line that will get broken
\end{lstlisting}
\end{document}


Is it me doing something wrong or is this a problem with the package?

-
What is your expectation? –  xport Jun 1 '11 at 17:49
That the keyword ) gets highlighted just like (, + and so on no matter what breaklines is set to. –  Stefan H. Jun 1 '11 at 23:31
–  Marco Daniel Jun 6 '11 at 16:58

Here are two solutions. The first uses the beta package version 2 of listings and works out of the box. The second patches listings to avoid the problem. Both use Marco's suggestion of using literate.

Solution with listings2

\documentclass{article}
\usepackage{listings2}
\usepackage{xcolor}

\lstset{
literate={)}{{{\color{red})}}}1 {(}{{{\color{red}(}}}1 {+}{{{\color{red}+}}}1,
keywordstyle=\color{red},
breaklines=true
}

\begin{document}
\begin{lstlisting}
)
+
(
A very long line that (will) get broken A very long line that will get broken A very long line that will get broken A very long line that will get broken
\end{lstlisting}
\end{document}


Notice however that there are a few syntaxic changes between listings and listings2, so you will have to adapt parts of your code.

Solution by patching listings

By removing \lst@ifbreaklines \lst@Def{)}{\lst@breakProcessOther)}\fi from listings's SelectCharTable hook, the problem disappears (it should not have any side effect):

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{xstring}

\makeatletter
\noexpandarg % to not expand arguments of \StrSubstitute
\expandafter\StrSubstitute\expandafter{\lsthk@SelectCharTable}{\lst@ifbreaklines \lst@Def{)}{\lst@breakProcessOther)}\fi}{}[\lsthk@SelectCharTable]
\makeatother

\lstset{
literate={)}{{{\color{red})}}}1 {(}{{{\color{red}(}}}1 {+}{{{\color{red}+}}}1,
keywordstyle=\color{red},
breaklines=true
}

\begin{document}
\begin{lstlisting}
)
+
(
A very long line that (will) get broken A very long line that will get broken A very long line that will get broken A very long line that will get broken
\end{lstlisting}
\end{document}


I've used xstring to do the substitution.

-
I am a bit reluctant to use the listings2 package since it doesn't seem like there's much documentation out there. I'd hate to have to stab in the dark because of (maybe subtle) differences between it and the listings package. Also it seems like this package is not part of the standard LaTex distributions. –  Stefan H. Jun 10 '11 at 0:02