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.
\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}
DejaVuSansMono
orberamono
just change the meaning of\ttdefault
, which is just what you want; they also allow to specify a scaling factor. The packagetgcursor
does the same, but unfortunately also changes the meaning of\bfdefault
; just add\renewcommand{\bfdefault}{bx}
after loadingtgcursor
.\ttdefault
...\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.