# What font to use for source code in a document? [closed]

I'm currently writing some text where I discuss a python script. So naturally, there's a lot of code in the document and it seems like listings is the default way to do so.

However, unlike what I'm used to when using latex, the default looks atrocious!

Non-monospaced source code!? WTF? (side question: or is there really a typographic "rule" for this?)

OK, I can fix that after a quick read of the docs with \ttfamily.

But this is still very weird, mostly the italic part for the comments. It just looks totally off to me.

Is this because I just happen to be used to weird fonts in my editors (Monospace Regular, not too weird, I'd guess) and this is normal and typographically sound, or is there a better option?

And if so, what would that option be? I'd rather not use some random font that I, personally, like but that is objectively bad, especially in combination with my default fonts for the text (all from Komas scrbook).

Edit:

Obviously, I can just use "random" monospaced/typewriter fonts, but my main concern is that I'll just pick one that will not work (typographically) with the default computer modern for the body of the document. So not only should the font work as a monospaced font for readable source code, it should also not look "off" next to computer modern. So some kind of website listing font X is compatible with font Y would probably be the most useful, but I haven't found one yet.

Another Edit:

I can't accept an answer, since I feel that the above is really important when using latex. To quote myself in an comment to @egreg's answer:

But isn't getting away from personal taste and instead using "professional" judgement the whole idea behind latex? Otherwise I'd just use Word with times new roman and comic sans like everyone else…

However, it seems like my feeling that the defaults are not optimal is shared, so I'm going to change them. Depending on the desired outcome, I think @egreg's and @Thruston's answers are good options. The first allows for italic comments, but the font seems off, compared to computer modern, whereas the second can't do italics, but has a fitting font. Setting commentstyle=\color{gray} for @Thruston's answer works best for me, since that survives black and white printing and also follows what I would consider an accepted default in most editors. Other than that, I will not bother with colors, since that seems hugely controversial. There have been forum wars fought over light vs. dark background or a solarized theme vs. hacker green… so I'll just stick with black and white (and gray…).

MWE:

\documentclass[a4paper,11pt, headings=normal, toc=bibliography, toc=listof]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{listings}
\lstset{language=Python, numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt, tabsize=4}%, basicstyle=\ttfamily}

\begin{document}

This is some other text.

\begin{lstlisting}
for i in list:
print i
# prints every entry in a list
\end{lstlisting}

This here is also non, sourcecode text.

\end{document}


## closed as primarily opinion-based by user36296, Romain Picot, Sebastiano, Phelype Oleinik, Stefan PinnowOct 11 '18 at 11:27

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

• You probably want to add columns=fullflexible, keepspaces=true, into your lstset... – Thruston Oct 9 '18 at 12:39
• Source code and similar text printed in a fairly "wide" sans-serif non-monospaced font usually looks fine, unless there is a reason you need to line up the text columns. In Python you certainly need to line up the Indents at the start of lines, because they encode the logical structure of the code, but I don't see why the rest of the line would need to be monospaced. FWIW Algol code (an early block structured language) was printed in books with proportionally spaced fonts right from the time the language was invented in the 1960s - long before TeX. – alephzero Oct 9 '18 at 17:37
• Can you formulate your question in a less rant-like fashion? If you want to ask how to change the font that is fine. If you want to know which font to choose that is borderline for too opinion based, but insulting the default settings of a package is not productive. – user36296 Oct 11 '18 at 9:59
• I already put it into the question that I'm not looking for opinions, but for some "official" guidelines on the whole issue. Alternatively, a link that explains how the defaults are in fact OK would also be OK, but unless there is one, it stands to reason that the defaults are in fact wrong, as per the general principle that source code ought to be monospaced. – JC_CL Oct 11 '18 at 14:18

In my opinion the defaults in lstlisting are very odd, but it's not hard to tame them. Here is your example with a few extra keys and some colour.

\documentclass[a4paper,11pt, headings=normal, toc=bibliography, toc=listof]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\definecolor{textblue}{rgb}{.2,.2,.7}
\definecolor{textred}{rgb}{0.54,0,0}
\definecolor{textgreen}{rgb}{0,0.43,0}
\usepackage{listings}
\lstset{language=Python,
numbers=left,
numberstyle=\tiny,
stepnumber=1,
numbersep=5pt,
tabsize=4,
basicstyle=\ttfamily,
keywordstyle=\color{textblue},
stringstyle=\color{textgreen},
frame=none,
columns=fullflexible,
keepspaces=true,
xleftmargin=\parindent,
showstringspaces=false}
\begin{document}

This is some other text.

\begin{lstlisting}
for i in list:
print i, "OK"
# prints every entry in a list
\end{lstlisting}

This here is also non, sourcecode text.

\end{document}


If you wanted comments in italics, then you just add it to the commentstyle line:

...
...


this produces:

For more details do texdoc listings...

If you don't like the look of the default ttfamily then try some different ones. You may also need to change the body font to match the monofont of course, the default fonts may not be quite what you want, but at least they go together well.

