5

I want to change some special character's color in listing for example all Parenthesis be red. I don't want to make them keywords they should have different color. is it possible?

1 Answer 1

10

You could use the literate key; in the following example. parentheses are typeset in red:

\documentclass{article}
\usepackage{listings} 
\usepackage{xcolor}

\lstset{
    basicstyle=\small\ttfamily,
    columns=fullflexible,
    literate=*{(}{{\textcolor{red}{(}}}{1}
         {)}{{\textcolor{red}{)}}}{1},
    }
\begin{document}

\begin{lstlisting}
    {
      _SetCalibTableName("lorem",100);
      g_Met_strReferenceName1=Met_strReferenceName;
    }
\end{lstlisting}

\end{document}

enter image description here

5
  • 1
    Thanks. it works. just a question what is that number 1 in literate? it should be always 1? what happen if we change it?
    – Karo
    Jul 17, 2014 at 14:50
  • 1
    @Karo You're welcome. The last argument (the "1" in my example) specifies the width (in number of characters) that the replacement string (the colored parenthesis) will occupy in the output. Wider strings might require a larger value. Jul 17, 2014 at 14:56
  • @GonzaloMedina I never understood that. For instance {…}{...}1 will output the same as {…}{...}3.
    – Manuel
    Jul 17, 2014 at 14:58
  • 1
    @Manuel that is because fullflexible was used in my exmaple; try this \lstset{ basicstyle=\small\ttfamily, literate=*{(}{{\textcolor{red}{aa}}}{1} {)}{{\textcolor{red}{mmm}}}{1}, } and you'll see the difference. Jul 17, 2014 at 15:15
  • @GonzaloMedina Ah, true. I use fullflexible too, so it has no meaning there.
    – Manuel
    Jul 17, 2014 at 15:20

You must log in to answer this question.

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