90

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?

8
  • Why not just use a monospaced font like \ttfamily (from Computer Modern)?
    – Werner
    Commented Nov 4, 2011 at 20:44
  • @Werner I am sorry but I am not following you here. You might have got that I am newbie :s
    – tugberk
    Commented Nov 4, 2011 at 20:45
  • 1
    It is recommendable to take a look at Section 4.6 of the listings manual. Commented Nov 4, 2011 at 20:51
  • 7
    @tugberk: See @Peter's answer. basicstyle=\ttfamily prints the listing content using \ttfamily - a monospaced font.
    – Werner
    Commented Nov 4, 2011 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
    Commented Nov 4, 2011 at 20:55

3 Answers 3

85

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.

3
  • 11
    Wouldn't this solution affect the fonts of the entire document? Or am I missing something? Commented Sep 5, 2019 at 15:27
  • @AdrianKeister: Did you try running the MWE? Commented Sep 5, 2019 at 16:11
  • @AdrianKeister this is not the case.
    – Genfood
    Commented Jan 7 at 17:53
59

Modify your basicstyle to include the font family:

\lstset{basicstyle=\ttfamily\footnotesize,breaklines=true}
1
  • This proved to be a far better answer as it does not impact the whole document.
    – Andry
    Commented Sep 26, 2019 at 15:46
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.

1
  • 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
    Commented Nov 18, 2023 at 13:19

You must log in to answer this question.

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