Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
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
    
add comment

1 Answer

up vote 0 down vote accepted

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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