4 min read
LaTeX Setup for VS Code

Set up your LaTeX workflow in Visual Studio Code

Demonstration

Demonstration

Install LaTeX\LaTeX

Follow the instructions from Get LaTeX - Mac OS, Windows, Linux and install LaTeX\LaTeX for your operating system.

Install latexindent

Download and install latexindent for formatting your LaTeX\LaTeX code. Note that on Mac OS, latexindent comes bundled with the MacTeX Distribution, however you will still need to download the following packages:

brew install perl
brew install cpanm

cpanm YAML::Tiny
cpanm File::HomeDir

Install LaTeX Workshop

We are finally ready to install the VS Code extension which will make writing our LaTeX\LaTeX code a breeze. Install the LaTeX Workshop extension for VS Code, then set the following settings in your User Settings (JSON) file:

{
  "workbench.editorAssociations": {
    "*.pdf": "latex-workshop-pdf-hook"
  },
  "latex-workshop.latex.recipe.default": "latexmk (lualatex)",
  "latex-workshop.formatting.latex": "latexindent",
  "latex-workshop.latex.autoClean.run": "onBuilt",
  "latex-workshop.view.pdf.invertMode.enabled": "auto",
  "latex-workshop.view.pdf.invert": 0.85
}

Let us go through these settings one by one to understand what is happening.

  1. This setting associates PDF files to be viewed with the internal PDF viewer of the LaTeX Workshop extension. This is important because the internal PDF viewer of this extension supports SyncTeX, which allows for synchronisation between the document and the source code, to allow for features such as forward/inverse search.
{
  "workbench.editorAssociations": {
    "*.pdf": "latex-workshop-pdf-hook"
  }
}
  1. This setting specifies that we should use the LuaTeX typesetting engine, which is the most modern and flexible.
{
  "latex-workshop.latex.recipe.default": "latexmk (lualatex)"
}
  1. This setting specifies our LaTeX\LaTeX formatter to be latexindent, which we installed previously.
{
  "latex-workshop.formatting.latex": "latexindent"
}
  1. This setting will clean up any auxiliary files created after building, which will help to keep our workspace a bit cleaner.
{
  "latex-workshop.latex.autoClean.run": "onBuilt"
}
  1. Finally, these settings will enable dark mode for our PDF viewer, if a dark theme is selected in VS Code.
{
  "latex-workshop.view.pdf.invertMode.enabled": "auto",
  "latex-workshop.view.pdf.invert": 0.85
}

Feel free to modify any of these options, and take a look at the LaTeX Workshop wiki to see all available settings.

Now, let us create the following snippet for LaTeX\LaTeX files, to easily add inline math:

{
  "Inline Math": {
    "prefix": "ml",
    "body": ["$$0$"],
    "description": "Insert inline math delimiters"
  }
}

How To Use

  • Open a LaTeX\LaTeX file and compile with ⌘ + ⌥ + B (or Ctrl + Alt + B on Windows/Linux).
  • Press ⌘ + ⌥ + V (or Ctrl + Alt + V on Windows/Linux) to view the generated PDF file using the internal PDF viewer.
  • To perform a forward search, press ⌘ + ⌥ + J (or Ctrl + Alt + J on Windows/Linux).
  • To perform an inverse search, press ⌘ + Left-click (or Ctrl + Left-click on Windows/Linux) in the internal PDF viewer.
  • View the Snippets and shortcuts section of the LaTeX Workshop wiki for a list of useful snippets and shortcuts.
  • The Playing with environments section of the wiki shows how to navigate, select, and manipulate environments.
  • (Optional but recommended) Use the Vim extension for Vim keybindings, which allow for quicker manipulation of your code.