How to set up inverse and forward search in LyX for Windows

I describe how to set up inverse and forward search in LyX for the Windows environment, with SumatraPDF as the pdf viewer. Inverse search lets you quickly move to the corresponding part of a LyX (or LaTeX) source from its pdf output, typically by clicking on the text in the pdf viewer. Forward search works in the opposite direction; from the LyX (or LaTeX) source, you can invoke forward search to make the pdf viewer scroll through the pdf document so the output counterpart of the source is shown. They are very useful especially when you are editing a long document. Most of the instructions overlap with those found at http://wiki.lyx.org/LyX/SyncTeX#toc3.

LyX

  1. Set Tools > Preferences > Paths > LyxServer pipe to \\.\pipe\lyxpipe.

  2. In Document > Settings > Output, check Synchronize with output. (this is checked by default)

Scripts

Create a batch file named lyxeditor.cmd with the following contents and save it to one of the locations in your PATH Windows environmental variable so the pdf editor can call it:

@echo off
SETLOCAL enabledelayedexpansion
set file=%1
set row=%2
REM remove quotes from variables
set file=!file:"=!
set row=!row:"=!
%comspec% /q /c (@echo LYXCMD:revdvi:server-goto-file-row:%file% %row%)> \\.\pipe\lyxpipe.in&&type \\.\pipe\lyxpipe.out
endlocal

The above cmd works well, but it shows an annoying black cmd window when you invoke the script. With a simple AutoHotkey script, one can not only suppress this window but also activate the LyX window after the inverse search.

Create an AutoHotkey script named lyx-inverse-search.ahk with the following code and save it to the same location at lyxeditor.cmd and compile it with the AutoHotkey to generate lyx-inverse-search.exe:

SetTitleMatchMode, RegEx
Run, lyxeditor.cmd "%1%" "%2%",, Hide
WinActivate, ahk_exe lyx.exe,,,

If you don't have AutoHotkey installed, you can also download the pre-compiled file.

SumatraPDF

SumatraPDF is good because you can recompile your LaTeX document while the pdf is open with SumatraPDF and the viewer will automatically refresh the pdf file. Some pdf viewers will lock the pdf file and will not let you re-generate the pdf file unless you close the file first. It also supports inverse and forward search nicely.

  1. Download and install Sumatra PDF.

  2. In LyX, go to Tools > Preferences > Paths and append the install location of SumatraPDF to PATH prefix. For example, C:\Program Files\SumatraPDF.

Changelog

[2018-07-16 Mon]

  • Update AutoHotkey script to correctly identify LyX window to activate

Comments

Comments powered by Disqus