2

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(&quot;Hello World&quot;);

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

Max N
  • 425
  • 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. – muzimuzhi Z Feb 10 '22 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 '22 at 13:48
  • 1
    As the fonts you mentioned are all otf/ttf fonts, yes (unless you want to directly use \font primitive). – muzimuzhi Z Feb 10 '22 at 13:51
  • Where can I learn more about this /font primitive? – Max N Feb 10 '22 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? – muzimuzhi Z Feb 10 '22 at 16:23
  • Because it's not a fresh document, it is a template and once I change it from pdflatex to xelatex or lualatex all the fonts of the document are changed. – Max N Feb 10 '22 at 16:26
  • Then the problem is not about fontspec at all, but about changing engine from pdftex to xe- or lua-tex, right? LaTeX uses TU font encoding for unicode engines, but T1 for pdftex. This may cause font messing. If the font you want is accessible under pdftex, then it's always possible to define a font switching command and use it in basicstyle=<cmd>. Among the fonts you mentioned, both plex and noto have pdftex support. If you've decided which font to use and have trouble applying it only for lstlisting, welcome back. – muzimuzhi Z Feb 10 '22 at 17:08
  • I've added an mwe. How canI change the font without switching to luatex or xetex? – Max N Feb 12 '22 at 13:07

1 Answers1

3

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(&quot;Hello World&quot;);

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

egreg
  • 1,121,712
  • 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 '22 at 11:13