Comprehensive setup for including Unicode Julia code in LaTeX document

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 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 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 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. But it has no example of a recipe.

This 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,

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

{
    "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.

2 Likes

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’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.

2 Likes

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

xelatex JuliaProgramming.tex --shell-escape

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

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?

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:

Σ(j^2 for j ∈ 1:5)
1 Like

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, 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 if you’re curious (not sure I recommend using it, just potentially borrowing some ideas).

2 Likes