# How to get a monospaced sans-serif font for the source code when using lstinputlisting

I have a document similar to this:

\usepackage{listings}
\begin{document}
\lstset{language=Matlab,%
basicstyle=...
}

\subsection*{Matlab code}
\lstinputlisting{../myScript.m}

\end{document}


I have found several answers on how to use lstlisting with a monospaced font and a few on how to get a sans-serif font by setting basicstyle equal to \ttfamily or \sffamily respectively.

But what should I do if I like to display my source code in a monospaced AND sans-serif font?

• You need a font that matches your criteria. With Xe-/LuaLaTeX this would be easy (something like Fira Mono). – TeXnician Feb 3 '18 at 21:24
• Please provide compilable code, as this is much more helpful than mere fragments. In this case, however, the issue is to find a font to your liking, as @TeXnician says. – cfr Feb 3 '18 at 22:05
• @cfr Since I am using \lstinputlisting{...} I think what is contained in my question is about the best "MWE" (or rather "ME") I can come up with as I shouldn't provide the rather text heavy input file. Besides it doesn't really matter what the input file is, so just write something in sometext.txt and save it in the same folder as the TeX-file and replace ../myScript.m with sometext.txt. The reason I provide basicstyle with no value is because I hoped that someone would fill it out solving my problem. However the answer doesn't seem that simple for pdfLaTeX... – Malte Feb 3 '18 at 22:39
• Exactly: it doesn't matter. So don't provide the 'text heavy' input file. Just 'write something in sometext.txt' and copy it into your question for people to copy-paste. You can use filecontents to make this more convenient, though people will deal with it if you don't. People are more likely to provide you with an example solution if all they have to do is add the font stuff to what you give them. They are less likely to if they have to complete your fragment first. You need a document class, too. – cfr Feb 3 '18 at 22:43
• Your approach with setmonofont seems right. If you've installed a complete TeX distribution you can use "Fira Mono" as argument. Then use basicstyle=\ttfamily and you're done. – TeXnician Feb 4 '18 at 8:02

Assuming you have access to fonts provided by your operating system, achieving your goal is straightforward: (a) Employ LuaLaTeX (or XeLaTeX) instead of pdfLaTeX; (b) load the fontspec package and load a suitable monospaced font via a \setmonofont directive; and (c) after loading the listings package, add the option basicstyle=\ttfamily to the argument(s) of \lstset.

\documentclass{article}
\usepackage{fontspec}
%\setmainfont{...} % select a suitable main text font
%\setsansfont{...} % select a sans-serif font
\setmonofont{Lucida Console}[Scale=MatchLowercase] % select a suitable monospaced font

\usepackage{listings}
\lstset{basicstyle=\ttfamily}

\begin{document}
Hello World. \lstinline{Hello World.}
\end{document}