手把手带你在vscode中配置latex

1. Introduction

LaTeX is a high-quality typesetting system that is widely used for academic and technical writing. It allows you to create professional-looking documents that include equations, tables, figures, and references. In this tutorial, we will show you how to set up a LaTeX environment in VS Code, an open-source code editor that is popular among developers and researchers.

2. Prerequisites

Before we get started, make sure you have the following software installed on your computer:

2.1 LaTeX distribution

A LaTeX distribution is a bundle of packages and executables that are necessary for compiling LaTeX files. There are several options available, but we recommend using MiKTeX, a free and cross-platform distribution that supports Windows, macOS, and Linux.

To install MiKTeX, follow these steps:

Go to the MiKTeX download page.

Choose the appropriate version for your operating system. We recommend using the Basic MiKTeX installer, which includes the most commonly used packages.

Run the installer and follow the instructions. You can leave the default settings unchanged.

2.2 VS Code

VS Code is a lightweight and powerful code editor that is easy to use and extensible. It supports a wide range of programming languages and file formats, including LaTeX.

To install VS Code, follow these steps:

Go to the VS Code download page and choose the appropriate version for your operating system.

Run the installer and follow the instructions. You can leave the default settings unchanged.

3. Installing the LaTeX Workshop Extension

VS Code supports extensions, which are packages that add new features and functionality to the editor. The LaTeX Workshop extension is a popular choice among LaTeX users, as it provides a comprehensive set of tools for authoring, previewing, and compiling LaTeX documents.

To install the LaTeX Workshop extension, follow these steps:

Open VS Code.

Press Ctrl+Shift+X or click on the Extensions icon (puzzle piece) in the sidebar to open the Extensions panel.

Search for "LaTeX Workshop" and click on the "Install" button.

4. Configuring LaTeX Workshop

Once you have installed the LaTeX Workshop extension, you need to configure it to work with MiKTeX and to customize its settings according to your needs.

4.1 Set the PATH variable

By default, LaTeX Workshop assumes that the MiKTeX executables are in your system PATH, which is a list of directories that the operating system searches for executables when you type a command in the terminal. If MiKTeX is not in your PATH, you need to add it manually.

To add MiKTeX to your PATH, follow these steps:

Open the Start menu and search for "Environment Variables".

Click on "Edit the system environment variables".

Click on the "Environment Variables" button.

Under "System Variables", find the "Path" variable and click on "Edit".

Add the path to the MiKTeX bin directory (e.g., C:\Program Files\MiKTeX 2.9\miktex\bin) at the end of the list.

Click on "OK" to close all the windows.

Restart VS Code for the changes to take effect.

4.2 Configure the LaTeX Workshop settings

LaTeX Workshop provides many settings that allow you to customize its behavior. You can access the settings by opening the Settings editor (Ctrl+,) and typing "latex" in the search box. Here are some of the most useful settings:

latex-workshop.view.pdf.viewer: Sets the default PDF viewer. We recommend using "tab", which opens the PDF in a new tab in VS Code.

latex-workshop.docker.enabled: Enables or disables the use of Docker containers for compiling LaTeX documents. If you are having problems with the default MiKTeX installation, you can try using a Docker container instead.

latex-workshop.synctex.enabled: Enables or disables SyncTeX, a feature that allows you to click on a position in the PDF and jump to the corresponding position in the source code.

latex-workshop.intellisense.surroundCommand.enabled: Enables or disables the use of LaTeX commands in IntelliSense suggestions. If you are new to LaTeX, you may find this helpful.

5. Creating a LaTeX Document

Now that you have set up your LaTeX environment, you can start creating a LaTeX document. A LaTeX document consists of two parts: the preamble and the document body. The preamble contains commands that affect the entire document, such as the document class, packages, and custom commands. The document body contains the actual content of the document, such as sections, equations, and figures.

Here is an example of a minimal LaTeX document:

\documentclass{article}

\begin{document}

Hello, World!

\end{document}

To create a new LaTeX document in VS Code, follow these steps:

Open VS Code.

Press Ctrl+N to create a new file.

Save the file with a .tex extension (e.g., document.tex).

Type or copy and paste the LaTeX code into the file.

Press Ctrl+S to save the file.

6. Previewing the Document

Once you have created a LaTeX document, you can preview it in several ways using LaTeX Workshop.

6.1 Preview in VS Code

The simplest way to preview a LaTeX document is to use the Preview panel in VS Code. LaTeX Workshop automatically compiles the document whenever you save it and updates the preview in real time.

To preview the document in VS Code, follow these steps:

Open the LaTeX document.

Press Ctrl+Shift+V or click on the Preview icon (eye) in the sidebar to open the Preview panel.

6.2 Preview in a PDF Viewer

If you prefer to use an external PDF viewer, you can configure LaTeX Workshop to open the PDF in your favorite viewer. You can also set up automatic SyncTeX synchronization between the PDF and the source code.

To preview the document in a PDF viewer, follow these steps:

Open the LaTeX document.

Press Ctrl+Shift+P to open the Command Palette.

Type "LaTeX Workshop: Build with recipe" and press Enter.

Select the appropriate recipe for your document (e.g., pdflatex).

Wait for the compilation to finish.

Press Ctrl+Shift+P and type "LaTeX Workshop: View LaTeX PDF file".

Select the appropriate viewer for your system (e.g., SumatraPDF).

7. Conclusion

In this tutorial, we have shown you how to set up a LaTeX environment in VS Code and how to create, preview, and compile LaTeX documents using the LaTeX Workshop extension. We hope this tutorial has been helpful and that you are now able to write beautiful and professional-looking documents with LaTeX.