LaTeX Workshop
repository·master·Indexed 11 days ago
https://github.com/james-yu/latex-workshopA comprehensive Visual Studio Code extension for LaTeX typesetting, featuring compilation recipes, PDF viewing with SyncTeX, IntelliSense, linting, and math equation previews. Version 10.17.1 provides tools for autocomplete, automatic \item insertion, and support for LaTeX-3 commands.
What's inside LaTeX Workshop
- LaTeX Workshop is a Visual Studio Code extension designed to provide core features for LaTeX typesetting within the VS Code environment. It supports various workflows including compilation, linting, viewing with SyncTeX, and advanced IntelliSense for LaTeX and BibTeX files.
Overview of the LaTeX Workshop PDF Viewer
masterThe PDF viewer is built upon PDF.js by Mozilla. It operates by loading
viewer.htmlwithin aniframeinside a VS Code Webview.Key architectural details:
- Local Server: A local web server provides the files and handles WebSocket communication.
- Dual Compatibility: The architecture allows the same
viewer.htmlto be used both within VS Code and directly in a web browser. - Control Mechanism: The extension controls the viewer (in both VS Code tabs and browsers) via WebSockets using the
Viewer.handleron the server side. - Feature Extension: Additional features are implemented by attaching event listeners in
latexworkshop.tsto DOM objects withinviewer.html, rather than overriding PDF.js functions directly.
Understand the structure of Intellisense data files
masterLaTeX Workshop uses several JSON data files to power its Intellisense (autocompletion) features. These files categorize LaTeX components to provide relevant suggestions to the user:
- Unicode mathematical symbols: Provided via
unimathsymbols.json. - Commands and Environments: Default lists are stored in
commands.jsonandenvironments.json. - BibTeX: Field requirements for BibTeX entries are split into
bibtex-entries.json(mandatory fields) andbibtex-optional-entries.json(optional fields). - Classes and Packages:
classnames.jsoncontains LaTeX class names (dependent on local LaTeX installation).packagenames.jsoncontains package names derived from CTAN.packages/directory contains completion files for specific classes (prefixed withclass-) and other package-specific data, generated from TeXStudio CWL files.
- Unicode mathematical symbols: Provided via
How LaTeX Workshop manages root files
masterUnlike many language extensions that assume a single compilation target per workspace, LaTeX Workshop dynamically detects the root file and the compilation target based on the document currently being edited. This allows the extension to work effectively in multi-root workspaces. The detection is handled byManager.findRoot(), which is triggered by theonDidChangeActiveTextEditorevent.SyncTeX communication flow
masterThe PDF viewer supports SyncTeX for bidirectional navigation between the PDF and the LaTeX source:
- Forward SyncTeX: The Server sends a
synctexmessage to the Viewer. - Backward SyncTeX: The Viewer sends a
reverse_synctexmessage to the Server.
- Forward SyncTeX: The Server sends a
How the LaTeX Workshop manager logic works
masterThe manager is responsible for detecting the correct root file and parsing the entire project. The lifecycle follows this flow:
- Trigger: Occurs on
OnWatchedFileChangedor after finding a new root file. - Parsing: The manager executes
ParseFileAndSubson the root file. - File Discovery:
ParseFlsis called to identify files, which may triggerParseFileAndSubsfor new files. - Input Processing: For every file,
ParseInputFilesis executed. If new files are discovered during this step, the cycle repeats. - Watcher Registration: New files are added to the file watcher via
addToFileWatcher.
- Trigger: Occurs on
Use SyncTeX for direct and reverse navigation
masterLaTeX Workshop supports SyncTeX, allowing you to jump between locations in your
.texsource code and the compiled PDF.- Direct Sync: Jump from source to PDF.
- Reverse Sync: Click in the PDF to jump to the corresponding location in the
.texsource.
Restoring the PDF viewer state
masterTo ensure the PDF viewer returns to its previous state (e.g., zoom level, page number) when VS Code restarts, the extension uses Webview serialization.
- The state of the PDF viewer is sent from the Webview to the extension via
window.parent.postMessageinlatexworkshop.ts. - Upon reopening, the state is sent back to the viewer via the
restore_statemessage. - The Viewer then restores its internal state before completing the
pagesloadedcycle.
- The state of the PDF viewer is sent from the Webview to the extension via
Preview math equations on hover
masterYou can preview mathematical equations directly in the editor. Hovering over the start tag of a math environment will trigger a MathJax preview popup.How the Snippet View loads SVG assets
masterThe Snippet View uses a file namedsnippetpanel.jsonto retrieve all SVG text required for displaying snippets. This file is generated by the build process (dev/createSymbolSvgs.ts) and is loaded into the WebView View via afetchrequest.Note on snippet syntax for LaTeX Workshop
masterWhen defining custom snippets for LaTeX Workshop, be aware that some snippet definitions require using four backslashes\\instead of the standard two. This is necessary due to how VS Code handles snippet grammar and escaping. Refer to the official VS Code documentation on user-defined snippets for more details on escaping characters.Sequence of events when opening a PDF file
masterWhen a PDF file is opened, the following sequence occurs:
- The Viewer loads
viewer.html. - The Viewer loads
latexworkshop.js. - The Viewer sends an
openmessage to the WebSocket Server. - The Viewer fetches
/config.jsonfrom the Web Server. - The Viewer loads
viewer.js. - The
webviewerloadedevent is triggered. - The Viewer sets
PDFViewerApplicationOptions. - Internal PDF.js lifecycle events trigger:
pagesinit$\rightarrow$documentloaded$\rightarrow$Apply params$\rightarrow$pagesloaded. - The Viewer sends a
loadedmessage to the Server.
- The Viewer loads