1. VSCode Config LaTeX (Mac)

I have been using Atom + MacTex + Skim for a long time. Recently, more and more of my friends advertised me about VSCode. Since I happened to reinstall my Macbook, I would like to give VSCode a try. The first task for me was to setup LaTeX environment. Similar as before, I again wanted to use MacTex + Skim. Fortunately, there were many tutorials. However, most of them were for an old version of VSCode, and their ways to install MacTex and Skim may not be the same as mine. As a result, I had to struggle for a while. Luckly, I made it. Here is my tutorial.

1.1. NOTE

  • Don’t email me about putting any third-party links on my website. I cannot (limited by the institution’s guideline) and I don’t have any plan to commertialize this website (or the one on Github.io). Thank you.

  • (bug fixed) There was a small bug of Skim such that you cannot open the pdf using its script properly. For VSCode users, nothing happened when you choose View LaTeX documnet in External viewer. I confirm that the bug has been fixed in version 1.6.7.(02/09/2022)

1.2. Preparation

Of course, you need to have VSCode, MacTex and Skim.

1.2.1. VSCode

The only way I know to install VSCode is via its offical website.

VSCode

If you want to launch it from the terminal, follow the official guide Open VSCode in terminal

1.2.2. MacTex

MacTex includes a LaTeX compiler. There are two ways to install:

1.2.2.1. Method 1: From installer (easy)

Use the standalone installer downloaded from MacTEX The installer is large (~4.4G), so be prepared. The installation is very stand. For most cases, simply clicking next all the way till you see finish.

1.2.2.2. Method 2: Via brew.

If you don’t have brew, you need to install it by following Homebrew

Then, in the terminal, execute

brew cask install mactex

1.2.3. Skim

Skim is a light-weight pdf viewer. Comparing with other pdf readers, such as Adobe Reader, Skim enables you to jump from the specific PDF paragraph to the corresponding codes in LaTeX. That is the primary reason for using Skim.

There are also two ways to install:

1.2.3.1. Method 1: From installer (easy)

Download the installer from Skim and then install.

1.2.3.2. Method 2: Via brew

If you don’t have brew, you need to install it by following Homebrew

Then, in the terminal, execute

brew cask install skim

1.3. Configure VSCode

Now, we can configure VSCode.

1.3.1. Step 1: Install LaTeX Workshop

In the VSCode, click the “Extension” button on the left and search for “LaTeX Workshop”. Install the extension.

../../../_images/install_LaTeX-Workshop-Extension.png

1.3.2. Step 2: Config The Skim

Choose any pdf file and open with Skim. In the menu bar (at the very top) choose Skim->Preferences->Sync. Check “Check for file changes” and “Reload automatically”. Choose “Visual Studio Code” at the dropdown menu. We check the two functions because we want Skim to automatically show the latest pdf when the MacTex generates the latest one.

../../../_images/skim_config.png

1.3.3. Step 3: Config LaTeX Setting

(Updated) In VSCode, press “cmd+shift+p”, type and choose “Preferences: Open Settings(JSON)”

../../../_images/open_json_settings.png

If you install Skim via installer, add the following code to the file (inside “{}” but outside “[latex]”: {})

"latex-workshop.view.pdf.viewer": "external",
"latex-workshop.view.pdf.external.viewer.command": "/Applications/Skim.app/Contents/SharedSupport/displayline",
"latex-workshop.view.pdf.external.viewer.args": [
    "0",
    "%PDF%"
],
"latex-workshop.view.pdf.external.synctex.command": "/Applications/Skim.app/Contents/SharedSupport/displayline",
"latex-workshop.view.pdf.external.synctex.args": [
    "-r",
    "%LINE%",
    "%PDF%",
    "%TEX%"
],
"latex-workshop.latex.clean.fileTypes": [
    "*.aux",
    "*.bbl",
    "*.blg",
    "*.idx",
    "*.ind",
    "*.lof",
    "*.lot",
    "*.out",
    "*.toc",
    "*.acn",
    "*.acr",
    "*.alg",
    "*.glg",
    "*.glo",
    "*.gls",
    "*.ist",
    "*.fls",
    "*.log",
    "*.fdb_latexmk",
    "*.snm",
    "*.nav"
],

If you install Skim via brew, add the following code

"latex-workshop.view.pdf.viewer": "external",
"latex-workshop.view.pdf.external.viewer.command": "/usr/local/bin/displayline",
"latex-workshop.view.pdf.external.viewer.args": [
    "0",
    "%PDF%"
],
"latex-workshop.view.pdf.external.synctex.command": "/usr/local/bin/displayline",
"latex-workshop.view.pdf.external.synctex.args": [
    "-r",
    "%LINE%",
    "%PDF%",
    "%TEX%"
],
"latex-workshop.latex.clean.fileTypes": [
    "*.aux",
    "*.bbl",
    "*.blg",
    "*.idx",
    "*.ind",
    "*.lof",
    "*.lot",
    "*.out",
    "*.toc",
    "*.acn",
    "*.acr",
    "*.alg",
    "*.glg",
    "*.glo",
    "*.gls",
    "*.ist",
    "*.fls",
    "*.log",
    "*.fdb_latexmk",
    "*.snm",
    "*.nav"
],

“latex-workshop.view.pdf.external.viewer.command” and “latex-workshop.view.pdf.external.viewer.args” are to link the external pdfview to Skim. “latex-workshop.view.pdf.external.synctex.command” and “latex-workshop.view.pdf.external.synctex.args” are to enable the jump back and forth function between pdf and the corresponding tex code. Arguments in “latex-workshop.latex.clean.fileTypes” indicate what type of files will be deleted when we clean the project folder.

I include a screenshot of my json file here for reference

../../../_images/example_of_json_settings.png

1.3.4. Step 4: Compile and Preview

For quick test, I provide you with a piece of very simple latex script below.

Copy and paste the source code into a new LaTeX source file, press “cmd+option+b” to compile, and click the “View PDF Latex file” button on the top-right corner of the VSCode. You can also select “LaTeX Workshop: View LaTeX pdf in external viewer” in the dropdown menu after pressing “cmd+option+p”.

After that, each time you save the latex source code, LaTeX Workshop will compile and then generate the document for you. Then, Skim will detect the changes of pdf file and refreshes the view.

\documentclass{article}

\usepackage{amsmath,amsthm,amsfonts}                % AMS Math
\usepackage{thmtools}                                       % Theorem Tools
\usepackage{bm}                                             % Bold Math

\title{Another Nobel Prize Level Observation}
\author{Yu Wang (Don't Tell Others Who I Am)}
\date{April 2022}

\begin{document}

\maketitle

\section{Conclusion}
After years of experiments, we provide that the widely accepted Ohm's law (\ref{eq: thesis contribution 1}) is correct only if we did the measurement correct. We think this is a breakthrough in the world of Electric Engineering, and this observation deserves a Nobel Prize.
\begin{align}
    V = IR \label{eq: thesis contribution 1}
\end{align}

Please keep this secret between you and me.

\end{document}
../../../_images/preview_step1.png ../../../_images/preview_step2.png

1.3.5. Step 5: Jump From Skim to VS Code

In the Skim, “cmd+shift+left click” a certain line, a figure, or any thing you want, you will be directed to the corresponding latex code in VSCode.