• This is still \ttfamily though, so having the comments in italics would just result in the same weirdness? Which of the settings controls the italics (or lack thereof), it's not readily apparent from the code. – JC_CL Oct 9 '18 at 12:55
• @JC_CL Add to the preamble the line \usepackage{sourcecodepro}, does this improve the result? OK, you need to have an installation including the Sourcecode Pro fonts. – Keks Dose Oct 9 '18 at 13:22
• commentstyle=\color{textred}\itshape I see. was searching for itali in the lstlisting docs and didn't find anything. The colors are all nice, but my main gripe is that italics look totally off, but (as per most editor defaults) comments should be in italics. \usepackage{sourcecodepro} gives me an error, even though I installed the font, but this probably gets me into "how to mix and match fonts in a document?" hell, as does the linked question, something I'd have hoped to avoid. – JC_CL Oct 9 '18 at 13:46
• Computer Modern "typewriter italic" is one of the ugliest fonts ever invented IMO. Comic Sans is a lot better! I wonder what Knuth was thinking when he came up with it.... – alephzero Oct 9 '18 at 17:43

It's not simply the serif font that makes the listing awful: it's mainly the font not being monospaced, so the characters look like being thrown on the page at random.

You might color the listing: good for screen reading, less good for printed paper.

Otherwise, you could look for different monospaced font (I don't like the italic default typewriter type too).

Here's a try with SourceCode Pro, just to see whether the result looks better.

\documentclass[a4paper,11pt, headings=normal, toc=bibliography, toc=listof]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{listings}

\lstset{
language=Python,
numbers=left,
numberstyle=\tiny,
stepnumber=1,
numbersep=5pt,
tabsize=4,
basicstyle=\ttfamily,
columns=fullflexible,
keepspaces,
}

\begin{document}

This is some other text.

\begin{lstlisting}
for i in list:
print i
# prints every entry in a list
\end{lstlisting}

This here is also non, sourcecode text.

This is some other text.

% roughly emulate \usepackage{sourcecodepro}
\lstset{
basicstyle=\fontfamily{SourceCodePro-TLF}\selectfont
}

\begin{lstlisting}
for i in list:
print i
# prints every entry in a list
\end{lstlisting}

This here is also non, sourcecode text.

\end{document}


OK, let's assume you like it. But it's definitely too big: we can fix it.

\documentclass[a4paper,11pt, headings=normal, toc=bibliography, toc=listof]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{listings}

\usepackage[scale=0.85]{sourcecodepro}

\lstset{
language=Python,
numbers=left,
numberstyle=\tiny,
stepnumber=1,
numbersep=5pt,
tabsize=4,
basicstyle=\ttfamily,
columns=fullflexible,
keepspaces,
}

\begin{document}

This is some other text.

\begin{lstlisting}
for i in list:
print i
# prints every entry in a list
\end{lstlisting}

This here is also non, sourcecode text.

\end{document}


There are several other monospaced fonts available, see the LaTeX font catalogue

A different choice might be LuxiMono:

\documentclass[a4paper,11pt, headings=normal, toc=bibliography, toc=listof]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{listings}

\usepackage[scaled=0.85]{luximono}

\lstset{
language=Python,
numbers=left,
numberstyle=\tiny,
stepnumber=1,
numbersep=5pt,
tabsize=4,
basicstyle=\ttfamily,
columns=fullflexible,
keepspaces,
}

\begin{document}

This is some other text.

\begin{lstlisting}
for i in list:
print i0123
# prints every entry in a list
\end{lstlisting}

This here is also non, sourcecode text.

\end{document}


• I don't like the zero with a point inside, I find it unreadable. – AndréC Oct 9 '18 at 14:01
• I on the other hand don't mind the dot… However, this font looks kinda off, compared to computer modern, and I'm no typographer that can judge if they fit. But it certainly is better than the atrocious italics in the default. – JC_CL Oct 9 '18 at 14:08
• luximono looks a tad better to me, but that probably is subjective. And I'd like to avoid relying on my subjective judgement. I added some clarification in the main text, maybe someone knows about some "official" rules for that. – JC_CL Oct 9 '18 at 14:18
• @JC_CL No "official” rule; personal taste is usually a good judge. – egreg Oct 9 '18 at 14:22
• But isn't getting away from personal taste and instead using "professional" judgement the whole idea behind latex? Otherwise I'd just use Word with times new roman and comic sans like everyone else… Now I'm not saying that luximono is "wrong", but I also don't know if it is "right". – JC_CL Oct 9 '18 at 14:26

Personally, for listing, the fundamental criterion is that the 0 is different from the letter O. For the inconsolata font is perfect.

\documentclass[a4paper,11pt, headings=normal, toc=bibliography, toc=listof]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{listings}
\lstset{language=Python, numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt, tabsize=4, basicstyle=\ttfamily}
\usepackage{inconsolata}
\begin{document}

This is some other text.

the 0 is not crossed out like the letter O

\begin{lstlisting}
for i in list:
print i
# prints every entry in a list
#  the 0 is crossed out unlike the letter O
\end{lstlisting}

This here is also non, sourcecode text.

\end{document}

• 0 vs. O is certainly important (and also not handled too well in the default), but it appears that inconsolata can't deal with italic comments. Also, I'm afraid that this the dreaded mixing of unmatching fonts. – JC_CL Oct 9 '18 at 13:55
• @JC_CL Indeed, the inconsolata font has no italics. – AndréC Oct 9 '18 at 13:59
• 0 vs O was important back in the day when computers couldn't even print lower-case letters, but if you write code in the 21st century where you need to distinguish 0 and upper-case O (or 1, upper-case I and lower-case L) in your variable names, the best solution to that issue is not "find a different font" ;) – alephzero Oct 9 '18 at 17:47
• @alephzero What is the best solution then? – AndréC Oct 9 '18 at 17:51