How can I use bold text inside a code listing? I wanted to make some parts of the code bold.
-
4I found this answer from another question more satisfacting than current answers because it does not define new keywords. – Gabriel Devillers Jan 05 '18 at 13:40
6 Answers
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 pxfonts. 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}
- 103
- 4
- 223,288
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
mathptmxpackage, 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
txfontsandpxfontsfont packages have been superseded (if not entirely replaced) bynewtxtext/newtxmathon the one hand andnewpxtext/newpxmathon the other. By all means, if you've used thetxfontsand/orpxfontspackages in the path, do consider switching to eithernewtxtext/newtxmathornewpxtext/newpxmath. What hasn't changed is thatnewtxtextandnewpxtextshare 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:

- 506,678
-
1Why 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 05: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
-
Actually, this works only for
\usepackage{lmodern}and\usepackage{pxfonts}. I tired\usepackage{txfonts}but it didn't work for me unfortunately. So the problem is: do we have to change the font of whole paper for just making one word bold? My research paper theme comes with its own font. So please help in this case – foobar Jun 28 '20 at 12:59 -
1@MuhammadAshfaq - I'm afraid I don't understand the claim you make, viz., that loading the
txfontspackage "didn't work" for you. For sure, when I reran the code of my answer (which I wrote nearly seven years ago...), I get the exact same output for both thetxtfontsand thepxfontspackages. Please clarify what it is that's not working for you. Please also state which TeX distribution you employ. (At the moment, I mostly use TeXLive2020.) – Mico Jun 28 '20 at 19:04 -
-
1@Mico i figured out the problem. Actually my article template was messing around with your code. By default, it was:
\documentclass[times]{elsarticle}, when I removedtimes, it worked fine as you suggested. Thanks. This comment may be helpful for those who may face similar issue – foobar Jun 30 '20 at 15:32 -
@MuhammadAshfaq - Good to know that you figured out the source of the discrepancy. – Mico Jun 30 '20 at 15:33
-
1@MuhammadAshfaq - No worries. But nevertheless many thanks for your expression of concern. – Mico Jun 30 '20 at 15:35
You can use lstlisting with mathescape set to true, and then use \textbf inside maths mode.
\begin{lstlisting}[mathescape=true]
$\textbf{Hello}$
Hello
\end{lstlisting}
Note that the font does change a bit, and you would have to do this for every word you want to make bold. But it is an alternative, and it also works for italics and other text commands you might want to use.
- 71
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
-
1Or you could
\usepackage{bold-extra}if you want a bold version ofcmtt. – kahen Nov 27 '11 at 21:28 -
-
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}
\lstloadlanguages{Matlab}
\lstset{
basicstyle=\scriptsize\upshape\ttfamily,
keywordstyle=\color{blue}\bfseries,
% etc.
}
Result:

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.
- 1,369
The listings package might suffice.
Start with:
\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).
- 7,857
-
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 06 '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 06 '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
morekeywordslisting facility might not be flexible enough to meet his/her needs. – Mico Sep 06 '11 at 18:14 -
Chapter 4.14 Escaping to LaTeX of the
listingspackage documentation provides several ways to embed LaTeX commands in your listing. – DevSolar Sep 07 '11 at 14:34
