8

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?

3

1 Answer 1

0

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

image of the result of the code

\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):

result of the code

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

1
  • 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, 2011 at 0:02

You must log in to answer this question.

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