54

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.

David Carlisle
  • 757,742
  • 2
    You need to use a font which has a bold tt family. Check http://www.tug.dk/FontCatalogue/typewriterfonts.html. AFAIK lmodern should be ok. – Martin Scharrer Oct 29 '11 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 '11 at 14:50

5 Answers5

49

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.

Marco Daniel
  • 95,681
  • And sorry my ignorance, but how do I use this? Just adding \renewcommand{\ttdefault}{pcr}? – Denilson Sá Maia 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
  • 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 '13 at 06:09
27

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.

egreg
  • 1,121,712
  • Is there a reason to also load the fontenc package? Wouldn't lmodern suffice? – A.Ellett May 21 '13 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 '13 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 '13 at 20:39
  • @slawek Maybe a follow-up question with a MWE is better – egreg Aug 26 '13 at 20:40
  • 1
    lighttt messed up the italic style and now it shows it as tilted. – Atcold Apr 11 '23 at 19:48
  • @Atcold I still have yet to find a solution to this. The answers from your post did not help since I have hundreds of thousands of listings and I do NOT want to go through and change them all :( – TheProgrammer Jul 13 '23 at 02:26
  • @TheProgrammer You just need \lstset{basicstyle=\ttfamily} in the document preamble – egreg Jul 13 '23 at 06:55
  • @egreg But the ttfamily I’m using is the lighttt lmodern variant. This is excellent, except for the fact that it overwrites the italic font face with the slanted version. I guess this is just a limitation of the font… – TheProgrammer Jul 13 '23 at 16:14
  • @TheProgrammer There is no Latin Modern Mono bold italic, just bold slanted. – egreg Jul 13 '23 at 20:17
  • @egreg, we were talking about Latin Modern Mono italic, which exists for the non-light version but it's inexistent for the light version. (Maybe we are talking about the same thing but I didn't get it.) – Atcold Jul 18 '23 at 18:45
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}
Ulrike Fischer
  • 327,261
  • 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á Maia Oct 29 '11 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}
Mensch
  • 65,388
Maciek
  • 41
  • 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 '12 at 17:41
  • 1
    Even though it's not the best solution nowadays, it is still a nice trick that might come handy someday. – Denilson Sá Maia Nov 25 '12 at 12:36
2

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?

Ayrat
  • 255