# Can I change the font just for lstlisting?

I'd like to keep all the fonts for the document the same and only change the font for the llstststing environment to something sensible like IBM Plex mono, Adobe Source Code Pro, or Noto Sans Mono. Is that possible and how so?

MWE:

\documentclass{article}

\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{plex-mono}

\usepackage[table,xcdraw]{xcolor}
\definecolor{backcolour}{rgb}{0.98,0.98,0.95}

\usepackage{listings}
\lstdefinestyle{prettycode}{
backgroundcolor=\color{backcolour},
aboveskip={0.9\baselineskip},
keepspaces=true,
}
\lstset{style=prettycode}

\begin{document}
\section{Section One}
\blindtext

\begin{lstlisting}[language=c, caption=Hello world program., label=listing:hello_world]
#include <stdio.h>

int main()
{

// prints hello world
printf("Hello World");

return 0;
}
\end{lstlisting}
\subsection{Subsection}
\blindtext
\end{document}


Related, but solutions seem to change the typewriter font for the whole document: set the font family for lstlisting

• If you already have a font-switching command which can be defined by eg fontspec's \newfontfamily, then \lstset{basicstyle=<cmd>} will do the work. Feb 10 at 12:58
• switching from pdflatex to xelatex is messing with all the fonts of the document. I absolutely need fontspec? Feb 10 at 13:48
• As the fonts you mentioned are all otf/ttf fonts, yes (unless you want to directly use \font primitive). Feb 10 at 13:51
• Where can I learn more about this /font primitive? Feb 10 at 16:02
• The XeTeX Reference and the LuaTeX Reference. fontspec is more or less just a wrapper to (different) font selection primitives and syntaxes provided by xetex and luatex, and it provides an cross-engine, easier-to-use, easier-to-read interface. Why you want to avoid it so much? Feb 10 at 16:23

I'd not use different monospaced fonts for different parts of one and the same document.

However, you can do what you want, provided you delve a bit into the font packages.

##### IBM Plex Mono
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[table,xcdraw]{xcolor}
\usepackage{listings}

\definecolor{backcolour}{rgb}{0.98,0.98,0.95}

\newcommand{\listingsttfamily}{\fontfamily{IBMPlexMono-TLF}\small}

\lstdefinestyle{prettycode}{
basicstyle=\listingsttfamily,
backgroundcolor=\color{backcolour},
aboveskip={0.9\baselineskip},
keepspaces=true,
}
\lstset{style=prettycode}

\begin{document}

\section{Section One}

Some text with a \texttt{monospaced} insert.

\begin{lstlisting}[language=c, caption=Hello world program., label=listing:hello_world]
#include <stdio.h>

int main()
{

// prints hello world
printf("Hello World");

return 0;
}
\end{lstlisting}

\end{document}


##### Source Code Pro

Replace the line for \listingsttfamily with

\newcommand{\listingsttfamily}{\fontfamily{SourceCodePro-TLF}\small}


##### Noto Sans Mono

Replace the line with

\newcommand{\listingsttfamily}{\fontfamily{NotoSansMono-TLF}\small}


but note that this has no italics nor slanted shapes.

• I understand it's against typography common sense to use two different monospaced fonts in the same document, but this is a conference template I want to adhere to. At the same time it doesn't have any example for code display which I find hard to read in courier family font. Hence I don't want to touch the rest of the style, just the listing style. Thanks for the excellent solution. Feb 14 at 11:13