# Do not apply literate option in comments

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?

• Can you show a minimal example? – egreg Apr 25 '16 at 15:10
• @egreg I edited the question and added more detail – Romário Apr 25 '16 at 15:21

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