TransWikia.com

Do not apply literate option in comments

TeX - LaTeX Asked by Romário on September 2, 2021

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?

2 Answers

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

Answered by Calvin on September 2, 2021

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?

Answered by Jan Holčák on September 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP