Michiel Kempen

Michiel Kempen

Hi, I'm Michiel, a passionate cloud engineer, full-stack webdeveloper,
and entrepreneur. I love to learn, teach, and build awesome things.
Currently making the cloud accessible at Smoothy.cloud ☁️🚀

Beautiful code snippets in LaTeX using the Minted package and Visual Studio Code #

At the time of writing this article, I am working on my master's thesis. In this context, I needed to put some code snippets in a variety of programming languages in my LaTeX document and add some nice looking syntax highlighting. 🎨✨

After some Googling, I found the LaTeX package Minted, which seemed like a great fit for my needs. I tried to install the package but quickly had to face a few problems. In this article, I report on my learnings and provide a step-by-step guide to properly install and use the Minted package in LaTeX using the LaTeX Workshop extension of Visual Studio Code.

Installing the Minted package #

To use the Minted package in my LaTeX document, the documentation told me to do two things:

  1. First I needed to install the Python syntax highlighter Pygments, which Minted uses behind the scenes to render the LaTeX document. Since I already had a recent version of Python installed on my machine, I could easily install Pygments via the pip package manager using the following command:
    pip3 install Pygments
  2. Next, I needed to import the Minted package in my LaTeX document. This I accomplished by including the following line of code at the top of my LaTeX document:
    \usepackage{minted}

However, when I tried to render my LaTeX document, I got the following errors:

Package minted Error: You must have 'pygmentize' installed to use this package.
Package minted Error: You must invoke LaTeX with the -shell-escape flag.

Right away I started Googling a solution for the first problem. But, after about 30 minutes of debugging, I still hadn't figured out why LaTeX wasn't able to find the Pygments package. So, I put problem 1 aside for a minute and took on problem 2.

To solve problem 2, I had to add the --shell-escape flag to my LaTeX Compiler. Since I use Visual Studio Code with the LaTeX Workshop extension to automatically compile my LaTeX document while writing, I dove into the VS Code settings.

The configuration option in question was hidden under Latex-workshop > Latex: Tools. If you are following along with this tutorial, you can quickly find this setting by searching for @ext:james-yu.latex-workshop tools in your settings page, as shown in the screenshot below.

Projects page

In contrast to many other settings in VS Code, this setting cannot be altered through the UI but requires you to manually edit VS Code's settings.json file. To open this file, click on 'Edit in settings.json'. This should open a JSON file containing key-value pairs that resemble your customized VS Code settings.

The key we are interested in is latex-workshop.latex.tools. Unless you have played with your LaTeX compile commands in the past, this key will not be present in the file. Therefore you have to add it yourself.

As value, I grabbed the default configuration from the LaTeX Workshop documentation and added the required --shell-escape flag to the args list of both the latexmk and pdflatex tool. The final result should look something like this:

"latex-workshop.latex.tools": [
    {
        "name": "latexmk",
        "command": "latexmk",
        "args": [
            "--shell-escape", // added this flag
            "-synctex=1",
            "-interaction=nonstopmode",
            "-file-line-error",
            "-pdf",
            "-outdir=%OUTDIR%",
            "%DOC%"
        ],
        "env": {}
    },
    {
        "name": "pdflatex",
        "command": "pdflatex",
        "args": [
            "--shell-escape", // added this flag
            "-synctex=1",
            "-interaction=nonstopmode",
            "-file-line-error",
            "%DOC%"
        ],
        "env": {}
    },
    {
        "name": "bibtex",
        "command": "bibtex",
        "args": [
            "%DOCFILE%"
        ],
        "env": {}
    }
],

After saving these changes, problem 2 was resolved. But, more importantly, also problem 1 suddenly disappeared and I could finally compile my LaTeX document. 🎉

Using the Minted package #

Once the package was successfully installed on my machine, I started reading through the Minted documentation once again to learn more about how I could tweak the default configuration of the package to better suit my visual preference. The configuration I eventually used in my thesis is the following:

\usepackage{minted}
\setminted[]{
    xleftmargin=.75cm,
    xrightmargin=.75cm,
    frame=single,
    framesep=.25cm,
    linenos,
    tabsize=2,
    breaklines
}

With the configuration in place, I could finally start adding the code-snippets from my experiments to my LaTeX document. As shown in the example below, each code-snippet has to be wrapped in a minted environment to which the language for the syntax highlighting is passed via an argument.

An overview of the supported languages in Minted can be found in this article by Overleaf.

In the example below, we disable the installation of the Prometheus Alertmanager since we will not be using it in our setup and change the password of the admin user in Grafana to \code{secret}.

\begin{minted}{yaml}
defaultRules:
  rules:
    alertmanager: false
alertmanager:
  enabled: false
grafana:
  adminPassword: secret
\end{minted}

As soon as you have your configuration file in place, you can install the Helm chart on your cluster using the following command:

\begin{minted}{bash}
helm install stable/prometheus-operator \
    --name prometheus-operator \
    --namespace monitoring \
    -f values.yaml
\end{minted}

The final result of this piece of LaTeX code is shown in the screenshot below. Nice, isn't it? ✨

Projects page

.dev domains not resolving due to Laravel Valet