# LaTeX in VSCode

Do you want to write LaTeX in VSCode?

When we talk about LaTeX editing, people would mention TeXstudio or WinEdt with MiKTeX or TeX Live. For a long time, TeXstudio was my one and only editor. But as time goes by, I couldn't stand its interface anymore, so I switched to the open-source, feature-rich VSCode.

This article will show you how to write LaTeX documents using VSCode and live-preview its PDF output inside VSCode, as well as use SyncTeX to sync cursors between input and output of the LaTeX document.

Note: This article demonstrates the process in Windows, but you can use the same tools on Linux with just a few small modifications.

Contents

## Step 1 : Install TeX Live

We prefer TeX Live over other distribution like MiKTeX or W32TeX, not just because it has more secure default values and well-maintained but also because it contains everything a regular user need when it comes to a TeX distribution. But you can use any other TeX distribution to your liking.

We recommend starting the TeX Live installation by downloading install-tl-windows.exe for Windows (~20mb), or install-tl-unx.tar.gz (~5mb) for everything else. Detailed installation steps is available on TeX Live homepage.

Please note that the installation downloads a hefty amount of data from the internet, so it takes from 20 minutes to 3 hours to complete.

## Step 2 : Install VSCode LaTeX extension

LaTeX-Workshop is the only extension you need for LaTeX typesetting with VSCode.

In order to install LaTeX-Workshop, open up VSCode and go to the Extension tab to search for it. If you didn't know how to do that, see our guide on how to install extensions in VSCode.

LaTeX-Workshop provides a smooth LaTeX experience, thanks to its long list of features :

• Build LaTeX (including BibTeX) to PDF automatically on save.
• Preview PDF in realtime side-by-side in VS Code or a PDF viewer.
• Direct and reverse SyncTeX. You can jump between any location in .tex source file and the corresponding PDF file and vice versa.
• Intellisense integrated, autocomplete for bibliography keys (\cite{}) and labels (\ref{}).
• LaTeX errors and warnings shown inside VS Code.
• Keyboard combination for quick text formatting.

For more advanced features you can install LaTeX Utilities, though it is no longer actively maintained.

## Step 3 : Configure LaTeX commands in VSCode

The first thing we need to do is set up LaTeX command line tools to work with LaTeX-Workshop.

To do that, open Command Palette and find Open Settings (JSON) and put the following snippet into settings.json.

"latex-workshop.latex.tools": [
// Compilation tools and commands
{
"name": "xelatex",
"command": "xelatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-pdf",
"%DOCFILE%"
]
},
{
"name": "pdflatex",
"command": "pdflatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOCFILE%"
]
},
{
"name": "bibtex",
"command": "bibtex",
"args": [
"%DOCFILE%"
]
}
],

By default, LaTeX Workshop uses latexmk to do the heavy lifting work. But we recommends using XeTeX, as it offer Unicode and OpenType supports out of the box.

There are other alternatives as well, but with XeTeX, almost everything "just work" without any further configuration.

What we've just put in the settings are the latex tools and the parameters they need so that LaTeX-Workshop can use them to run bibtex, pdflatex and xetex with the right arguments every time the .tex file changes.

Note : Use %DOCFILE% instead of %DOC% in the configuration to enable support for Unicode file paths.

## Step 4: Adds LaTeX compile chain into VSCode

The next thing we need to configure is the compile chain (Latex-Workshop "recipe"). The first recipe is the default compiler we've chosen (xelatex)

"latex-workshop.latex.recipes": [
{
"name": "xelatex 🔃",
"tools": [
"xelatex"
],
},
{
"name": "pdflatex 🔃",
"tools": [
"pdflatex"
]
},
{
"name": "xelatex➞bibtex➞xelatex.2x 🔃",
"tools": [
"xelatex",
"bibtex",
"xelatex",
"xelatex"
]
},
{
"name": "pdflatex➞bibtex➞pdflatex.2x 🔃",
"tools": [
"pdflatex",
"bibtex",
"pdflatex",
"pdflatex"
]
}
],

If you regularly need bibtex, you can either choose one of these workflow:

1. When compiling, click the tick in the lower left corner of the VSCode interface, click "Build LaTeX project", select the recipe with bib (named xelatex➞bibtex➞xelatex.2x 🔃 in our configuration).
2. Replace xelatex 🔃 recipe with xelatex➞bibtex➞xelatex.2x 🔃 so that xelatex recipe becomes the default, then you can trigger it with the hotkeyto the first key Put the recipe with bib in the first place, and it can be compiled as the default recipe, or it can, but because the number of compilations is more, the speed will be slower;
3. Add %!BIB program = bibtex at the beginning of the document, this is a special command specifies that the document needs to be processed with bibtex.

Similarly, to require the document to be processed by pdflatex, add the following line to the top of your document.

%!TEX program = pdflatex

## Step 5 : Preview LaTeX PDF in VSCode

The next thing we're going to do is setting up a PDF viewer to live-preview the compiled PDF file.

You can use the built-in PDF.js PDF reader to preview the output PDF file by clicking the Ope Preview button on the upper right corner of the file.

Any PDF file opened this is way is monitored by a file watcher to be automatically reloaded when it changes on disk or after a successful build.

## Step 6 : Sync cursors with SyncTeX

If you use the built-in PDF.js-based reader of LaTeX-Workshop, javascript built-in version of SyncTeX is used by default.

Forward/Direct SyncTeX (tex to pdf) can either be activated by selecting 'Navigate, select, and edit' > 'SyncTeX from cursor' in the side bar, or by the default shortcut Ctrl + Alt + J.

Backward/Reverse SyncTeX (pdf to tex) is automatically activated each time you select an element in the PDF preview.

Although not officially supported, you can also configure PDF preview and SyncTeX to work with any other PDF reader, such as SumatraPDF or Adobe Acrobat DC. This configure is particularly useful if you're using more than one monitor and don't want to be constrained to a single window by VS Code.

We hope that this article helped you learn how to write LaTeX documents using VSCode and live-preview its PDF output inside VSCode as well as use SyncTeX to sync cursors between input and output of the LaTeX document. You may also want to see our guide on Spell Check Extensions for VSCode or how to bring ZSH terminal to VSCode.

Click to rate this post!
[Total: 39 Average: 4.9]

### 1 thought on “LaTeX in VSCode”

1. Marc

Please do not use %!TEX program = pdflatex with —shell-escape and/or minted. It really messes the whole thing up. Took me three days to debug this, why even use it if you have it in your recipe?