# Best unicode font to work with minted in xelatex/lualatex

I have a bunch of unicode heavy (Julia) code that I want to plot with minted and am having trouble getting the unicode to display. For example, take the following code

f(x) = x.^2 + π
const ⊗ = kron
const Σ = sum
# Calculate Σ_{j=1}^5 j^2
Σ([j^2 for j ∈ 1:5])


Now, put this in a xelatex file

% !TEX engine = xelatex
\documentclass[12pt]{article}
\usepackage[letterpaper]{geometry}
\usepackage{fontspec,unicode-math}
\usepackage{xunicode}
\setmonofont{Consolas} %Might be better ones?
\usepackage{minted}
\begin{document}
\begin{minted}{julia}
f(x) = x.^2 + π
const ⊗ = kron
const Σ = sum # Although sum may be just as good in the code.
# Calculate Σ_{j=1}^5 j^2
Σ([j^2 for j ∈ 1:5])
\end{minted}
\end{document}


If you compile this, you will see that the $\pi$ and $\Sigma$ work fine, but the \odot and $\in$ do not. Is this something that I need to pick a better monofont for? Do I need other packages? Etc.

You can fill in the gaps by making the characters active with a suitable definition

% !TEX engine = xelatex
\documentclass[12pt]{article}
\usepackage[letterpaper]{geometry}
\usepackage{fontspec,unicode-math}
\usepackage{newunicodechar}
\newunicodechar{∈}{\makebox[\fontcharwd\fonta]{$\in$}}
\newunicodechar{⊗}{\makebox[\fontcharwd\fonta]{$\otimes$}}
\setmonofont{Consolas} %Might be better ones?
\usepackage{minted}
\begin{document}
\begin{minted}{julia}
f(x) = x.^2 + π
const ⊗ = kron
const Σ = sum # Although sum may be just as good in the code.
# Calculate Σ_{j=1}^5 j^2
Σ([j^2 for j ∈ 1:5])
\end{minted}
\end{document}

• Wow! \newunicodechar! Mar 19, 2018 at 18:37
• @egreg the package doesn't have the simple elegance of indentfirst but it has its uses sometimes. Mar 19, 2018 at 18:39
• @DavidCarlisle You and egreg are the best. +1. Mar 19, 2018 at 21:57

If you want to avoid having to define characters, you could switch to a font that has those characters. The GNU FreeMono font is a good one since it is free and has over 4,000 characters. It has a much larger character set than most monospace fonts, including the previously missing math characters you needed. You can download it here: https://www.gnu.org/software/freefont/

This is an updated example using the FreeMono font that displays the missing characters.

% !TEX engine = xelatex
\documentclass[12pt]{article}
\usepackage[letterpaper]{geometry}
\usepackage{fontspec,unicode-math}
\usepackage{xunicode}
\setmonofont{FreeMono} %switched to FreeMono
\usepackage{minted}
\begin{document}
\begin{minted}{julia}
f(x) = x.^2 + π
const ⊗ = kron
const Σ = sum # Although sum may be just as good in the code.
# Calculate Σ_{j=1}^5 j^2
Σ([j^2 for j ∈ 1:5])
\end{minted}
\end{document}


• nks. Are there any of these fonts that are part of a typical miktex installation, or download able from CTAN ? I would prefer not to have additional setup instructions Mar 20, 2018 at 2:18
• I'm not sure if it comes with miktex, but since you are already using xelatex, you just have to download the fonts in otf version (ftp.gnu.org/gnu/freefont/freefont-otf-20080323.zip) to your system and install them normally, which is a painless process usually. Xelatex with then be able to access your system fonts, without any special latex-related extra steps.