# 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.

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}


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 are virtually indistinguishable from Computer Modern, while having access to a monospaced font that's more compact than 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 font family 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.

• 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.

• Update Jan 2019: In the meantime, i.e., since I wrote the original version of this answer back in 2011, the txfonts and pxfonts font packages have been superseded (if not entirely replaced) by newtxtext/newtxmath on the one hand and newpxtext/newpxmath on the other. By all means, if you've used the txfonts and/or pxfonts packages in the path, do consider switching to either newtxtext/newtxmath or newpxtext/newpxmath. What hasn't changed is that newtxtext and newpxtext share the same monospaced font -- both medium ("normal") and bold weight.

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:

• Why the downvote? I wrote this answer nearly 7 years ago. If some fonts have changed since, kindly inform me what's no longer up-to-date about this answer, and I'll gladly update it. Thanks. – Mico Apr 12 '18 at 5:24
• This does not cover italics with monospace, only bold. (I am not the one who down-voted this answer) – Mikaël Mayer Jul 30 '18 at 16:44
• @MikaëlMayer - Thanks for this. I still would prefer for whoever saw fit to downvote this answer to provide some usable feedback regarding what irked him/her so much as to create the urge to downvote. – Mico Jul 30 '18 at 18:25

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 ... – user2478 Nov 27 '11 at 21:38

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.