How to use LaTeX on VS Code?
When MiKTeX is running and LaTeX-Workshop installed, you do not need perl or latexmk.
Building
Just open the command palette in VS Code (Menu: View > Command Palette ...) and type: Latex workshop: build with recipe
, click on it and choose pdflatex->bibtex->pdflatex2 instead of latexmk.
If you want to build directly via command Ctrl + Alt + B, edit your user settings:
Go to Menu in VS Code: File > Preferences > Settings, expand entry Extensions and choose LaTeX. Scroll down to Latex: Recipes and click Edit in settings.json
. Click on the pencil next to "latex-workshop.latex.recipes" in order to copy this entry from DEFAULT USER SETTINGS to USER SETTINGS. In the USER SETTINGS delete the whole entry
{
"name": "latexmk",
"tools": [
"latexmk"
]
},
Save the file USER SETTINGS and afterwards you can build your latex-project via Ctrl + Alt + B.
Pdf-Viewer
In order to start PDF viewer you can also open command palette again and type: Latex workshop: view latex pdf file in vscode tab
.
Setup Synctex
Download https://ctan.org/tex-archive/systems/win32/w32tex/win64/pdftex-w64.tar.xz, unzip it twice (with 7-zip for example) to my\Favourite\location\path
. Open again Settings/LateX (s.o.) scroll to Synctex: Path and fill in my\Favourite\location\path\synctex.exe
.
Now Synctex is available from source to PDF via Ctrl + Alt + J or vice versa from PDF to source via Ctrl + left clicking.
I faced some difficulties in configuring and I decided to do this tutorial. In fact, it would be easier to download MacTex, which is the full version; however, I was not in the mood to occupy 4GB of my SSD with this.
The tutorial is pretty basic anyway, so have patience.
Important: I assume that: (1) BasicTex is already installed on your Mac and LaTex Workshop is installed in the VS Code, and (2) there are not any of the required packages.
Download Packages
Configuring TeX Live Manager
We will use TeX Live Manager to install the packages. It is accessible via tlmgr
command
1) Open Terminal on your Mac
2) Type sudo tlmgr update --self
to update the TeX Live Manager itself
3) Once the process is complete, we will update all packages already installed by BasicTex. To do so, type sudo tlmgr update --all
Installing Packages
Now we will install the necessary packages. They are: latexmk, latexindent, synctext, chktex and texcount. I will not show the installation of all the packages because the process is exactly identical, having only to substitute the name of what I am going to use of example (the chktex) by the others.
1) At the Terminal, type sudo tlmgr install chktex
. Wait for the installation to be completed. Usually the dynamic is as follows:
MacBook-Pro: ~ gabriel $ sudo tlmgr install chktex
Password:
tlmgr: package repository http://ctan.dcc.uchile.cl/systems/texlive/tlnet (not verified: gpg unavailable)
[1/2, ??: ?? / ??: ??] install: chktex.x86_64-darwin [54k]
[2/2, 00: 01/00: 01] install: chktex [10k]
running mktexlsr ...
done running mktexlsr.
tlmgr: package log updated: /usr/local/texlive/2018basic/texmf-var/web2c/tlmgr.log
2) Repeat this for all packages
3) To check where each package is installed, use the which [package_name]
command, for example which chktex
. In my case, the answer comes:
MacBook-Pro-of-Gabriel: ~ gabrielsalgueiro $ which chktex
/Library/TeX/texbin/chktex
Configuring in VS Code
Configuring the paths
1) Open your VS Code and go to the Commands tab (Cmd + Shift + P generally), and open the settings by typing Preference: Open Settings
2) In the window that appears, locate the Extensions
tab. Click and find LaTex
3) Fill in the required fields with the paths discovered in the previous step. Just go down the page you will see where it should fill with the paths. The default is [Package_name]: Path
Generating Files
1) Configured all, go test. Open the .tex file.
2) Build (Cmd + Option + B, by default).
3) View the result (Cmd + Option + V, by default)
3.1) If all goes well, it will ask where you want to view the PDF (browser, in a new tab, or even in an external viewer). Select your option and be happy.
Q. Remember to use a folder that does not have restricted characters. Defining the workspace inside the "Documents" folder solves this problem, for example.
You'll need the latexmk
package from the MikTeX Console and the Perl language interpreter. Since Perl is easier to set up, let's do that first. Simply go to The Perl Programming Language site and download the latest version of Perl. I'd recommend ActiveState Perl (it's easier to set up, I hear).
Next, you need the latexmk
package. This is also easy to get. Go to your MikTeX Console, and since you'll be getting a package, run in Admin mode. Once there, go to the Packages tab on the left-hand side of the console window, and in the Search box, type latexmk
. You'll see the package in the results menu - it's recognizable by MikTeX, but isn't installed yet. Install the package, and restart your VS Code application.
As for the PDF viewer problem, once a .tex
file is saved, said file is automatically compiled, and the PDF is ready to be viewed in the VS Code application. Click the leftmost button in the upper right side of the application, just below the Windows minimize icon.