11

As has been asked often enough, to get bold keywords in lstlisting environments, one has to use a different typewriter font. Most answers suggest to include this or that package.

If just including a package changes the font, then the package must be applying some magic in the background to install itself. And unless the package was specifically made for listings, this means it potentially also changes other stuff.

How can I use another font in my listings while being sure that nothing else is affected, or at least that nothing but the default typewriter font is changed?

3
  • 2
    Good font packages such as DejaVuSansMono or beramono just change the meaning of \ttdefault, which is just what you want; they also allow to specify a scaling factor. The package tgcursor does the same, but unfortunately also changes the meaning of \bfdefault; just add \renewcommand{\bfdefault}{bx} after loading tgcursor.
    – egreg
    Commented May 2, 2014 at 20:08
  • @egreg I think Arian wants to use a different font for listings without changing \ttdefault...
    – jub0bs
    Commented May 2, 2014 at 20:27
  • Changing \ttdefault is acceptable, as Herbert said it doesn't make much sense to use different monospaced fonts. But my work has to conform to a given style (cls file is provided), and I want to change it as little as possible.
    – Cephalopod
    Commented May 2, 2014 at 21:02

2 Answers 2

7

That's an excellent question. Many answers to questions about listings on this site tell you to load along with the fontenc package with option T1 (which is fine) along with a package such as courier or beramono in order to have access to boldface typewriter fonts. However, packages like beramono and courier change the default typewriter font family globally, which may not be what you want.

If you want a certain font only inside listings, you should simply select it in the basicstyle key.

basicstyle = \fontfamily{desired-font}\selectfont ...

I suggest you define a macro to easily select the font in question (see below). You should still load the fontenc package with option T1, but don't load beramono or the likes of it.

enter image description here

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{listings}

% macro to select a scaled-down version of Bera Mono (for instance)
\makeatletter
\newcommand\BeraMonottfamily{%
  \def\fvm@Scale{0.85}% scales the font down
  \fontfamily{fvm}\selectfont% selects the Bera Mono font
}
\makeatother

\lstset{
  basicstyle=\BeraMonottfamily, 
  frame=single,
}

\begin{document}

\texttt{Computer Modern typewriter font, as usual}

\begin{lstlisting}[language=C,caption=listing typeset with Bera Mono]
/* Hello World program */

#include<stdio.h>

main()
{
    printf("Hello World");

}
\end{lstlisting}
\end{document}
2
2

It makes no real sense to use a Monofont for listings and the default CM mono for typewriter text. Use it for all \ttfamily and it looks nicer. It is one of the typographers rule not to mix a lot of different fonts. A scaled version of DejaVu Mono:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[scaled=0.9]{DejaVuSansMono}
\usepackage{listings}

\lstset{basicstyle=\ttfamily}

\begin{document}
\begin{lstlisting}[language=C]
/* Hello World program */

#include<stdio.h>

main()
{
    printf("Hello World");

}
\end{lstlisting}
\end{document}

enter image description here

1
  • 1
    The OP doesn't want the default typewriter font to be Bera Mono everywhere in the document, only in listings.
    – jub0bs
    Commented May 2, 2014 at 19:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .