17

I'm writing my dissertion using TeX. I would like to use a specific font in my lstlisting source code. How can I use a specific font like Monaco or Arial? I usually found solutions with basicstyle setted to \ttfamily but it's not what I want.

Werner
  • 603,163
Eng
  • 171

2 Answers2

19

You can't use a font in TeX unless it's known to the TeX system. For example Monaco isn't, unless you use XeLaTeX or LuaLaTeX on Mac OS X. A good typewriter font for pdflatex can be BeraMono:

\usepackage[scaled]{beramono}

and then \ttfamily will use this font. You can say also scaled=.85 or some other number, in order to accommodate the size to your main font.

There's a clone of Courier

\usepackage{tgcursor}

that sports a neat distinction between medium and bold weight (but might not agree with your main font).

However, you're not forced to use typewriter type for listings; if you say

\lstset{basicstyle=\sffamily}

you'll get whatever sans serif font is the default. You can change the default font by loading the suitable package; for example

\usepackage{tgheros}

will load a clone of Helvetica as the sans serif font and the above declaration will make this one the default. This package has a scale option similar to the scaled for beramono. Note that Arial is not considered to be a free font, so it's not in standard TeX distributions.

If you're using XeLaTeX or LuaLaTeX as typesetting engines, then you can use whatever OpenType or TrueType font you have on your system. For example, after having loaded fontspec you can define a suitable command

\usepackage{fontspec}
...
\newfontfamily{\lstsansserif}[Scale=.85]{Arial}

and then \lstset{basicstyle=\lstsansserif} would use Arial for listings.

egreg
  • 1,121,712
12

Instead of \ttfamily you can use all the other commands which select a font. E.g.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings}
\lstset{basicstyle=\fontfamily{pzc}\selectfont} %Zapf

\begin{document}
\begin{lstlisting}
abcde
\end{lstlisting}
\end{document}
Ulrike Fischer
  • 327,261