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

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 output

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

-
You can work with the option literate. An example is given here tex.stackexchange.com/questions/41720/…. Others are also available on tex stackexchange. – Marco Daniel Feb 18 '12 at 16:04
@PabloGonzalez The majority of your potential target audience may not know which package the \lstset command is defined in. You should provide a full minimal example. By providing it, you increase the chances that people are willing to have a look at your question and this increases the change that you will get an answer. – Marc van Dongen Feb 18 '12 at 16:11
@MarcoDaniel thanks for link ... – Pablo González L Feb 18 '12 at 19:04
@MarcvanDongen, Yes... Edit the post to clarify the idea – Pablo González L Feb 18 '12 at 19:06

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}


-
Thanks, but I do not put the slash (/) in red, and all the numbers in gray, how coloring these characters? – Pablo González L Feb 18 '12 at 21:40
For digits see: tex.stackexchange.com/questions/40284/… – Marco Daniel Feb 18 '12 at 22:01
Related to the / you can also work with literate – Marco Daniel Feb 18 '12 at 22:02
Many thanks for the comments and links, I solved the original problem, now where do I click to give the problem solved? – Pablo González L Feb 18 '12 at 23:39
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 1, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.888884961605072, "perplexity": 2093.4958640006803}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2016-07/segments/1454701146241.46/warc/CC-MAIN-20160205193906-00126-ip-10-236-182-209.ec2.internal.warc.gz"}