Skip to content

6. Latex Environment

Use Visual Studio Code (VS Code) with the LaTeX Workshop extension to create and edit LaTeX documents. VS Code is a popular, flexible text editor that can be configured as a LaTeX IDE with the help of extensions like LaTeX Workshop.

1. Latex Workshop Extension

1.1 Steps

  1. Install LaTeX Distribution:

    • Before setting up LaTeX Workshop, you’ll need to install a LaTeX distribution on your system:
  2. Install VS Code:

  3. Install LaTeX Workshop Extension:

    • Open VS Code.
    • Go to the Extensions view (Ctrl+Shift+X or Cmd+Shift+X on macOS).
    • Search for LaTeX Workshop.
    • Click “Install”.
  4. Configuration (Optional):

    • LaTeX Workshop should work out of the box for most users, but you can configure it further if needed.
    • You can adjust the settings by going to File > Preferences > Settings and searching for “LaTeX Workshop” to customize things like compiler settings, output directory, etc. Latex Di

    The only requirement is a compatible LaTeX distribution in the system PATH. For example, TeX Live. It is strongly recommend TeX Live. Other possible choices are:

  • TinyTeX as a lighter-weight distribution based on TeX Live.

    The TinyTeX distributions contain only a small subset of the 4000+ packages downloaded by TeX Live. Additional packages can be installed with tlmgr install <pkgname>.

  • MiKTeX is another lightweight distribution with a convenient automatic on-demand package install.

  • Other useful tools:

    • Optional: ChkTeX to lint LaTeX projects.
    • Optional: latexindent.pl for formatting support if it is not provided by your LaTeX distribution. You also have to install a few standard Perl modules. See the official document.

1.2 Key Features

  1. Integrated PDF Viewer:

    • LaTeX Workshop comes with a built-in PDF viewer that can display your compiled PDF within VS Code, allowing for forward and inverse search (i.e., syncing between the source code and PDF).
  2. Auto-build and Preview:

    • The extension can automatically compile your LaTeX files on save, or you can manually trigger the build with a command.
    • It also supports real-time preview, which updates as you edit your LaTeX code.
  3. Syntax Highlighting:

    • VS Code with LaTeX Workshop provides syntax highlighting for LaTeX, making your code easier to read and troubleshoot.
  4. Snippets and Auto-completion:

    • LaTeX Workshop offers useful code snippets for frequently used LaTeX commands and environments.
    • It also provides auto-completion for LaTeX commands, environments, labels, and citations.
  5. Error Detection:

    • The extension helps detect compilation errors and highlights them in the editor, making it easier to find and fix issues.
  6. Bibliography Support:

    • LaTeX Workshop integrates with BibTeX and Biber for managing citations and bibliographies.
  7. Multi-file Project Support:

    • If you’re working on large projects that involve multiple .tex files, LaTeX Workshop provides good support for handling multi-file documents, with proper reference linking.
Play

Features

1.3 Pros

  • Customizable: VS Code is highly customizable, and you can install additional extensions or tweak settings to suit your LaTeX workflow.
  • All-in-one Editor: You can use VS Code not only for LaTeX but also for coding in other languages, making it a great all-purpose editor.
  • Cross-platform: Like LaTeX Workshop, VS Code is available on Windows, macOS, and Linux.
  • Rich feature set: With LaTeX Workshop, you get an IDE-like experience with built-in PDF viewing, error handling, auto-completion, and more.

1.4 Cons

  • Initial Setup: For new users, setting up VS Code with LaTeX Workshop may seem more complex than using a dedicated LaTeX editor like TeXMaker.
  • No Real-time Collaboration: Unlike Overleaf, VS Code doesn’t offer built-in real-time collaboration, though you can use Git for version control.
Reference

2. Latex Workshop Settings

These are important settings for Latex Workshop Extension.

// "latex-workshop.latex.autoClean.run": "onBuild", // Perform clean before the built
"latex-workshop.latex.tools": [ // These values are default
{
"name": "latexmk",
"command": "latexmk",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-pdf",
"-f",
"-outdir=%OUTDIR%",
"%DOC%"
],
"env": {}
},
{
"name": "lualatexmk",
"command": "latexmk",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-lualatex",
"-f",
"-outdir=%OUTDIR%",
"%DOC%"
],
"env": {}
},
{
"name": "xelatexmk",
"command": "latexmk",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-xelatex",
"-outdir=%OUTDIR%",
"%DOC%"
],
"env": {}
},
{
"name": "latexmk_rconly",
"command": "latexmk",
"args": [
"%DOC%"
],
"env": {}
},
{
"name": "pdflatex",
"command": "pdflatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"%DOC%"
],
"env": {}
},
{
"name": "bibtex",
"command": "bibtex",
"args": [
"%DOCFILE%"
],
"env": {}
},
{
"name": "rnw2tex",
"command": "Rscript",
"args": [
"-e",
"knitr::opts_knit$set(concordance = TRUE); knitr::knit('%DOCFILE_EXT%')"
],
"env": {}
},
{
"name": "jnw2tex",
"command": "julia",
"args": [
"-e",
"using Weave; weave(\"%DOC_EXT%\", doctype=\"tex\")"
],
"env": {}
},
{
"name": "jnw2texminted",
"command": "julia",
"args": [
"-e",
"using Weave; weave(\"%DOC_EXT%\", doctype=\"texminted\")"
],
"env": {}
},
{
"name": "pnw2tex",
"command": "pweave",
"args": [
"-f",
"tex",
"%DOC_EXT%"
],
"env": {}
},
{
"name": "pnw2texminted",
"command": "pweave",
"args": [
"-f",
"texminted",
"%DOC_EXT%"
],
"env": {}
},
{
"name": "tectonic",
"command": "tectonic",
"args": [
"--synctex",
"--keep-logs",
"%DOC%.tex"
],
"env": {}
}
],
"latex-workshop.latex.recipes": [ // These values are default
{
"name": "latexmk",
"tools": [
"latexmk"
]
},
{
"name": "latexmk (latexmkrc)",
"tools": [
"latexmk_rconly"
]
},
{
"name": "latexmk (lualatex)",
"tools": [
"lualatexmk"
]
},
{
"name": "latexmk (xelatex)",
"tools": [
"xelatexmk"
]
},
{
"name": "pdflatex -> bibtex -> pdflatex * 2",
"tools": [
"pdflatex",
"bibtex",
"pdflatex",
"pdflatex"
]
},
{
"name": "Compile Rnw files",
"tools": [
"rnw2tex",
"latexmk"
]
},
{
"name": "Compile Jnw files",
"tools": [
"jnw2tex",
"latexmk"
]
},
{
"name": "Compile Pnw files",
"tools": [
"pnw2tex",
"latexmk"
]
},
{
"name": "tectonic",
"tools": [
"tectonic"
]
}
],
"latex-workshop.latex.autoBuild.run": "onSave", // Automatically builds on save
"latex-workshop.latex.build.forceRecipeUsage": true, // Forces the use of LuaLaTeX recipe
"latex-workshop.latex.recipe.default": "latexmk (lualatex)" // Set LuaLaTeX as default