Go to page:

Search:   Help

Navigation

Groups

LyX documentation

Edit

Shared groups

Links

LyX /

SyncTeX

Categories: Category SyncTeX
<< | Page list | >>

This page describes how to use SyncTeX to get reverse search, that is, the ability to get from a line in a PDF to the same line in the LyX document with a single click, and forward search, that is, the ability to get from a line in the LyX document to the same line in the PDF viewer. Forward search is achieved by right-clicking in the LyX document and choosing "Forward Search".

Forward and Reverse Search Settings for LyX 2.0 and Newer

Setting up LyX

  • In Document > Settings > Formats, check Synchronize with output
  • If your viewer needs another output format than \synctex=-1 (which is the default), select it or insert it in "Custom Macro"
  • Modify Preferences > Output > General > PDF command for your PDF viewer (see the viewer-specific notes below)

Caveats

  • MacOSX and renamed Lyx.app: Forward/reverse search does not seem to work (on OS X at least) if the LyX.app is renamed or not in /Applications. A work-around for this is to put a symlink (symbolic link) in /Applications that points to Lyx.app. Using an OS X alias will not work. To create the symlink, open Terminal.app which is in /Applications/Utilities by double-clicking on it. Then paste a line like the following
ln -s /path/to/LyX.app /Applications
where /path/to/ is the path to the folder containing LyX.app. Hit return and quit Terminal.app. Check /Applications to see if the symlink is there – it should look like the LyX.app icon but with a small arrow added. Note that symlinks are different from aliases in that with an alias, if the target (pointed-to) file is moved, the alias will still point to it but the symlink will not. Finally, there is a free utility that can be used if one prefers to not use Terminal.app; see https://www.macupdate.com/app/mac/10433/symboliclinker.

Viewer-specific notes

Evince

As opposed to many other readers, forward and backward search with evince requires some helper scripts to be installed (since evince uses the D-Bus channel to communicate with editors). These scripts can be downloaded here. Installation and setup instructions are included in the ZIP archive (see README file).

If the scripts don't work out of the box, the reason might be your LyX generating "[filename].synctex" files instead of "[filename].synctex.gz" (the scripts assume the latter). In this case, you must replace the line synctex_file += ".synctex.gz" by synctex_file += ".synctex" in the script evince_sync_lyx .

Depending on your system's configuration, you might have to change the python interpreter in the first line of the scripts from #!/usr/bin/python to #!/usr/bin/python3 , if the former one points to Python version 2.x, and things do not work.

Skim

In LyX,

  • Tools→Preferences→Output→General→PDF command: /path/to/Skim.app/Contents/SharedSupport/displayline $$n $$o $$t where /path/to/ is the path to Skim.app which is frequently just /Applications/.

In Skim:

  • Skim PDF-TeX Sync setting: Custom
  • command: /Applications/LyX.app/Contents/MacOS/lyxeditor
  • arguments: "%file" %line; /usr/bin/osascript -e 'tell application "LyX" to activate'

Okular

In LyX (Forward search):
  • Tools→Preferences→Output→General→PDF command: okular --unique "file:$$o#src:$$n $$f" or okular --unique "$$o#src:$$n $$f" (Windows)
  • On Windows: Add KDE's bin path (e.g. C:\ProgramData\KDE\bin\) as search path to LyX, separated with semicolon before (at Tools→Preferences→Paths→PATH)
  • This only works in LyX version 2.0.5+
In Okular (Inverse search):
  • Click Settings→Configure Okular
  • On Linux:
* Click Editor, and select LyX-client from the drop-down list.
* If it doesn't work, try "Custom editor" and insert lyxeditor %f %l
  • On Windows:
