1

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

8
  • 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?
    – Max N
    Feb 10 at 13:48
  • 1
    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?
    – Max N
    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

1 Answer 1

2

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

1
  • 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.
    – Max N
    Feb 14 at 11:13

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.