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 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?

share|improve this question
add comment

3 Answers 3

up vote 6 down vote accepted

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}

enter image description here

share|improve this answer
    
Thanks a bunch, that did exactly what i needed –  agodemar Sep 1 '11 at 10:59
add comment

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}
share|improve this answer
1  
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
add comment

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:

Hilighted source code

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

share|improve this answer
    
Thanks for this very interesting solution. –  agodemar Sep 1 '11 at 11:00
add comment

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.