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}
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}
.
Best Answer
I used
morekeywords
option instead ofotherkeywords
and you probably was loading the default predifined language by specifying\begin{lstlisting}[style=Python]
, as a first step you should define your listings language style (customize) bylstdefinestyle
and then use it in thelstlisting
environment as a stylestyle=myPython
.For the second part i am not sure but i am afraid there is no other way to add keywords automatically rather than adding them by collecting, the list of the library keywords and pasting it into
morekeywords = {keyword1,...}
.