3

Why square bracket does not appear in green in the second example ?

\documentclass[11pt, oneside]{article}  

\usepackage{listings}
\usepackage{xcolor}
\definecolor{darkgreen}{rgb}{0,0.6,0}
\newcommand{\CodeSymbol}[1]{\textcolor{darkgreen}{#1}}

\lstset{
  language=[LaTeX]{TeX},
  }

 \lstdefinestyle{myLangStyleA}{
    literate={\{}{{\CodeSymbol{\{}}}1
}

\lstdefinestyle{myLangStyleB}{
    literate={\{}{{\CodeSymbol{\{}}}1
          {\[}{{\CodeSymbol{\[}}}1, 
}

\begin{document}

\begin{lstlisting}[style=myLangStyleA]
[ {
\end{lstlisting}

\begin{lstlisting}[style=myLangStyleB]
[ {
\end{lstlisting}

\end{document}  

Which produces this result :

Green square bracket does not appear

Thanks !

3

The bracket [ is not a special character, so you should simply use it as is.

\documentclass[11pt, oneside]{article}  

\usepackage{listings}
\usepackage{xcolor}
\definecolor{darkgreen}{rgb}{0,0.6,0}
\newcommand{\CodeSymbol}[1]{\textcolor{darkgreen}{#1}}

\lstset{
  language=[LaTeX]{TeX},
}

\lstdefinestyle{myLangStyleA}{
  literate={\{}{{\CodeSymbol{\{}}}1
}

\lstdefinestyle{myLangStyleB}{
  literate={\{}{{\CodeSymbol{\{}}}1
           {[}{{\CodeSymbol{[}}}1, 
}

\begin{document}

\begin{lstlisting}[style=myLangStyleA]
[ {
\end{lstlisting}

\begin{lstlisting}[style=myLangStyleB]
[ {
\end{lstlisting}

\end{document}  

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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