To skip the narrative and get the result, check out this repository which contains a ready-to-go project for VS Code for LaTeX using Docker containers and more.

The problem is this: LaTeX is unpleasant to install and requires many modules. Containers are nice at encapsulating that nastiness for us so the objective is to use a container that has LaTeX already installed to compile our project. We will mainly leverage these two components:

  • LaTeX Workshop Extension which comes with container based compilation integration. It also handles the usual syntax highlighting, formatting and automatic compilation when a file has changed.
  • latex-docker is the container which has LaTeX installed. You can use any container you like that exposes pdflatex but there is no point in reinventing the wheel here unless you have specific requirements.

Assuming Docker is installed, we use the .vscode/settings.json file in our workspace to link them together:

{
    "editor.formatOnSave": true,
    "editor.wordWrap": "on",
    "latex-workshop.latex.outDir": "./out",
    "latex-workshop.view.pdf.viewer": "tab",
    "latex-workshop.docker.enabled": true,
    "latex-workshop.docker.image.latex": "ghcr.io/xu-cheng/texlive-full",
}

and that’s it. We are asking the extension to enable docker and use the specified image from the GitHub repository. Here are some things to note:

  • A new container instance will be spawned every time you compile. This is not really an issue as containers are really cheap to spawn and kill. It also helps to ensure every compilation starts the same so as to increase reproducibility.
  • It is shareable. Anyone cloning the project will get the same file thanks to the container based compilation. No missing packages and no version mismatches.
  • The container also has the LaTeX linter which the extension can use. So when you save a file, it first of all formats it such as fixing the alignment of table columns.

I think the ease and simplicity of this setup has definitely given me hope on using LaTeX locally and not feeling desperate to use an online platform such as Overleaf. When used as part of a repository, you get all the nice features like clean version control and continuous integration. If you are keen on the real-time collaborative editing of files which the online platforms offer, then you can still use Live Share from within VS Code.