5

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

3
  • 3
    You can work with the option literate. An example is given here tex.stackexchange.com/questions/41720/…. Others are also available on tex stackexchange. Commented Feb 18, 2012 at 16:04
  • 3
    @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.
    – user10274
    Commented Feb 18, 2012 at 16:11
  • @MarcvanDongen, Yes... Edit the post to clarify the idea Commented Feb 18, 2012 at 19:06

1 Answer 1

5

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

4
  • Thanks, but I do not put the slash (/) in red, and all the numbers in gray, how coloring these characters? Commented Feb 18, 2012 at 21:40
  • For digits see: tex.stackexchange.com/questions/40284/… Commented Feb 18, 2012 at 22:01
  • Related to the / you can also work with literate Commented Feb 18, 2012 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? Commented Feb 18, 2012 at 23:39

You must log in to answer this question.

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