tags:

views:

134

answers:

1

How can I set certain parts of an lstlisting in boldface?

\begin{lstlisting}[escapechar=@]
fun(foo, bar, @\textbf{baz}@ );
\end{lstlisting}

This compiles, but baz isn't set in boldface :( What am I missing?

+1  A: 

Your basic approach should be to tell listing what words to emphasise. A straightforward way to do this is like this:

\lstset{emph={baz},emphstyle=\textbf}

Of course, that will emphasise baz whenever it occurs. But this is a better approach than trying to put the markup into your listing itself, you don't (ought to) want to do that since you (ought to) want to use listings to format an unmodified piece of code. Especially so if you are including source files rather than typing code snippets.

Once you've got the basics under your belt look at the documentation, to learn how to set styles for keywords and for identifiers, and how to modify the inbuilt list(s) of keywords for your languages of choice.

High Performance Mark
Thanks, but I really want to emphasize baz in this particular context only.
FredOverflow
Well, just make that \lstset call within the scope you want it, reset outside that scope. I wouldn't go as far as to say that you can't set arbitrary text in arbitrary formats in listing, but you will always be fighting against the package if you do.
High Performance Mark
By the way, `emphstyle=\textbf` does not work for me, whereas for example `emphstyle=\color{red}` does. It seems lstlsting really does not want me to mess with the font faces :(
FredOverflow
Try `\bfseries` instead of `\textbf`.
High Performance Mark