[Tex/LaTex] Color \ , [], {} and / in listings environment

codecolorlistings

I tried to color a source code listing as follows.
I would like the \ to be the same color as the word that follows for example:

\documentclass in red including "\"
\usepackage in blue including "\"

I would like brackets [ ] in green, but black text between them and { } and / in red, but the text within them in black and numbers in gray.

The file:

\documentclass[10pt]{article}
\usepackage{bera}
\usepackage{xcolor}
\usepackage{listings}
\lstset{basicstyle=\footnotesize\ttfamily,columns=flexible,frame=single,framerule=0pt,%
    backgroundcolor=\color{gray!20},%
    xleftmargin=\fboxsep,%
    xrightmargin=\fboxsep,
    language=[LaTeX]TeX,%
    keywordstyle=\color{blue},%
        texcsstyle=*\color{red}\bfseries,%
        texcs={end,begin,documentclass},%
        mathescape=false,escapechar=|,%
        literate={<B>}{\textcolor{blue}{\string\usepackage}}1
             {<P>}{\textcolor{blue}{\string\graphicspath}}1
                 {<I>}{\textcolor{green}{[}}1
             {<D>}{\textcolor{green}{]}\normalcolor}1
                     {<$>}{\textcolor{red}{\{}\color{black}}1
                     {<$$>}{\textcolor{red}{\}}\normalcolor}1
                         }
\pagestyle{empty}
\begin{document}
\begin{lstlisting}
\documentclass<I>12pt<D><$>article<$$>
<B><$>xcolor<$$>
<B><$>listings<$$>
<B><I>options<D><$>graphicx<$$>
<P><$><$>img/pdf<$$><$$>
\begin<$>document<$$>
Text
\end<$>document<$$>
\end{lstlisting}
\end{document}

I should add (if possible), my idea is to get a text colored similarly to Texmaker.
The outputenter image description here

I only need color / and numbers.
One more query, do I add a literate for every word you want in blue?
Thanks

Best Answer

In relation to my comment. You can use the option literate only for the brackets.

\documentclass[10pt]{article}
\usepackage{bera}
\usepackage{xcolor}
\usepackage{listings}
\lstset{basicstyle=\footnotesize\ttfamily,columns=flexible,frame=single,framerule=0pt,%
    backgroundcolor=\color{gray!20},%
    xleftmargin=\fboxsep,%
    xrightmargin=\fboxsep,
    language=[LaTeX]TeX,%
    keywordstyle=\color{blue},%
        texcsstyle=*\color{red}\bfseries,%
        texcs={end,begin,documentclass,graphicspath},%
        mathescape=false,escapechar=|,%
        literate={<B>}{\textcolor{blue}{\string\usepackage}}1
                           {\{ }{\textcolor{red}{\{}}1
                           {\}}{\textcolor{red}{\}}}1
                           {[}{\textcolor{green}{[}}1     
                           {]}{\textcolor{green}{]}}1     
         }
\pagestyle{empty}
\begin{document}
\begin{lstlisting}
\documentclass[12pt]{article}
<B>{xcolor}
<B>{listings}
<B>[options]{graphicx}
\graphicspath{{img/pdf}}
\begin{document}
Text
\end{document}
\end{lstlisting}
\end{document}

enter image description here

Related Question