9

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?

3 Answers 3

9

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.

5
  • 2
    Thanks, but I really want to emphasize baz in this particular context only. Commented Apr 24, 2010 at 10:27
  • 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. Commented Apr 24, 2010 at 10:33
  • 1
    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 :( Commented Apr 24, 2010 at 10:38
  • 1
    Try \bfseries instead of \textbf. Commented Apr 24, 2010 at 11:24
  • A problem I encountered with both this solution and the one provided by @velop below, is that they don't work when the code language is determined (either at \lstset or as attribute after \begin{lstlisting}). Is there a reason and, more importantly, a solution to this?
    – Jopie
    Commented Nov 23, 2018 at 9:56
7

Your solution is correct. However, the standard LaTeX monospaced font does not have a boldface version, so \textbf doesn’t do anything within the lstlisting environment. According to macfreek.nl, you can do the following to redeclare the boldface version:

\DeclareFontShape{OT1}{cmtt}{bx}{n}{<5><6><7><8><9><10><10.95><12><14.4><17.28><20.74><24.88>cmttb10}{}

I’ve tested the following example and it works as you desire:

\documentclass{article}
\usepackage{listings}
\DeclareFontShape{OT1}{cmtt}{bx}{n}{<5><6><7><8><9><10><10.95><12><14.4><17.28><20.74><24.88>cmttb10}{}
\lstset{language=C, basicstyle=\ttfamily}
\begin{document}

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

\end{document}

I use the same technique with \color{red} instead of \textbf to hint the audience of a beamer presentation at the difference between two almost identical code listings.

5

I found an answer under http://www.mrunix.de/forums/archive/index.php/t-42976.html

\lstset{moredelim=[is][\bfseries]{[*}{*]}}
\begin{lstlisting}
bla bla bla
blupp
[*wichtiger hervorzuhebender text*]
weiter im text
\end{lstlisting}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.