tags:

views:

170

answers:

1

I'm using Latex and the listings package to display some C++ code (with syntax highlighting) inside a document.

I need to highlight some parts of the code. This specific highlight has nothing to do with syntax highlighting, it's just some parts of the code I'd like to highlight so that the reader can focus on it.

I wish I could make some variable name, for instance, displaying in bold, and on a yellow background. Here is something I did with MSWord I'd like to reproduce with Latex (of course, not the red and green underlining):

.

I haven't found a way to do it with the listings package. Is it possible?

+3  A: 

You can enable arbitrary LaTeX commands inside your listings region:

\begin{listings}[escapeinside=\{\}]
{\highlight{Colonnes[3] = 9}}
\end{listings}

\highlight is your highlighting macro (you need to write it yourself). See the listings documentation, section 4.14 (“Escaping to LaTeX”) for further details. Notice that you now need to escape every other occurrence of the special characters in your code – so for C++ code, {} is probably a bad choice.

Konrad Rudolph
Thanks for the hint ! I couldn't make it work using `escapeinside` (don't know why, got error I didn't understand), but made it work with `escapechar=\%`.For the record, I defined the `\highlight` macro this way :\newcommand\highlight[1]{\colorbox{yellow}{#1}}using the package `xcolor`.
Jérôme