12

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.

12

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

enter image description here

% !TEX engine = xelatex
\documentclass[12pt]{article}
\usepackage[letterpaper]{geometry}
\usepackage{fontspec,unicode-math}
\usepackage{newunicodechar}
\newunicodechar{∈}{\makebox[\fontcharwd\font`a]{$\in$}}
\newunicodechar{⊗}{\makebox[\fontcharwd\font`a]{$\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}
  • 1
    Wow! \newunicodechar! – egreg Mar 19 '18 at 18:37
  • 1
    @egreg the package doesn't have the simple elegance of indentfirst but it has its uses sometimes. – David Carlisle Mar 19 '18 at 18:39
  • @DavidCarlisle You and egreg are the best. +1. – Sebastiano Mar 19 '18 at 21:57
7

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}

FreeMono output

  • 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 – jlperla Mar 20 '18 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. – Brad Mar 20 '18 at 2:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.