2

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?

Malte
  • 55
  • 4
    You need a font that matches your criteria. With Xe-/LuaLaTeX this would be easy (something like Fira Mono). – TeXnician Feb 03 '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 03 '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 03 '18 at 22:39
  • @TeXnician I must admit that I am not familiar with Xe- or LuaLaTeX, but can you write an example with XeLaTeX? I did try to configure TexMaker to XeLaTeX and that adding \usepackage{fontspec} followed by \setmonofont{...} to the preamble, but I hadn't luck in finding any font package names of the fonts in my system that are both monospaced AND sans-serif...but maybe I have got the approach all wrong? – Malte Feb 03 '18 at 22:43
  • 1
    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 03 '18 at 22:43
  • 1
    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 04 '18 at 08:02
  • @cfr I'll take notice of that. – Malte Feb 05 '18 at 19:30
  • @TeXnician Thanks a lot. The "Fira Mono" didn't work, but the problem was as you suggest to find a proper font stored in my system. It work with the font "Noto Sans". – Malte Feb 05 '18 at 19:31

2 Answers2

2

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.

enter image description here

\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}
Mico
  • 506,678
1

A nice sans serif monospace font is Source Code Pro from Google. You can use it just by importing it:

\usepackage{sourcecodepro}

MWE:

\documentclass{article}
\usepackage{sourcecodepro}

\begin{document} \texttt{This text is in source code pro.} \end{document}

Source code pro

Gepeto97
  • 11
  • 3