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?

8
  • 4
    You need a font that matches your criteria. With Xe-/LuaLaTeX this would be easy (something like Fira Mono).
    – TeXnician
    Commented Feb 3, 2018 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
    Commented Feb 3, 2018 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
    Commented Feb 3, 2018 at 22:39
  • 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
    Commented Feb 3, 2018 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
    Commented Feb 4, 2018 at 8:02

2 Answers 2

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}
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

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .