# Typesetting Julia Code in Latex Documents (Unicode)

**URL:** <https://discourse.julialang.org/t/typesetting-julia-code-in-latex-documents-unicode/10176>\
**Category:** Community\
**Created:** [April 5, 2018, 2:49pm UTC](https://discourse.julialang.org/t/typesetting-julia-code-in-latex-documents-unicode/10176 "2018-04-05T14:49:00Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![JonasIsensee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasisensee/32/4704_2.png) [@JonasIsensee](https://discourse.julialang.org/u/JonasIsensee)\
**Post date:** [April 5, 2018, 2:49pm UTC](https://discourse.julialang.org/t/typesetting-julia-code-in-latex-documents-unicode/10176/1 "2018-04-05T14:49:00Z")

</div>

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  
Instead of using:  
\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}

![Screenshot%20from%202018-04-05%2016-57-58](https://global.discourse-cdn.com/julialang/original/3X/5/8/5871462ba76f591d1eea80b53915006c6572232e.png)

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

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [April 5, 2018, 2:49pm UTC](https://discourse.julialang.org/t/typesetting-julia-code-in-latex-documents-unicode/10176/2 "2018-04-05T14:49:52Z")

</div>

This post seems to be incomplete.

---

<div class="post-metadata">

**Author:** ![JonasIsensee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasisensee/32/4704_2.png) [@JonasIsensee](https://discourse.julialang.org/u/JonasIsensee)\
**Post date:** [April 5, 2018, 3:34pm UTC](https://discourse.julialang.org/t/typesetting-julia-code-in-latex-documents-unicode/10176/3 "2018-04-05T15:34:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 5, 2018, 4:54pm UTC](https://discourse.julialang.org/t/typesetting-julia-code-in-latex-documents-unicode/10176/4 "2018-04-05T16:54:49Z")

</div>

> [@JonasIsensee](#):
>
> The standard LateX engine pdfLateX is not capable of having Unicode in your .tex file.

I am not sure about this. The MWE

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

```

renders fine.

---

<div class="post-metadata">

**Author:** ![JonasIsensee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasisensee/32/4704_2.png) [@JonasIsensee](https://discourse.julialang.org/u/JonasIsensee)\
**Post date:** [April 5, 2018, 5:53pm UTC](https://discourse.julialang.org/t/typesetting-julia-code-in-latex-documents-unicode/10176/5 "2018-04-05T17:53:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 5, 2018, 5:57pm UTC](https://discourse.julialang.org/t/typesetting-julia-code-in-latex-documents-unicode/10176/6 "2018-04-05T17:57:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [April 5, 2018, 9:34pm UTC](https://discourse.julialang.org/t/typesetting-julia-code-in-latex-documents-unicode/10176/7 "2018-04-05T21:34:19Z")

</div>

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)

> <https://tex.stackexchange.com/questions/422039/best-unicode-font-to-work-with-minted-in-xelatex-lualatex>

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [May 8, 2019, 5:30pm UTC](https://discourse.julialang.org/t/typesetting-julia-code-in-latex-documents-unicode/10176/8 "2019-05-08T17:30:24Z")

</div>

I can add to this old conversation that a nice pygements lexer for the julia language is provided here  
[https://github.com/sisl/pygments-julia](https://github.com/sisl/pygments-julia)  
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  
[https://github.com/baggepinnen/configs/blob/master/scripts/atomone.py](https://github.com/baggepinnen/configs/blob/master/scripts/atomone.py)

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [February 11, 2020, 1:27am UTC](https://discourse.julialang.org/t/typesetting-julia-code-in-latex-documents-unicode/10176/10 "2020-02-11T01:27:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [February 11, 2020, 3:07am UTC](https://discourse.julialang.org/t/typesetting-julia-code-in-latex-documents-unicode/10176/11 "2020-02-11T03:07:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kunzaatko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kunzaatko/32/25153_2.png) [@kunzaatko](https://discourse.julialang.org/u/kunzaatko)\
**Post date:** [May 16, 2021, 9:37pm UTC](https://discourse.julialang.org/t/typesetting-julia-code-in-latex-documents-unicode/10176/12 "2021-05-16T21:37:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tapyu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tapyu/32/37182_2.png) [@tapyu](https://discourse.julialang.org/u/tapyu)\
**Post date:** [December 17, 2022, 5:32pm UTC](https://discourse.julialang.org/t/typesetting-julia-code-in-latex-documents-unicode/10176/13 "2022-12-17T17:32:21Z")

</div>

The codes seem pretty neat for Greek letters. However, it does not work for bold letters

```plaintext
\documentclass{article}
\usepackage{minted}
\usepackage{fontspec}
\usepackage{polyglossia}
\usepackage{unicode-math}

\usepackage{xunicode}
\setmonofont{DejaVu Sans Mono}[Scale=MatchLowercase]

\usepackage{newunicodechar}
\begin{document}
\begin{minted}[breaklines,escapeinside=||,mathescape=true, linenos, numbersep=3pt, gobble=2, frame=lines, fontsize=\small, framesep=2mm]{julia}
    using Convex, SCS, Plots, LaTeXStrings

    N = 30 # N in the set {0, 1, ..., N}
    n = 3 # order of the linear dynamical system
    x_des = [7, 2, -6] # constraint -> 𝐱(N) == x_des
    # model parameters
    𝐀 = [-1 .4 .8; 1 0 0; 0 1 0]
    𝐛 = [1, 0, 0.3]

    𝐗 = Variable(n, N+1) # [𝐱(0) 𝐱(1) ... 𝐱(N)]
    𝐮 = Variable(1, N) # [u(0) u(1) ... u(N-1)]
    f0 = sum(max(abs(𝐮), 2abs(𝐮)-1)) # objective function
    constraints = [
        𝐗[:,2:N+1] == 𝐀*𝐗[:,1:N]+𝐛*𝐮, # recursive equation
        𝐗[:,1] == zeros(n), # initial condition
        𝐗[:,N+1] == x_des, # final condition
    ]
    problem = minimize(f0, constraints)
    solve!(problem, SCS.Optimizer; silent_solver = true)

    fig = plot(vec(𝐮.value), xlabel=L"k", title=L"u(k)", line=:steppre)
    savefig(fig, "figs/4.17.png")
\end{minted}
\end{document}

```

The generated PDF is

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/6/36356c761254c65e81acf372b781f82fd1954130.png)

How can I solve it? I tried to use some of Nerd Font patches, by using

```julia
\setmonofont[Path = /home/tapyu/.local/share/fonts]{Meslo LG M DZ Regular Nerd Font Complete.ttf}

```

but it does not work…

1. I’ve tried with both XeLaTeX and LuaLaTeX engines

---

<div class="post-metadata">

**Author:** ![hersle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hersle/32/211689_2.png) [@hersle](https://discourse.julialang.org/u/hersle)\
**Post date:** [September 28, 2025, 9:15pm UTC](https://discourse.julialang.org/t/typesetting-julia-code-in-latex-documents-unicode/10176/14 "2025-09-28T21:15:59Z")

</div>

I noticed ArXiv now supports the xelatex compiler, which is compatible with unicode, fontspec and so on. Lualatex is not available yet, though. And I assume most journals are still a different story.

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [September 29, 2025, 8:50pm UTC](https://discourse.julialang.org/t/typesetting-julia-code-in-latex-documents-unicode/10176/15 "2025-09-29T20:50:01Z")

</div>

You should try jlcode it works very well : [GitHub - wg030/jlcode: A latex package for displaying Julia code using the listings package. The package supports pdftex, luatex and xetex for compilation.](https://github.com/wg030/jlcode)

They are also responsive on GitHub issues if you have any
