Write and build LaTeX documents using Visual Studio Code on macOS

If you're like me and Visual Studio Code is your preferred text editor and you are looking for a way to use it as a LaTeX editor for creating your next paper, software manual, or well... any document, you are in the right place. There's no need to install an additional editor just for this purpose. Let's use the editor we already love and are familiar with.

  1. Since VS Code is your preferred text editor (right?), I'm assuming you already have it installed on your system, but if that's not the case, download VS Code for macOS and drag it to the Applications folder.
  2. In VS Code, go to extensions and look for LaTeX Workshop. The one developed by James Yu is the want you want. Let's install it. LaTeX Workshop extension
  3. Now, you'll need a TeX distribution installed on your system. My favorite one is MiKTeX because it allows you to keep your TeX installation as minimal as possible but can also automatically install additional components from the internet when required by your project. Let's download MiKTeX for macOS.
  4. Install MiKTeX by clicking on the downloaded .dmg file. Agree to the license agreement terms and drag the MiKTeX app to the Applications folder.
  5. Open the newly installed MiKTeX Console and click on Finish private setup. MiKTex setup screen

    If you need MiKTeX to be available for all system users, select Restart as administrator and finish setup instead.

  6. After the setup finishes, a dialog box will appear asking to restart MiKTeX. Hit OK and wait for the MiKTeX console to reopen. You've now set up MiKTeX in your system. Disregard the error prompt asking you to check for MiKTex updates. MiKTex Console screen

    Optional: After restarting the application, you can click the Upgrade button to upgrade from the minimal TeX installation to a standard one. It isn't necessary because MiKTeX can install packages on the fly depending on your TeX project needs.

  7. Before we can test our setup, the environment variable PATH should reference the directory that contains the MiKTeX executables. In macOS, these executables are found in ~/bin if you've installed MiKTeX for private use (as done in step 5). Let's open the macOS Terminal and run this command:
sudo launchctl config user path "$HOME/bin:$PATH"
Terminal screen with command

If you have installed MiKTeX system-wide, you can find the MiKTeX executables in /usr/local/bin. In this case, you'll need to run sudo launchctl config system path "/usr/local/bin:$PATH".

  1. After running the command needed, you should see a successful configuration setup message. We now have all we need for this to work. Reboot your system for the last changes to take effect.

    Important: Your system must be rebooted before proceeding to the testing steps.

Let's test your setup.

Open your TeX project in VS Code. If you don't have any on hand, you can try this one from my GitHub repo. This project is a recipe template that contains a .cls file with the document configuration and required packages. You can also find a .tex file, which is the actual document template and content. Click on template.tex. If you installed LaTeX Workshop (see step 2), you should see a green arrow at the top right of the editor window (in the split editor option section). Click it; it will build your LaTeX project and generate the resulting .pdf document. This process will take a few minutes, depending on the scale of your document and how many packages need to be downloaded to build it.

When the document is being built, you'll see various prompts asking if you want to download and install packages. If you don't want to be asked each time a new package needs to be downloaded, open the MiKTeX Console app, and in Settings > General, set package installation to Always.

VS Code screen with built document

And there you have it! You're ready to create documents to your heart's content using VS Code and LaTeX. Let me know if you have any questions or comments.

Share:
Questions or comments?

Was this helpful?

If this content added value to your day, consider fueling my late-night coding sessions! Your support helps me keep creating and sharing more geeky goodness.

Buy me a coffeeBuy me a coffee