52

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.

2
  • 2
    You need to use a font which has a bold tt family. Check tug.dk/FontCatalogue/typewriterfonts.html. AFAIK lmodern should be ok. Oct 29, 2011 at 14:48
  • 3
    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, 2011 at 14:50

5 Answers 5

48

As Martin mentioned in the comment, you need a font which provides such a combination. The following example shows 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}

The first "y:=2" is not rendered as bold, but the second one is.

3
  • And sorry my ignorance, but how do I use this? Just adding \renewcommand{\ttdefault}{pcr}? Oct 29, 2011 at 15:00
  • @DenilsonSá: If you only need the typewriter font of courier you can use this command. Oct 29, 2011 at 15:04
  • 1
    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, 2013 at 6:09
24

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}

enter image description here

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

\usepackage[lighttt]{lmodern}

\begin{document}

Abc {\ttfamily abc \bfseries abc}

\end{document}

enter image description here

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.

4
  • Is there a reason to also load the fontenc package? Wouldn't lmodern suffice?
    – A.Ellett
    May 21, 2013 at 20:22
  • 1
    @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, 2013 at 20:23
  • 1
    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, 2013 at 20:39
  • @slawek Maybe a follow-up question with a MWE is better
    – egreg
    Aug 26, 2013 at 20:40
15

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}
1
  • 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. Oct 29, 2011 at 15:44
4

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}
2
  • 2
    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, 2012 at 17:41
  • 1
    Even though it's not the best solution nowadays, it is still a nice trick that might come handy someday. Nov 25, 2012 at 12:36
1

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:

result with ttfamilywithbold

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

enter image description here

The main source: How do I use a particular font for a small section of text in my document?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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