Can I change the font just for lstlisting

listings

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

Best Answer

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}

enter image description here

Source Code Pro

Replace the line for \listingsttfamily with

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

enter image description here

Noto Sans Mono

Replace the line with

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

but note that this has no italics nor slanted shapes.

enter image description here