2 minute read

In this post, we see how to setup VSCode to compile your Latex documents. DevContainer is necessary to avoid installing the Latex compiler directly on your host, but inside a container.

Devcontainer Initialization

Prepare a Dockerfile containing the Latex compiler. For simplicity, we are going to install the texlive-full package. Copy the below Dockerfile into the .devcontainer folder.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ARG BASE_IMAGE="ubuntu:24.04"
FROM ${BASE_IMAGE}

RUN <<EOF
    apt update
    apt install -y wget git make texlive-full
EOF

# additional packages
RUN <<EOF
    apt update
    # used to convert svg to pdf while rendering svg
    apt install -y inkscape
EOF

USER 1000

This image is available on DockerHub as maluz/latex-vscode.

Create the .devcontainer/devcontainer.json file with the following content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{
    "name": "vscode-multi-layer-assurance",
    // Replace "build" section with the "image" as commented if you want to use
    // the already available Docker image
    // "image": "maluz/latex-vscode",
    "build": {
        "dockerfile": "Dockerfile"
    },
    "customizations": {
        "vscode": {
            "extensions": ["james-yu.latex-workshop"],
            "settings": {
                "latex-workshop.latex.autoBuild.run": "onSave",
                "latex-workshop.latex.outDir": "output",
                "latex-workshop.latex.recipes": [
                    {
                        "name": "latexmk",
                        "tools": [
                            "latexmk"
                        ]
                    }
                ],
                "latex-workshop.latex.tools": [
                    {
                        "name": "latexmk",
                        "command": "latexmk",
                        "args": [
                            "-cd",
                            "-synctex=1",
                            "-interaction=nonstopmode",
                            "-file-line-error",
                            "-shell-escape", // for svg to pdf conversion
                            "-lualatex",
                            "-outdir=%OUTDIR%",
                            "%DIR%/main.tex"
                        ]
                    }
                ]
            }
        }
    }
}

When opened in a container, it will install the Latex Workshop extension and overwrite the default settings to:

  • Use the lualatex compiler
  • Copy the output files into the output folder
  • Re-compile every time a file is tex file saved

User Settings

In the devcontainer configuration, I have specified the settings option, which includes the directives useful to compile the Latex document and generate the PDF. This section is overridden by the .vscode/settings.json file, which is supposed to be project specific (as it is usually versioned), but it also contains preference-based options, such as auto-build the pdf on document save.

For this reason, a common practice is to keep only the settings.json file, removing the same settings in the devcontainer section, but rename that file to something like settings.json.template. This file can then be versioned, but each user should create its own settings.json starting from the template and customize the necessary options. The user-specific settings.json should not be versioned, so add it to .gitignore.

Comments