Chapter 8: Advanced Typesetting: LaTeX with WSL2, TeXLive, and VS Code
For creative technologists and academics, producing high-quality documents, reports, or publications often necessitates tools beyond standard word processors. LaTeX is a powerful typesetting system renowned for its ability to produce beautifully formatted documents, especially those with complex mathematical formulae, structured layouts, and extensive bibliographies.
This chapter guides you through setting up a robust LaTeX environment using Windows Subsystem for Linux (WSL2), TeXLive (a comprehensive LaTeX distribution), and Visual Studio Code (VS Code) with the highly-regarded LaTeX Workshop extension. While the setup involves several steps, the resulting workflow offers a seamless and powerful experience for LaTeX development within a familiar coding environment.
8.1 Prerequisites and Overview
This guide assumes you have a working Windows environment and are familiar with the basics of VS Code, as covered in earlier chapters. We will cover:
- Installing Windows Subsystem for Linux (WSL2) and Ubuntu.
- Installing TeXLive, a comprehensive LaTeX distribution, on Ubuntu.
- Connecting VS Code to your WSL2 environment.
- Configuring the LaTeX Workshop extension in VS Code for an optimal experience.
- Leveraging Claude Code for AI-assisted LaTeX development.
8.2 Setting up Windows Subsystem for Linux (WSL2) and Ubuntu
WSL2 allows you to run a genuine Linux environment directly on Windows, providing access to Linux-native tools and workflows.
-
Install Ubuntu via Microsoft Store:
- Open the Microsoft Store on your Windows machine.
- Search for "Ubuntu" (e.g., "Ubuntu 22.04 LTS") and install it.
- Launch Ubuntu once installed to complete the initial setup, which includes creating a username and password for your Linux environment.
-
Install Windows Terminal (Recommended):
- For a superior terminal experience, install "Windows Terminal" from the Microsoft Store. It offers tabbed access to various shells, including your WSL2 Ubuntu instance.
-
Basic Linux Command Familiarity:
- Once inside your Ubuntu terminal (via Windows Terminal or the Ubuntu app), familiarise yourself with these essential commands:
cd <directory>: Change directory (e.g.,cd /mnt/c/Users/YourUser/Documentsto access your Windows Documents). Your Linux home directory is~.ls: List directory contents (ls -hlfor detailed, human-readable output).mkdir <directory_name>: Create a new directory.mv <source> <destination>: Move or rename a file/directory.cp <source> <destination>: Copy a file/directory.rm <file>: Remove a file (rm -r <directory>to remove a directory recursively).sudo <command>: Execute a command with superuser (administrator) privileges.apt: The Advanced Package Tool used for managing software on Debian-based Linux distributions like Ubuntu.sudo apt update: Refresh the list of available packages.sudo apt upgrade: Upgrade all installed packages to their newest versions.
man <command>: Display the manual page for a command (e.g.,man ls).
- Once inside your Ubuntu terminal (via Windows Terminal or the Ubuntu app), familiarise yourself with these essential commands:
8.3 Installing TeXLive on Ubuntu
TeXLive is a comprehensive and widely used LaTeX distribution.
-
Update Package Lists: Ensure your package lists are up to date:
sudo apt update -
Install TeXLive: Install a substantial set of TeXLive packages, including common LaTeX tools and extra features. The
texlive-latex-extrapackage includes many useful classes and styles.sudo apt install texlive texlive-latex-extra- Note: This command installs a version of TeXLive available in Ubuntu's repositories, which might not be the absolute latest. However, it is generally stable and sufficient for most purposes.
- You can add other specific packages as needed, such as
texlive-sciencefor scientific publications or language-specific packages liketexlive-lang-french.
-
Explore CTAN (Optional but Recommended): The Comprehensive TeX Archive Network (CTAN) is the central repository for LaTeX packages, documentation, and related software. It's an invaluable resource for finding packages and their documentation (often in PDF format).
8.4 Connecting VS Code to WSL2
VS Code integrates seamlessly with WSL2, allowing you to edit files and use terminals within your Linux environment directly from the VS Code interface on Windows.
-
Install the WSL Extension in VS Code:
- Open VS Code.
- Go to the Extensions view (
Ctrl+Shift+XorCmd+Shift+X). - Search for "WSL" by Microsoft and install it.
-
Connect to WSL:
- Once the extension is installed, open the Command Palette (
Ctrl+Shift+PorCmd+Shift+P). - Type "WSL: Connect to WSL" and select it. You may be prompted to choose your installed Linux distribution (e.g., Ubuntu).
- A new VS Code window will open, connected to your WSL2 environment. You can identify this by the green indicator in the bottom-left corner, which should show your WSL distribution (e.g., "WSL: Ubuntu").
- Once the extension is installed, open the Command Palette (
-
Open Your Project Folder:
- Within the WSL-connected VS Code window, you can open folders located within your Linux file system (e.g.,
/home/your_linux_username/projects) or access your Windows file system via/mnt/c/.
- Within the WSL-connected VS Code window, you can open folders located within your Linux file system (e.g.,
8.5 Setting up LaTeX Workshop in VS Code
The LaTeX Workshop extension for VS Code provides a rich set of features for LaTeX development.
-
Install LaTeX Workshop:
- In your WSL-connected VS Code window, go to the Extensions view.
- Search for "LaTeX Workshop" by James Yu and install it within the WSL environment.
-
Understanding Root Files:
- LaTeX Workshop typically identifies the main
.texfile of your project (the "root" file) by looking for the\documentclass{...}command.
- LaTeX Workshop typically identifies the main
-
Configure Build Tools (Workspace Settings):
- To ensure LaTeX Workshop uses
pdflatex(a common LaTeX compiler that produces PDF output) correctly within WSL, you'll configure its settings. - Open VS Code settings (
Ctrl+,orCmd+,). - It's best to apply these settings at the Workspace level. If you have a specific LaTeX project folder open, switch to the "Workspace" tab in the settings UI.
- Click the "Open Settings (JSON)" icon in the top-right corner of the settings UI (it looks like a document with curly braces).
- This will open (or create) a
.vscode/settings.jsonfile in your project's root directory. Add the following configuration:
{ "latex-workshop.latex.tools": [ { "name": "pdflatex", "command": "pdflatex", "args": [ "--shell-escape", "-synctex=1", "-interaction=nonstopmode", "-file-line-error", "%DOC%" ] } ], "latex-workshop.latex.recipes": [ { "name": "pdflatex", "tools": [ "pdflatex" ] } ], "latex-workshop.view.pdf.viewer": "tab" // Optional: open PDF in a VS Code tab }- Save the
settings.jsonfile.
- To ensure LaTeX Workshop uses
-
Using LaTeX Workshop:
- Open a
.texfile. - When you save your
.texfile, LaTeX Workshop should automatically compile it. - You can view the PDF output directly within VS Code (often in a separate tab or side-by-side).
- SyncTeX: Ctrl+Click (or Cmd+Click) on text in the PDF viewer to jump to the corresponding LaTeX source, and vice-versa (Ctrl+Alt+J or Cmd+Alt+J from source to PDF).
- Explore the LaTeX Workshop side panel (usually a TeX icon in the Activity Bar) for more commands and options.
- For detailed information, consult the LaTeX Workshop Wiki.
- Open a
-
Spell Checking:
- Consider installing a spell-checking extension like "Code Spell Checker" (by Street Side Software) for your LaTeX documents. It supports multiple languages, including UK English.
8.6 Enhancing Your LaTeX Workflow with Claude Code
Just as Claude Code can assist with general programming tasks (as discussed in Chapter 6), it can also be a valuable companion for writing LaTeX. Launch Claude Code from your LaTeX project directory and try:
- Understanding Complex Syntax:
- "Explain what the
\usepackage{tikz}and\begin{tikzpicture}commands do in my document."
- "Explain what the
- Generating Structures:
- "Generate a LaTeX
articledocument structure with sections for Introduction, Methods, Results, and Conclusion." - "Create a LaTeX
beamerslide template for a presentation titled 'My Research'."
- "Generate a LaTeX
- Debugging Errors:
- "I'm getting this LaTeX compilation error:
[paste error message]. What does it mean and how can I fix it?"
- "I'm getting this LaTeX compilation error:
- Finding Packages:
- "What LaTeX package can I use to create timelines?"
- Refining Content:
- "Suggest alternative phrasing for the abstract in mydocument.tex in a formal academic style."
Remember to use Git for version control, especially when incorporating AI-generated LaTeX code, to easily track changes and revert if necessary.
8.7 Manual Compilation (For Understanding)
Before relying entirely on the LaTeX Workshop extension, it can be instructive to compile a document manually from the WSL terminal:
-
Create a Simple
.texFile:- In your WSL environment, create a file named
mydocument.texwith the following content:\documentclass{article} \title{My First LaTeX Document via WSL} \author{Your Name} \date{\today} \begin{document} \maketitle Hello, LaTeX world from WSL2 and VS Code! \end{document}
- In your WSL environment, create a file named
-
Compile from Terminal:
- Open a WSL terminal in the directory containing
mydocument.tex. - Run the
pdflatexcommand:pdflatex mydocument.tex - This will generate several files, including
mydocument.pdf. You can open this PDF to view your compiled document.
- Open a WSL terminal in the directory containing
This manual step helps confirm that TeXLive is installed correctly and provides a basic understanding of the compilation process.
This comprehensive LaTeX setup empowers you to create professional-quality documents efficiently, combining the strengths of LaTeX, the Linux environment via WSL2, and the versatile VS Code editor, further enhanced by AI assistance from Claude Code.
Next: Chapter 9: Context Management & Advanced Agent Workflows