2

I defined my own programming language using listings and, to do so, I had to use the literate option a lot in my definition. For example, to make parentheses bold and green, I did this:

\definecolor{darkgreen}{rgb}{0, 0.65, 0.2}
\lstdefinelanguage{lang}{
  comment=[l]{\#},
  literate=
    {(}{{{\textbf{\color{darkgreen}(}}}}1
    {)}{{{\textbf{\color{darkgreen})}}}}1
}

It works fine. However, I want the replacements defined in the literate option not to be applied inside comments. For example:

(a b c)   # These parentheses should be green
# (a b c) # These shouldn't

Is this possible at all?

Romário
  • 139

2 Answers2

1

This is not a proper fix, but you can work around the problem in specific cases by escaping the listings environment.

\lstset{
  escapeinside={(*@}{@*)},
}

% ...

\begin{lstlisting}[language=lang]
    (a b c)
    # (*@{\color{black} (}@*)a b c(*@{\color{black} )}@*)
\end{lstlisting}

(You might need to change "black" to the same color you use for comments.)

Calvin
  • 111
0

Adding star character should help. Ignoring both comments and strings.

\definecolor{darkgreen}{rgb}{0, 0.65, 0.2}
\lstdefinelanguage{lang}{
    comment=[l]{\#},
    literate=
    *{(}{{{\textbf{\color{darkgreen}(}}}}1
    {)}{{{\textbf{\color{darkgreen})}}}}1
}

Found here: How to apply the star (*) to only a few of the literates in listings environment?