9

How can I add another color at other keywords?

For example:

morekeywordsA={RequirePackage,ProvidesPackage}=\definecolor{keywordsA}{HTML}{8A4A0B}

morekeywordsB={{,},=}=\definecolor{keywordsB}{HTML}{othercolor}

\documentclass{scrreprt}

\usepackage{listings}

\usepackage[dvipsnames]{xcolor}

\definecolor{keywords}{HTML}{8A4A0B}

\definecolor{background}{HTML}{EEEEEE}

\definecolor{comments}{HTML}{868686}

\lstset{language=[LaTeX]Tex,
    keywordstyle=\color{keywords},
    basicstyle=\normalfont\ttfamily,
    commentstyle=\color{comments}\ttfamily,
    stringstyle=\rmfamily,
    numbers=left,
    numberstyle=\scriptsize,
    stepnumber=1,
    numbersep=8pt,
    showstringspaces=false,
    breaklines=true,
    frameround=ftff,
    frame=lines,
    morekeywords={RequirePackage,ProvidesPackage},
    backgroundcolor=\color{background}
}

\begin{document}

\begin{lstlisting}[firstnumber=1]
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{arsclassica}[2012/02/21 v4.0 Customizing ClassicThesis (LP)]
\RequirePackage{classicthesis}
\RequirePackage{caption}% Caption package
\end{lstlisting}

\end{document}
  • The colors are 8A4A0B (keywords), 145680 ({, }, =, …), 868686 (comments) and 953838 (lengths). – Qrrbrbirlbel Sep 19 '12 at 13:45
  • @Qrrbrbirlbel thanks! and the background? How can I set the lengths color and the ({, }, =, …) color with the listing package? (I have added the new color in the code) – Aurelius Sep 20 '12 at 0:07
  • Background is EEEEEE. To me it seems you need keywordstyle=[2]\color{braces}, (more)keywords=[2]{\{,\},[,],(,)} and alsoletter={\\,\{,\},[,],(,)}. But it doesn't work. If I add \\RequirePackages,\\ProvidesPackage to morekeywords the backslash gets colored, too. But braces don't get colored if they're not standing alone. And btw, you reset settings again (I find basicstyle=\normalfont\ttfamily three times). Maybe you should minimize your MWE for a better chance of answers. – Qrrbrbirlbel Sep 20 '12 at 0:37
  • @Qrrbrbirlbel I Have simplified the code ;) – Aurelius Sep 20 '12 at 17:10
  • @Qrrbrbirlbel And the color for the \def command color? used here: tex.stackexchange.com/questions/35080/…? Where do you take those colors? – Aurelius Sep 22 '12 at 22:17
9

Since you want to customize (La)TeX, it's a good idea to set texcsstyle to have the same color as the keywords and to use the star so that the backslash will also have the same color; regarding your issue, the braces are not really keywords; I set their style (and also for the square brackets) using literate (notice the * character in the first literate line to prevent color form appearing in comments and strings):

\documentclass{scrreprt}
\usepackage{listings}
\usepackage[dvipsnames]{xcolor}

\definecolor{keywords}{HTML}{8A4A0B}
\definecolor{background}{HTML}{EEEEEE}
\definecolor{comments}{HTML}{868686}
\definecolor{myblue}{RGB}{20,105,176}

\lstset{language=[LaTeX]Tex,
    keywordstyle=\color{keywords},
    texcsstyle=*\color{keywords},
    basicstyle=\normalfont\ttfamily,
    commentstyle=\color{comments}\ttfamily,
    stringstyle=\rmfamily,
    numbers=left,
    numberstyle=\scriptsize,
    stepnumber=1,
    numbersep=8pt,
    showstringspaces=false,
    breaklines=true,
    frameround=ftff,
    frame=lines,
    morekeywords={RequirePackage,ProvidesPackage,NeedsTeXFormat},
    backgroundcolor=\color{background},
    literate=
            *{\{}{{\textcolor{myblue}{\{}}}{1}
            {\}}{{\textcolor{myblue}{\}}}}{1}
            {[}{{\textcolor{myblue}{[}}}{1}
            {]}{{\textcolor{myblue}{]}}}{1},
}

\begin{document}