At the following two subpoints, change LyX's bin path (here as an example C:\Program Files (x86)\LyX\bin\)) accordingly:
* Click Editor, select "Custom Text Editor" and insert "C:\Program Files (x86)\LyX\bin\lyxeditor.cmd" "%f" "%l" (don't delete the quotation marks before "C:" and after "cmd")
* create a batch-file in LyX's bin path named lyxeditor.cmd:

@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


  • Now, holding SHIFT while clicking with your left mouse button somewhere in the PDF should make LyX move to the corresponding position in the source document.
NB: This only works in Browse mode - for example, pressing Shift-click will not do anything in Text Selection mode:

qpdfview

In LyX

  • Modify Preferences > Output > General > PDF command to be qpdfview --unique "$$o#src:$$t:$$n:0"
  • In Preferences > File Handling > File formats verify that the viewer in the various PDF (*) formats is set to qpdfview --unique

In qpdfview

  • In Edit > Settings > Source editor and insert lyxclient -g %1 %2
  • (optional) Modify Edit > Settings > Modifiers > Open in source editor to your liking (not all options work, try "Ctrl and Alt").
  • To use reverse search, in qpdfview:
* Right click the document and choose Edit 'file.tex' at... ;
OR
* Hold the selected modifier key(s) while left-clicking the document.

Zathura (on Linux)

In LyX:

  • Tools→Preferences→Output→General→PDF command: zathura --synctex-forward $$n:1:$$t $$o (don't miss space before $$o).

In Zathura:

  • Open Zathura config file, usually in home directory ~/.config/zathura/zathurarc
  • add to it:

    set synctex true
    set synctex-editor-command "lyxclient -g %{input} %{line}"
    


Sumatra PDF (on Windows; Updated 2019-07-04)

Tools → Preferences → Path

  • Add the directories, C:\Program Files (x86)\SumatraPDF & C:\Program Files (x86)\LyX\bin, (where SumatraPDF.exe and LyX reside), to the PATH prefix line. NOTE this is important to get right the system command from LyX should be simply SumatraPDF.exe (then the -forward-search "TeX" line# and "preview.pdf" filename) equally the system call back from SumatraPDF to LyX pipe is via "lyxeditor.cmd" "%f" %l
  • Set the system memory line handle LyXServer pipe to \\.\pipe\lyxpipe.

Tools → Preferences → File Handling → Converters

  • Select the format LaTeX (pdflatex) -> PDF (pdflatex)
  • Set the entry converter to
      pdflatex -interaction=nonstopmode -synctex=1 $$i
  • Click Modify (otherwise the change will be discarded).
  • This change is needed to make MikTeX produce a FILE.synctex.gz synchronisation file.

Tools → Preferences → Output → General

  • In the “forward search” section set the entry PDF command to
      SumatraPDF -reuse-instance -forward-search "$$t" $$n "$$o"

Tools → Preferences → File Handling → File Formats

  • Select the format PDF (pdflatex)
  • Set the Viewer field to Custom with a command
    SumatraPDF -reuse-instance -inverse-search "CMD /C (ECHO LYXCMD:revdvi:server-goto-file-row:%f %l) > \\.\pipe\lyxpipe.in"
    
    (Note: inverse search of .lyx files with spaces or other special characters in the path or file name may not work with this approach on some Windows systems)
  • You may need to close all SumatraPDF windows and use “Document → View”.

Alternatively,

  • You set inverse search command directly in SumatraPDF by going to “Preferences → Options” and set the fields “Command line for the inverse search” to "C:\Program Files (x86)\LyX\bin\lyxeditor.cmd" "%f" %l (don't delete the quotation marks before "C: and after cmd" or the ones either side of "%f" ) (the lyxeditor.cmd file must be created as per the instructions above for the Okular viewer, and is probably best place somewhere else than LyX's install folder...)
  • In this case you can reduce the “Viewer” command to SumatraPDF.

Using

  • Unlike in some plain-LaTeX editors, forward search will not be toggled by the “Document → View” command; It must be explicitly used with “Right Click → Forward Search”.
  • Inverse search (from SumatraPDF to LyX) is effected by double clicking on a line in Sumatra.

Additional Remarks

  • The current version was tested with LyX 2.1.3 and 2.1.4. UpTo LyX 2.3.3 (2019) and SumatraPDF v.3.1.2 (32/64 bit) on Windows XP - Win 10.
  • If the path to the LyX file contains blanks, the synchronization may or may not work. In case the synchronization does not work and the path contains blanks, one can try changing the directories and/or file names so that they do not contain the blanks (tested on Windows 10 on 2019-06-25).
  • The older versions and current 3.1.2 just need exactly the same simplified SumatraPDF “editor” command as mentioned above in the section for Okular. "C:\Program Files (x86)\LyX\bin\lyxeditor.cmd" "%f" %l (don't delete the quotation marks before "C:" and after "cmd")
  • If pasting from the wiki into LyX (e.g. SumatraPDF) when following these instructions, ensure that you do not also paste a bullet at the start of the entry!
  • Judging from the old text, the “Converters” setting wasn't required before LyX 2.1.x.
  • The old version of this section mentioned an alternative value of \\.\pipe\C:\lyxserver in case that reverse search doesn't work.If you try, you need to change \\.\pipe\lyxpipe.in into \\.\pipe\C:\lyxserver.in.
  • Renaming the pipe to C:\lyxserver shouldn't make any difference, as on Windows named pipes reside in their own namespace, separate from user-accessible files. \\.\pipe\C:\lyxserver should have nothing to do with the file \\.\C:\lyxserver aka C:\lyxserver

Forward and Reverse Search Settings for LyX 1.6.*

To get this wonderful feature, you'll have to (1) enable SyncTeX in LyX, and (2) set LyX as the editor in your PDF reader.

Enable SyncTeX in LyX

First you'll have to ensure that LyX writes the SyncTeX file which tells your PDF reader about the correspondences between PDF positions and source document positions.

  1. Enter Tools > Preferences > File Handling > Export programs
  2. Click "LaTeX (pdflatex) > PDF (pdflatex)" in the list. In the "Export program" box, it now says pdflatex $$i. Instead, put pdflatex -synctex=-1 $$i there.
  3. Click Modify to the right of the list, then Apply.

(Some tutorials will tell you to use -synctex=1 instead. This enables compression, in order to save some disk space. However, the PDF readers I've tried have not been able to use the compressed file.)

Set LyX as SyncTeX editor in your PDF reader

If you use Okular

  1. In Okular, click Settings > Set up Okular
  2. Click Editor, and select LyX-client from the drop-down list.

Now, holding SHIFT while clicking with your left mouse button somewhere in the PDF should make LyX move to the corresponding position in the source document.

If you use Skim.app

  1. In Skim, click Preferences > Sync
  2. Select LyX in the menu

Now, holding COMMAND-SHIFT while clicking with your left mouse button somewhere in the PDF should make LyX move to the corresponding position in the source document.

In case LyX does not open Skim.app as your previewer: In LyX, click Preferences > File formats > PDF (pdflatex), enter open -a Skim.app $$i in the viewer setting. Then you need to click the Modify button, and then Save.

If you use Sumatra, yap or xdvi on Cygwin

See http://wiki.lyx.org/LyX/LyXOnCygwin#toc6

Troubleshooting SyncTeX

Make sure that the .lyxpipe.in and .lyxpipe.out files do not exist when LyX is not running. (They are normally deleted when LyX quits, but if LyX crashes, they remain and prevent LyX from communicating with PDFView.) Their location can be found in LyX > Preferences > Paths.

SyncTeX vs pdfsync

Another possible way of getting reverse-search is adding \usepackage{pdfsync} to the LaTeX preamble. However, this method is seen as deprecated now. (SyncTeX was created by the same person as pdfsync, and has the potential for more precise positioning; although LyX is at the moment not able to completely make use of the extra precision.)

Categories

Category SyncTeX

Edit - History - Print - Recent Changes - All Recent Changes - Search
Page last modified on 2022-03-23 08:32 CET