Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
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. –  Marco Daniel Feb 18 '12 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. –  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

1 Answer 1

up vote 5 down vote accepted

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

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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