Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
1  
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
2  
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

4 Answers 4

up vote 19 down vote accepted

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}

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

share|improve this answer
    
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}

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.

share|improve this answer
    
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
    
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}
share|improve this answer
    
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}
share|improve this answer
    
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. –  tohecz 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á Nov 25 '12 at 12:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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