# Using \ttfamily with \bfseries (or how to enable bold in fixed-width font)

I'm using listings package, and I've added basicstyle=\ttfamily\footnotesize. However, when I do that, the keywords from the embedded source-code are not bold anymore.

I figured that \ttfamily together with \bfseries does not work.

And then I found a FAQ about bold-extras, and I tried adding \usepackage[bold-extras], and it didn't work either. What does that package do? Should I just ignore it and move on?

And then I finally found Using bold/italic text inside listings?, which may be a duplicate to this one, but was a lot harder to find. I also found Bold Typewriter Type fonts, which got me more confused.

So, what should I do in order to get a bold fixed-width font for use within listings?

Using pdflatex from texlive-2010.

-
You need to use a font which has a bold tt family. Check tug.dk/FontCatalogue/typewriterfonts.html. AFAIK lmodern should be ok. –  Martin Scharrer Oct 29 '11 at 14:48
The Computer Modern Typewriter font family doesn't have a boldface variant. You can say \usepackage{lmodern}, maybe with the option lighttt for better distinction between medium weight and boldface letters in typewriter type. Or \usepackage{beramono}, for example, that provides a boldface variant. –  egreg Oct 29 '11 at 14:50

As Martin mentioned in the comment you need a font which provides such a combination. In the following example you can see that the font courier has this combination implemented instead of Computer Modern.

\documentclass{article}
\usepackage{listings}
\begin{document}

% Default Computer Modern font (no bold implemented)
\renewcommand{\ttdefault}{cmtt}
\begin{lstlisting}[basicstyle=\ttfamily\bfseries]
y:=2
\end{lstlisting}

% Using Courier font
\renewcommand{\ttdefault}{pcr}
\begin{lstlisting}[basicstyle=\ttfamily\bfseries]
y:=2
\end{lstlisting}

\end{document}


-
And sorry my ignorance, but how do I use this? Just adding \renewcommand{\ttdefault}{pcr}? –  Denilson Sá Oct 29 '11 at 15:00
@DenilsonSá: If you only need the typewriter font of courier you can use this command. –  Marco Daniel Oct 29 '11 at 15:04
The solution with lmodern (see Ulrike answer) looks much better than this pcr version; besides it gives a font that is consistent with the default fixed font. –  alfC Mar 26 '13 at 6:09

A different option is to use the Latin Modern fonts, that sport a fully featured boldface typewriter font. They have also another feature, because they can use a lighter version for the medium weight:

\usepackage[lighttt]{lmodern}


Let's compare a couple of examples:

\documentclass{article}
\usepackage[T1]{fontenc}

\usepackage{lmodern}

\begin{document}

Abc {\ttfamily abc \bfseries abc}

\end{document}


\documentclass{article}
\usepackage[T1]{fontenc}

\usepackage[lighttt]{lmodern}

\begin{document}

Abc {\ttfamily abc \bfseries abc}

\end{document}


Where the distinction between the weights in monospaced fonts is crucial, this can be an option.

This will work out of the box with listings and the option

basicstyle=\ttfamily,


will suffice after having loaded lmodern with or without the lighttt option.

-
Is there a reason to also load the fontenc package? Wouldn't lmodern suffice? –  A.Ellett May 21 '13 at 20:22
@A.Ellett I do it just by habit (I'm European, you know). It doesn't harm and makes available many more glyphs than OT1. –  egreg May 21 '13 at 20:23
I prefer lighttt look, but it's turns out that when there are comments in the listing it throws errors I guess it lacks italic variant? Is there a simple workaround for it? lmodern without the lighttt option works just fine. –  slawek Aug 26 '13 at 20:39
@slawek Maybe a follow-up question with a MWE is better –  egreg Aug 26 '13 at 20:40

Bold-extra is not a package you load, it is a collection of fonts. But as this are metafont (bitmap) fonts I wouldn't recommend using them. Better switch either to the lmodern fonts, or use the luximono or the beramono-typewriter font:

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
%\usepackage{luximono}
%\usepackage[scaled=0.85]{beramono}
\begin{document}
abc
\ttfamily abc \bfseries abc

\end{document}

-
Both answers are good. I'm accepting the other one because it seems to allow more fine-grained control. But, still, if I could I would accept these two answers. –  Denilson Sá Oct 29 '11 at 15:44

You can also use poors-man-bold \pmb from the package amsbsy:

\usepackage{amsbsy}
[...]
\begin{lstlisting}[keywordstyle=\pmb,language=C]
for (i = 0; i < 10; ++i)
\end{lstlisting}

-
You of course can, but LaTeX is no longer a poor man having no bold typewriter fonts. As you can see from the other answers, the font options are numerous. And \pmb renders really badly. –  yo' Nov 23 '12 at 17:41
Even though it's not the best solution nowadays, it is still a nice trick that might come handy someday. –  Denilson Sá Nov 25 '12 at 12:36

The answer by egreg loads new fonts that seems to change fonts for the whole document -- not only in listings (in my document it changed the font used in paragraphs).

The accepted answer uses pcr -- some may find it not very beautiful. You could use lmtt instead.

Below is the version that uses lmtt and additionally wraps the font-change command into the new command.

Put into the preamble of your document:

\newcommand*{\ttfamilywithbold}{\fontfamily{lmtt}\selectfont}


Now, use it like this:

\begin{lstlisting}[basicstyle=\ttfamilywithbold,language=python,mathescape]
b_reach= $C$
while (b_reach $\neq$ b_reach):
b_reach = b_reach
b_reach= b_reach $\cup$ b_pred_b(b_reach, $P$, $\pgraph$)
if b_reach contains an initial state:
return 'Yes'
return 'No'
\end{lstlisting}


You will get:

And compare it with non-bold version (that has \ttfamily instead of ttfamilywithbold`):

-