# Typesetting Julia Code in Latex Documents (Unicode)

Hey there,

I love how I can use Unicode in Julia programming. When trying to write my report about my work I encountered a problem, though. Regular LateX is not compatible with Unicode characters.
This is not a problem in equations as you can use \alpha and such but this doesn’t work properly with
code embedded through listings or minted.

Luckily there is a fix for people who don’t want to give up and
stop using Unicode characters while coding.

This is a report of what I did to make it work and I hope it will
be helpful to others in the future.

1. Change LateX Engine
The standard LateX engine pdfLateX is not capable of having Unicode in your .tex file.
A more recent engine that can do this is LuaLateX or XeTeX.
I chose LuaLateX. ( You can set this in the settings of your LateX editor)

2. Change Font encoding / Language packages
\usepackage[utf8]{inputenc}
\usepackage{babel}
use:
\usepackage{fontspec}
\usepackage{polyglossia}
which are more recent implementations. You also need to set a font that
has all the characters you want to use. For example:
\setmonofont{DejaVu Sans Mono}[Scale=MatchLowercase]

3. Install Minted
This package uses a python library called Pygmentize to proberly set the code.
To make this possible it needs to be allowed to call Python in the first place.
For this add –shell-escape as a flag to LuaLateX in the settings of your editor.
You will probably also need to install Pygmentize as well. See the documentation of minted.

4. \usepackage{unicode-math}
This allows you to write unicode characters within math equations.

5. Have fun with it:
Here is a minimal working example:

\documentclass[12pt,a4paper]{scrartcl}

\usepackage{fontspec}
\usepackage{polyglossia}
\setmonofont{DejaVu Sans Mono}[Scale=MatchLowercase]
\usepackage{minted}

\usepackage{latexsym,exscale,stmaryrd,amsmath,amssymb}
\usepackage{unicode-math}

\begin{document}
\begin{minted}{julia}
function f(τ)
return π * τ
end
\end{minted}

\begin{minted}{julia}
∀ ε > 0 ∃ δ > 0 : ∀x,y ∈ ℝ : ||x-y|| < ε ⇒ ||f(x) - f(y)|| < δ
\end{minted}

$∀ ε > 0 ∃ δ > 0 : ∀x,y ∈ ℝ : ||x-y|| < ε ⇒ ||f(x) - f(y)|| < δ$
\end{document}

Note on entering greek letters:
If you are like me and don’t use the Caps-lock key very often.
Linux allows you to reprogram that key. You can make it change the layout to greek while the key is pressed.
So caps-lock + a creates an α.

Have fun with it!
Best,
Jonas

13 Likes

This post seems to be incomplete.

Yes, I’m sorry about that.
I must have hit a the wrong button while typing.

\documentclass[a4paper,12pt]{article}
\usepackage[utf8x]{inputenc}
\usepackage{textgreek}
\usepackage{minted}
\begin{document}
\begin{minted}{julia}
foo(α, β) = α ≤ β
\end{minted}
\end{document}


renders fine.

Interesting. I looked at the documentation of textgreek and didn’t find where it says that you can directly enter
greek letters.

It does still have the downside of not allowing all of unicode and not being available in equations,
as the package unicode_math is only compatible with LuaLateX and XeTeX.

Your suggestion might be easier though, if one is happy without those extras.

I would still prefer LuaTeX, but when cooperating with people on documents I have learned not to take a working LuaTeX for granted. So making pdflatex work is IMO beneficial, even if it involves a bit of effort.

Thanks for highlighting minted — it provides a very nice rendering.

Thanks for posting this. Also, if there are any symbols which don’t show, you can see the responses to my question which show how to replace unicode symbols with latex ones (where required)

I can add to this old conversation that a nice pygements lexer for the julia language is provided here

use this together with minted, it’s better than the one bundled with pygments.

I further made a highlighting scheme trying to get it as close as possible to the default in atom, atomone, it’s available here

8 Likes

Another point to consider is that arXiv compiles with pdflatex and not lualatex.

5 Likes

I actually literally the other week ran into this exact issue because I wanted Julia code in my paper, and emailed arXiv who said they are willing to make exceptions and just accept compiled PDF’s, which you can do with lualatex or whatever you’d like. Although fwiw I actually went with a different solution which is just compiling the Julia code separately on my own, then \includegraphics’ing it.

One more thing to consider about the minted package is that in order for it to work properly with latexmk the \$out_dir cannot be set to anything but ".". I had this issue and it took some time to figure out what exactly went wrong.