# Comprehensive setup for including Unicode Julia code in LaTeX document

**URL:** <https://discourse.julialang.org/t/comprehensive-setup-for-including-unicode-julia-code-in-latex-document/26553>\
**Category:** Community\
**Tags:** question\
**Created:** [July 19, 2019, 4:56pm UTC](https://discourse.julialang.org/t/comprehensive-setup-for-including-unicode-julia-code-in-latex-document/26553 "2019-07-19T16:56:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [July 19, 2019, 4:56pm UTC](https://discourse.julialang.org/t/comprehensive-setup-for-including-unicode-julia-code-in-latex-document/26553/1 "2019-07-19T16:56:01Z")

</div>

TL; DR: What is the complete setup for VSCode to run XeLaTeX or LuaLaTeX, with working examples using unicode characters with minted? I’ve looked everywhere, finding the `latex-workshop.latex.tools` in one spot, the `latex-workshop.latex.recipes` in another spot, then example `.tex` files elsewhere, and they don’t play nicely together. I am so confused.

So, I am new to the world of programming and spend more than half my time lost trying to understand what on earth everyone is talking about.

In [this](https://tex.stackexchange.com/a/422090/141582) answer to a Stack Exchange question, Brad gives an example without the need to “define” new unicode characters (as with another answer to that question) because it has a huge variety of them.

In [this](https://github.com/James-Yu/LaTeX-Workshop/issues/209) post, the creator of LaTeX-Workshop discourages the use of the magic comment which is used in the Stack Exchange answer aforementioned, so I’ve deleted it. I also have no idea where the `latex-workshop.latex.toolchain` is meant to be copied-and-pasted (or edited).

I’ve learned from [here](https://tex.stackexchange.com/a/290494/141582) that `minted` is better than `listings` because the former takes in the syntax highlighting and formatting rules from `pygmentize`, which I have installed through the python installer.

I’ve obtained an example of defining the `xelatex` tool from [here](https://stackoverflow.com/a/56109129/9053825). But it has no example of a recipe.

[This](https://discourse.julialang.org/t/typesetting-julia-code-in-latex-documents-unicode/10176) guy’s example isn’t specific enough for me, i.e. VSCode settings are unclear to me.

The `.tex` file I have is as follows,

```nohighlight
\documentclass[12pt]{article}

\usepackage[letterpaper]{geometry}
\usepackage{fontspec,unicode-math}
\usepackage{xunicode}
\usepackage{minted}

\setmonofont{FreeMono}

\begin{document}

\section{Example Code}
Hello, here is some code.

\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}

```

With the following as my `settings.json` file for VSCode.

```json
{
    "latex-workshop.latex.recipes": [
        {
            "name": "latexmk",
            "tools": "latexmk"
        }
    ],
    "latex-workshop.latex.tools": [
        {
            "name": "latexmk",
            "command": "latexmk",
            "args": [
                "-xelatex",
                "-synctex=1",
                "-interaction=nonstopmode",
                "-file-line-error",
                "%DOC%"
            ]
        }
    ]
}

```

Are these sufficient for a successful run? It doesn’t run successfully for me.

---

<div class="post-metadata">

**Author:** ![sswatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sswatson/32/5135_2.png) [@sswatson](https://discourse.julialang.org/u/sswatson)\
**Post date:** [July 19, 2019, 7:39pm UTC](https://discourse.julialang.org/t/comprehensive-setup-for-including-unicode-julia-code-in-latex-document/26553/2 "2019-07-19T19:39:09Z")

</div>

There are two questions here: (1) how to get minted working with Unicode characters, and (2) how to get VS Code configured to run the correct compilation commands. You probably want to compile your document directly from the command line at first, because that will help you determine whether it’s a TeX issue or an issue with the VS Code extension.

In case it’s helpful, [here](https://www.overleaf.com/read/bwzhsfcdjmkb)’s a working example with your code. Note in particular the latexmkrc file, which specifies that xelatex should be used with the shell escape option.

Follow-up: it looks to me like the only thing missing from your VS Code build recipe is the shell-escape option. Try adding `"-shell-escape"` to the list of `args`.

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [July 19, 2019, 11:23pm UTC](https://discourse.julialang.org/t/comprehensive-setup-for-including-unicode-julia-code-in-latex-document/26553/3 "2019-07-19T23:23:47Z")

</div>

Thanks @sswatson. Nice to see your copy of my example in Overleaf works.

I forgot to add in my long list of discoveries in my question that the “–shell-escape” argument had to be included too, and that somewhere else, someone said it was unfortunate that we had to use it, thanks for reminding me. I’ve added it to the list of arguments, but it didn’t work. Two dashes first, right? (i.e. I think I’ve seen in places `--shell-escape` instead of `-shell-escape`.)

Also, running it in the command line makes sense. I tried running

```julia
xelatex JuliaProgramming.tex --shell-escape

```

But that gave me a `"very "^5` long output. Amongst the output, I get lines that say:

```julia
Sorry, but miktex-makemf did not succeed.

! Package fontspec Error: The font "FreeMono" cannot be found.

`BI' can't be a subfont created by hbf2gf

The log file hopefully contains the information to get MiKTeX going again:

C:\<Path>\Local\MiKTeX\2.9\miktex\log\miktex-maketfm.log

```

I could check the log file. I could try to install the font FreeMono. But I’ve been exploring rabbit holes for too long and I’m tired of it now.

Edit: What does the `latexmkrc` file do? Does it have an extension?

---

<div class="post-metadata">

**Author:** ![JackDevine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jackdevine/32/1048_2.png) [@JackDevine](https://discourse.julialang.org/u/JackDevine)\
**Post date:** [July 20, 2019, 12:01am UTC](https://discourse.julialang.org/t/comprehensive-setup-for-including-unicode-julia-code-in-latex-document/26553/4 "2019-07-20T00:01:49Z")

</div>

Slightly off topic, but you can drop the list comprehension in your `sum` example and do the sum with a generator. That way Julia won’t allocate an array to do the sum:

```julia
Σ(j^2 for j ∈ 1:5)

```

---

<div class="post-metadata">

**Author:** ![sswatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sswatson/32/5135_2.png) [@sswatson](https://discourse.julialang.org/u/sswatson)\
**Post date:** [July 20, 2019, 1:58am UTC](https://discourse.julialang.org/t/comprehensive-setup-for-including-unicode-julia-code-in-latex-document/26553/5 "2019-07-20T01:58:44Z")

</div>

Minted has to call out to pygments to do syntax highlighting; I doubt that that will ever be possible directly in TeX. So the shell escape requirement is just a consequence of what you’re trying to do. I’ve made my peace with it. As for the number of dashes, the one-dash version is for when you’re supplying an argument to `latexmk`, while the two-dash version is for `xelatex`.

`latexmkrc` configures `latexmk`, and it does not have an extension. You don’t need it if you’re going to directly call `xelatex --shell-escape JuliaProgramming.tex`; it’s necessary in Overleaf because that is the only way you can customize the compilation command in that system. I thought it might also be useful in your case because you’re using `latexmk` in your VS Code build recipes.

I have typeset a lot of Julia code using Minted, and I ran into font problems several times with slightly exotic glyphs (particularly those involving accents). Eventually I realized that many of these issues could be headed off by using Google’s [Noto Sans Mono](https://www.google.com/get/noto/), which I find to be much preferable to alternatives that I explored, both aesthetically and with respect to codepoint coverage.

I feel you on the rabbit hole fatigue. I’m grateful to be past that wrangling in my own writing setup. Another issue with Minted that you haven’t even gotten to yet is that you’ll probably want to turn the shell escape off when you haven’t changed any of the code you’re typesetting, because that speeds up compilation considerably. Unfortunately, Minted doesn’t make this particularly easy to do. My solution to that problem, as well as integration with tcolorboxes and some other conveniences, is implemented in [this LaTeX package](https://github.com/sswatson/latex-setup/blob/master/codeboxes.sty) if you’re curious (not sure I recommend using it, just potentially borrowing some ideas).
