How to implement two-way search between LaTeX and SumatraPDF in VSCode, my...

Implementing a two-way search between LaTeX and SumatraPDF in Visual Studio Code requires a specific configuration that leverages the unique capabilities of the SumatraPDF viewer and the LaTeX Workshop extension. The core mechanism hinges on establishing a communication channel where the PDF viewer can send a "reverse search" command back to VSCode, instructing it to jump to the corresponding LaTeX source code line. This is not a default feature and necessitates explicit setup. The primary steps involve configuring LaTeX Workshop to use SumatraPDF as its viewer with the correct command-line arguments, and then configuring SumatraPDF itself to use VSCode as its inverse search client. The critical technical detail is that SumatraPDF must be launched with the `-forward-search` and `-recompile` flags, pointing to the source file and line number, while its inverse search setting must be programmed to execute VSCode's command-line interface, `code.cmd`, with arguments to open the file at a specific line.

The practical implementation begins within VSCode's settings for the LaTeX Workshop extension. In your `settings.json`, you must define a custom viewer recipe. A typical configuration would set the `latex-workshop.view.pdf.viewer` to `external`, and then specify a custom command under `latex-workshop.view.pdf.external.command`. The command should invoke SumatraPDF's executable path, often structured as `["C:\\Path\\To\\SumatraPDF.exe", "-forward-search", "%TEX%", "%LINE%", "-reuse-instance", "-inverse-search", "\"code.cmd\" \"%f\":%l", "%PDF%"]`. The `-inverse-search` parameter here is crucial; it provides SumatraPDF with the instruction it needs to call back to VSCode. The placeholder `%f` will be replaced by the PDF viewer with the file path and `%l` with the line number when you perform a reverse search by double-clicking in the PDF.

Concurrently, you must ensure that the `code.cmd` command, which is installed with VSCode and typically resides in its installation directory, is available in your system's PATH environment variable. This is essential for SumatraPDF to successfully execute the inverse search command. If it is not in the PATH, you must use the full absolute path to `code.cmd` in the `-inverse-search` argument. The sequence is then operational: compiling from VSCode launches SumatraPDF with forward search, automatically highlighting the corresponding text in the PDF. Double-clicking any text in the SumatraPDF window triggers the inverse search command, launching `code.cmd` with the file and line number arguments, which opens the source file in your existing VSCode window and jumps the cursor to that precise line.

The implications of this setup are significant for productivity, creating a truly integrated editing and preview environment. It eliminates the manual search for code corresponding to a PDF element, which is error-prone and time-consuming. Potential pitfalls include path errors due to spaces in directory names, which require proper quoting in the command strings, and system-specific variations in the location of executables. The configuration is also sensitive to updates of either VSCode or SumatraPDF that might change default behaviors or paths, though the underlying protocol is stable. Successfully implementing this creates a seamless, bidirectional workflow where the boundary between writing code and viewing the formatted output becomes virtually transparent.