# Using bold/italic text inside listings?

How can I use bold text inside a code listing? I wanted to make some parts of the code bold.

-
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Jubobs Mar 1 '14 at 18:28

As per Mico's answer, there is no boldfaced monospaced font in the Computer Modern font family, so you need to use a font that has bold monospaced font. Below is an example using listings that make the keywords bold using the pxffonts. Here is a comparison of the results without and with the \usepackage{pxfonts}:

\documentclass[border=2pt]{standalone}
\usepackage{listings}
\usepackage{pxfonts}

\lstset{language=C,
basicstyle=\ttfamily,
keywordstyle=\bfseries,
showstringspaces=false,
morekeywords={include, printf}
}

\begin{document}
\begin{lstlisting}
/* Prints Hello World */

#include <stdio.h>

int main(void) {
printf("Hello World!");
return 0;
}
\end{lstlisting}
\end{document}


## Alternate Solution:

You could also use the the courier font form ttfamily with bfseries or how to enable bold in fixed width font:

\documentclass[border=2pt]{standalone}
\usepackage{listings}

\lstset{language=C,
basicstyle=\ttfamily,
keywordstyle=\bfseries,
showstringspaces=false,
morekeywords={include, printf}
}

\begin{document}
\begin{lstlisting}
/* Prints Hello World */

#include <stdio.h>

int main(void) {
printf("Hello World!");
return 0;
}
\end{lstlisting}

\hrule
\renewcommand{\ttdefault}{pcr}
\begin{lstlisting}
/* Prints Hello World */

#include <stdio.h>

int main(void) {
printf("Hello World!");
return 0;
}
\end{lstlisting}
\end{document}

-

you need a typewriter font which supports bold characters, eg:

\usepackage[scale=0.9]{beramono}


and inside the listing you can use the escape character. You'l lfind some examples in the documentation, run texdoc listings

-
Or you could \usepackage{bold-extra} if you want a bold version of cmtt. –  kahen Nov 27 '11 at 21:28
no, I don't want the cmtt font ... –  Herbert Nov 27 '11 at 21:38

As you must have already discovered, there is no boldfaced monospaced font in the Computer Modern font family. (Computer Modern is what's loaded by default.) To get the option of highlighting part of your code in a boldfaced font, you must switch to a different font family.

• One way to do this is to add the command \usepackage[scaled=1.04]{couriers} to the preamble of your document; doing so will tell (La)TeX to use the Courier monospaced font instead of the Computer-Modern (CM) variety. The ordinary and boldfaced versions of Courier look very different from CM mono, which is presumably what you want. The downside of using Courier is that it's set very loosely, i.e., you can't squeeze as many words into a line as you could with CM mono.
• A fairly unobtrusive option -- in the sense that the main text and math fonts look virtually indistinguishable from Computer Modern, while having a monospaced font that's not as loose as Courier -- would be to use the Latin Modern font family, loaded with the command \usepackage lmodern. While this font family does have a boldface-monospaced font, it is not all that bold(faced) and thus may not meet your needs.
• Assuming you're willing to depart from the Computer/Latin Modern fonts entirely, some good options for font families could be selected with either one of the following two commands: \usepackage{pxfonts} (a Palatino font) or \usepackage{txfonts} (a Times Roman font). Note that the (variable-spaced) text fonts will look very different, but the creator of these two font families gave them each the same style of monospaced text. By the way, the mathptmx package, which gives you still another option for Times New Roman, does not feature a boldfaced monospaced font and hence won't do the job for you.)

The following MWE serves to illustrate the effects of choosing among these options:

\documentclass{article}
%% Uncomment one (and only one) of the following four \usepackage commands:
%% \usepackage[scaled=1.04]{couriers}
\usepackage{lmodern}
%%\usepackage{pxfonts}  % Palatino font
%%\usepackage{txfonts}  % Times font

\begin{document}
%Monospaced font:
\texttt{The quick brown fox jumps over the lazy dog.}

%Monospaced font with some boldfaced words:
\texttt{The quick brown \textbf{fox} jumps over the \textbf{lazy} dog.}
\end{document}


Courier mono generates this text:

Latin Modern mono looks like this (note the rather minor difference between non-bold and bold):

Both the pxfonts and txfonts samples look like this:

-

The listings package might suffice.

\usepackage{listings}

...

\lstset{morekeywords={example,foo,bar}}

...

\begin[language=C]{lstlisting}
YOUR_CODE_HERE
\end{lstlisting}


The package allows you to define your own language definition (\lstdefinelanguage), and you get some fine-grained control about how the language is presented, so you get some re-usability of your setup (i.e., preferable over manually setting your keywords bold).

-
Does the listings package provide the option of bold-facing (if desired) complete sentences at a time? Also, can you control when words such as "example", "foo", and "bar" should be typeset in boldface-mono and when they should be printed in the normal mono font? –  Mico Sep 6 '11 at 14:43
Beyond my knowledge of the package. Since I do not know what exactly your requirements are (and would have to look up the package documentation myself), I'd recommend you sift through the docs at the link above. –  DevSolar Sep 6 '11 at 16:21
I am actually not the person who originally posted the question, and hence I don't either know what his/her specific needs (in terms of bold-facing some words) might be. It just struck me that the morekeywords listing facility might not be flexible enough to meet his/her needs. –  Mico Sep 6 '11 at 18:14
Chapter 4.14 Escaping to LaTeX of the listings package documentation provides several ways to embed LaTeX commands in your listing. –  DevSolar Sep 7 '11 at 14:34

I can't remember where I came across the following definition to use a boldface typewriter font with Computer Modern:

% Declare bold typewriter font with Computer Modern
\DeclareFontShape{OT1}{cmtt}{bx}{n}{<5><6><7><8><9><10><10.95><12><14.4><17.28><20.74><24.88>cmttb10}{}


Then:

\usepackage{listings}

I'm not an expert in fonts in LaTeX so I'm not entirely sure what the DeclareFontShape command does behind the scenes but I like the result.