\begin{lstlisting}[firstnumber=1]
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{arsclassica}[2012/02/21 v4.0 Customizing ClassicThesis (LP)]
\RequirePackage{classicthesis}
\RequirePackage{caption}% Caption package
\end{lstlisting}

\end{document}

enter image description here

To answer your specific question, you can use [<number>] both in keywordstyle and in keywords, to define different keyword sets with their own style; a little example

\documentclass{scrreprt}
\usepackage{listings}
\usepackage[dvipsnames]{xcolor}

\definecolor{keywords}{HTML}{8A4A0B}

\definecolor{background}{HTML}{EEEEEE}

\lstset{language=[LaTeX]Tex,
    keywordstyle=\color{keywords},
    keywordstyle=[2]\color{red},
    keywordstyle=[3]\color{orange},
    basicstyle=\normalfont\ttfamily,
    showstringspaces=false,
    frameround=ftff,
    frame=lines,
    morekeywords={RequirePackage,ProvidesPackage},
    keywords=[2]{Some,Other,Keywords},
    keywords=[3]{and,another,test},
    backgroundcolor=\color{background}
}

\begin{document}

\begin{lstlisting}[firstnumber=1]
\NeedsTeXFormat{LaTeX2e}
Some Other Keywords
and another test
\end{lstlisting}

\end{document}

enter image description here

To apply color to lengths, one can escape to LaTeX (this however won't work for in-line listings):

\documentclass{scrreprt}
\usepackage{listings}
\usepackage[dvipsnames]{xcolor}

\definecolor{keywords}{HTML}{8A4A0B}
\definecolor{background}{HTML}{EEEEEE}
\definecolor{comments}{HTML}{868686}
\definecolor{lengthcolor}{RGB}{200,40,150}
\definecolor{myblue}{RGB}{20,105,176}

\lstset{language=[LaTeX]Tex,
    keywordstyle=\color{keywords},
    texcsstyle=*\color{keywords},
    basicstyle=\normalfont\ttfamily,
    commentstyle=\color{comments}\ttfamily,
    stringstyle=\rmfamily,
    numbers=left,
    numberstyle=\scriptsize,
    stepnumber=1,
    numbersep=8pt,
    showstringspaces=false,
    breaklines=true,
    frameround=ftff,
    frame=lines,
    morekeywords={RequirePackage,ProvidesPackage,NeedsTeXFormat,setlength},
    backgroundcolor=\color{background},
    literate=
            *{\{}{{{\color{myblue}{\{}}}}{1}
            {\}}{{{\color{myblue}{\}}}}}{1}
            {[}{{{\color{myblue}{[}}}}{1}
            {]}{{{\color{myblue}{]}}}}{1},
   escapeinside=!!
}

\newcommand\lencolor[1]{\textcolor{lengthcolor}{\texttt{#1}}}

\begin{document}

\begin{lstlisting}[firstnumber=1]
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{arsclassica}[2012/02/21 v4.0 Customizing ClassicThesis (LP)]
\RequirePackage{classicthesis}
\RequirePackage{caption}% Caption package
\setlength\parskip{!\lencolor{18pt}!}
\hspace*{!\lencolor{15cm}!}
\end{lstlisting}

\end{document}

enter image description here

  • Thanks ! And is also possible set a color for the lengths? like: 8pt – Aurelius Sep 22 '12 at 22:20
  • 1
    @FormlessCloud I guess so; right now I am busy, but as soon as I can I'll let you know. – Gonzalo Medina Sep 22 '12 at 22:24
  • 1
    Thanks! you are too kind ! also, another thing, if I add this codes at your answer i get colored only the left brace {)}{{\textcolor{myblue}{)}}}{1} {(}{{\textcolor{myblue}{(}}}{1} – Aurelius Sep 22 '12 at 22:41
  • @FormlessCloud I found a solution using escaping to LaTeX; perhaps there's another simpler way of doing this, so I think it's better for you to open a fresh new question with the request about adding color to lengths. In this way you might get more alternatives. – Gonzalo Medina Sep 23 '12 at 18:00
  • @FormlessCloud regarding the other issue (the one with literate), it seems to be a bug; there's some problem with literate and breaklines=true. I opened this question tex.stackexchange.com/q/73795/3954. – Gonzalo Medina Sep 23 '12 at 18:24

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.