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 would like to highlight some words in a lstlisting. The language is HTML. However, \emph seems not to be working with HTML. Any suggestions how I could work around this problem?

Right now I have:

\lstdefinestyle{htmlCode} {
language=html,
basicstyle=\scriptsize\ttfamily,
keywordstyle=\bfseries\ttfamily,
commentstyle=\color{gray}\ttfamily,
emph={time},
emphstyle=\color{green}
}

\begin{lstlisting}[style=htmlCode,caption={Html code}] 
<b>Time: $time$</b>
\end{lstlisting}
share|improve this question
    
Would you be able to post what you currently have, or at least a minimal working example (MWE)? –  Werner Aug 23 '11 at 21:23
    
Sure, I will... –  nrainer Aug 23 '11 at 21:47
    
Ok, I have added sample code. –  nrainer Aug 23 '11 at 21:54
    
Thanks, I've provided one solution. :) –  Werner Aug 23 '11 at 21:58
    
@Werner: Thank you. –  nrainer Aug 23 '11 at 22:08

1 Answer 1

up vote 3 down vote accepted

You could use the escapechar=<char> option for listings in the following way:

\documentclass{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{listings}
\begin{document}
 \lstdefinestyle{htmlCode} {
   language=html,
   basicstyle=\scriptsize\ttfamily,
   keywordstyle=\bfseries\ttfamily,
   commentstyle=\color{gray}\ttfamily,
   escapechar=| % Escape to LaTeX between |...|
}

 \begin{lstlisting}[style=htmlCode,caption={Html code}] 
<b>Time: $|\color{green}time|$</b>
\end{lstlisting}
\end{document}

Listings with escape character

The reason for this working may involve the fact that listings allows escaping to LaTeX when using the mathescape=true flag. This escapes around $...$, which is exactly around your time "keyword".

share|improve this answer
    
Great, that's working. –  nrainer Aug 23 '11 at 22:06
    
I actually also want to capture the $ sign (unlike in the example). Could I use something like morecomment=[b]{\$} to format all sequences between the $ character? –  nrainer Aug 23 '11 at 22:07
    
@nrainer: You could use |\color{green}\$time\$|. –  Werner Aug 23 '11 at 22:16

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.