87

I have the following set up for my listings :

\lstset{basicstyle=\footnotesize,breaklines=true}
\lstset{framextopmargin=50pt,frame=bottomline}

I would like to change the font family to Courier. How can I enable this?

tugberk
  • 4,177
  • 7
  • 25
  • 26
  • Why not just use a monospaced font like \ttfamily (from Computer Modern)? – Werner Nov 04 '11 at 20:44
  • @Werner I am sorry but I am not following you here. You might have got that I am newbie :s – tugberk Nov 04 '11 at 20:45
  • 1
    It is recommendable to take a look at Section 4.6 of the listings manual. – Thorsten Donig Nov 04 '11 at 20:51
  • 7
    @tugberk: See @Peter's answer. basicstyle=\ttfamily prints the listing content using \ttfamily - a monospaced font. – Werner Nov 04 '11 at 20:52
  • @Werner what does ttfamily actually do here? To me, just a miracle happen there. I didn't say the you can use the courier. I just put the ttfamily there. – tugberk Nov 04 '11 at 20:55
  • @tugberk: \ttfamily is a Computer Modern font which looks very similar to Adobe's Courier. Adding \usepackage{courier} overrides the \ttfamily output to now represent Courier. If the similarities are negligible to you, just use \ttfamily. It's that simple. – Werner Nov 04 '11 at 20:58
  • @Werner aha, ttfamily is a font family. I thought that was a command. thanks for the clarification. – tugberk Nov 04 '11 at 21:17
  • @tugberk: You can use \texttt{...} or {\ttfamily ...} which produces the same result. – Werner Nov 04 '11 at 21:20

3 Answers3

84

With \usepackage{courier} in your preamble you can use the courier font in the listings as follows:

\documentclass[12pt]{article}
\usepackage{listings}
\usepackage{lipsum}
\usepackage{courier}

\lstset{basicstyle=\footnotesize\ttfamily,breaklines=true}
\lstset{framextopmargin=50pt,frame=bottomline}

\begin{document}
\begin{lstlisting}
   a b c
\end{lstlisting}
\lipsum[1]
\end{document}

The \usepackage{courier} in the preamble causes \ttfamily to produce output in the courier font. Without including this package, you can still use \ttfamily to get a mono spaced font by including that as part of the basicstyle=... setting.

Peter Grill
  • 223,288
59

Modify your basicstyle to include the font family:

\lstset{basicstyle=\ttfamily\footnotesize,breaklines=true}
John Leonard
  • 1,294
2

If you use \lstset{basicstyle=\fontfamily{pcr}\footnotesize} it will set only the font used in listings to Courier. Original response where this snippet was found is here. A list containing some of the supported fonts can be found here.

  • Warning: \fontfamily should be followed by \selectfont, in this case it just happen to work because it's followed by \footnotesize -- in particular if you swap the 2 commands it will not work anymore. – user202729 Nov 18 '23 at 13:19