Rather use keywords={smooth}
instead of otherkeywords
. According to the listings
documentation, the latter
defines keywords that contain other characters, or start with digits.
Here's an updated MWE showing the desired output:
\documentclass[12pt]{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\usepackage[usenames,dvipsnames]{color}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{lightgray}{rgb}{0.9,0.9,0.9}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\lstset{numbers=left,
numbersep=5pt,
basicstyle=\sffamily\tiny,
numberstyle=\tiny\color{gray},
keywordstyle=\color{blue},
commentstyle=\color{dkgreen},
stringstyle=\color{mauve},
backgroundcolor=\color{lightgray},
frame=single,
columns=fullflexible
}
\begin{document}
\title{}
\author{}
\maketitle
\section{somesection}
\lstset{language=Python, caption=somecaption, keywords={smooth}}
\begin{figure}[h]
\begin{lstlisting}
smooth is welcome
smoothed is not
\end{lstlisting}
\end{figure}
\end{document}
The above MWE specifies the keyword
s, but subsequently also overwrites the existing keywords supported by the language
setting. To maintain the existing set and add more/remove some, use morekeywords={smooth}, deletekeywords={is,not}
.
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}
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}
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}
Best Answer
I know that the question is nearly three years old now, but I am dealing with the same problem at the moment. I have searched the internet and the same or closley related questions have been asked a lot of times without 100% satisfying answers to me because I am looking for a solution like the one that was desired here. As I have found one which is not perfect, but quite good, I want to share it in case someone else comes across here. But generally I would agree with @Jubobs, it's really a pain in the neck doing this in listings.
So basically what I did is I colored everything with the color I want to have for the numbers using the basicstyle attribute. Then I colored the keywords, comments and strings as usal using the keywordstyle, commentsytle and stringstlye attributes. Now the trick is to use the identifierstyle to color all variable names in the actual basic color. What is left concerning the basic style then is all digits as well as all operators, brakets and so on like ( + - * ... After that you obviously only have to color the operators, and breakets and so on with the basic color to get your result. This can be done with the literate attribute.
I will add a few lines of code from my Julia style that I am creating right now to illustrate what I mean. Notice that in my case I wanted to color the numbers with the same color as the strings, but you can obiously simply create a new one for that job.
Ok, I guess this is what you consider a MWE:
The result looks like:
Note that with the approach you dont even need to escape the 2 in in_identifier2.