77

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
    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
    Nov 4, 2011 at 20:45
  • 1
    It is recommendable to take a look at Section 4.6 of the listings manual. Nov 4, 2011 at 20:51
  • 7
    @tugberk: See @Peter's answer. basicstyle=\ttfamily prints the listing content using \ttfamily - a monospaced font.
    – Werner
    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
    Nov 4, 2011 at 20:55

2 Answers 2

79

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.

2
  • 6
    Wouldn't this solution affect the fonts of the entire document? Or am I missing something? Sep 5, 2019 at 15:27
  • @AdrianKeister: Did you try running the MWE? Sep 5, 2019 at 16:11
52

Modify your basicstyle to include the font family:

\lstset{basicstyle=\ttfamily\footnotesize,breaklines=true}
3
  • 1
    This is far better solution especially for XeLaTeX.
    – 71GA
    Jun 20, 2017 at 8:26
  • Agreed. This works great, and is the simplest option. Sep 5, 2019 at 15:27
  • This proved to be a far better answer as it does not impact the whole document.
    – Andry
    Sep 26, 2019 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.