# How to emphasize with in a listing an identifier containing a digit?

I have this LaTeX code that I want to typeset using listings:

\documentclass[paper=a4,fontsize=12pt,version=last]{scrlttr2}


I want to emphasize "scrlttr2" with a different format. How can it be done with listings?

-

You must set scrlttr2 as a keyword. The digit in the keyword need a special handling. You need otherkeyword

\listfiles
\documentclass{article}
\usepackage{color}
\usepackage{listings}
\lstset{language=[LaTeX]tex,
otherkeywords={scrlttr2},
morekeywords=[2]{scrlttr2},
keywordstyle=[2]{\color{red}\large}
}

\begin{document}
\begin{lstlisting}
\documentclass[paper=a4,fontsize=12pt,version=last]{scrlttr2}
\end{lstlisting}
\end{document}


-
Thanks a bunch, that did exactly what i needed –  agodemar Sep 1 '11 at 10:59

You can also use one escaping character. I use ⠶ because it is not used in listings.

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}

\begin{document}
\lstset{escapechar=\⠶}  % Any UTF-8 character can be used.
\lstset{language=Python}
\begin{lstlisting}
for i in range(5):
⠶\textcolor{red}{print}⠶(i)
print('Fin de la boucle...')
\end{lstlisting}

\lstset{escapechar={}} % If necessary, we can delete the special escaping character.
\begin{lstlisting}
for i in range(5):
⠶\textcolor{red}{print}⠶(i)
print('Fin de la boucle...')
\end{lstlisting}

\end{document}

-
Thanks, this is a smart way of escaping. –  agodemar Sep 1 '11 at 11:01
Very interesting to see using an unusual UTF-8 character for an escape char. –  Stefan Kottwitz Nov 3 '11 at 17:11

More generally: if you want to emphasize everything between pairs of braces (and accounting for nested braces), you can define them as a special type of "comment". You can do this for other "spans" of characters which you would like to be typeset differently, such as math-mode elements.

\documentclass{article}
\usepackage{color}
\usepackage{listings}
\lstset{
language=[LaTeX]tex,
morecomment=[n][\color{blue!50!black}\bfseries]{\{}{\}},
morecomment=[s][\color{green!50!black}\bfseries\slshape]{$}{$},
}

\begin{document}
\begin{lstlisting}
\documentclass[paper=a4,fontsize=12pt,version=last]{scrlttr2}
\begin{document}
Pythagoras' Theorem: $a^2 + b^2 = c^2$
\end{document}
\end{lstlisting}
\end{document}


Result:

The "interpretation" of these spans as somehow being "comments" is incidental; what matters is that it allows you to set the formatting.

-
Thanks for this very interesting solution. –  agodemar Sep 1 '11 at 11:00