5

I'm in a situation where I'm building my LaTeX with a script that preprocesses my .Tex files, producing new ones as output that are then compiled with latexmk.

Ideally, the synctex files will point to the original, not the generated output, so that I can easily jump between my viewer and editor.

Assuming that the generated files correspond to the original ones line by line, or that I have some way to find the matching lines between them, is there a way to easily edit the produced synctex file to point to the right thing?

1 Answers1

5

Editing the synctex output file: pointing to the correct file.

The information supplied in the question is incomplete. The question will be re-phrased with missing specifics, and then answered in that setting.

Re-Phrased Question.

Let's assume the sources to be processed by a script are almost the files ingested into the stomach of the TeX engine, pdflatex assumed. A folder structure imagined for $HOME="/home/jmite" is /home/jmite/tex/src /home/jmite/tex/work An unprocessed TeX file a.tex and its script-processed output a+.tex are assumed located as follows: /home/jmite/tex/src/a.tex /home/jmite/tex/work/a+.tex The main file is assumed to be /home/jmite/tex/work/main.tex Engine pdflatex compiles the PDF from connected folder /home/jmite/tex/work.

The Problem.

Synctex provides synchronization data in main.synctex. However, the file the editor loads during forward search is a+.tex and not a.tex. Similarly, inverse search requires script output a+.tex to be in the editor, not the desired source a.tex.

The text editor should only load /home/jmite/tex/src/a.tex, the original template.

Question:

Can a.tex be opened in the editor instead of a+.tex, by editing the synctex file?

Short Answer.

File name a+.tex can be edited to file name a.tex in synctex file main.synctex. The associated data will be wrong. Both forward and inverse search will locate the cursor at the wrong place.

It still may be the easiest work-around for the issue. The replacement main.synctex is the output of a shell script that reads main.synctex line-by-line and changes the affected file names.

How to do editing of main.synctex.

A tested solution is an editor button that calls a BASH script to make the edits. Script construction is a time sink, due to particulars of the folders and file names in the project. The basic script reads main.synctex line-by-line and echoes the line or its edited version to tmp.synctex, same directory. Rename file tmp.synctex to main.synctex to finish. Because main.synctex can have millions of lines, the script can have a long runtime.

How to use the editor button.

Without clicking the button, the default synctex action is used: forward search loads a+.tex in the editor. Inverse search works as expected. After the button is clicked, then forward search loads a.tex in the editor. Inverse search may be broken.

Structure of main.synctex

The advice to look at main.synctex is correct and valuable for learning how synctex works.

Assumed: main.tex is the file compiled by pdflatex and there are many TeX/LaTeX files referenced in main.tex. Assume a large project like a book which uses BibTeX, multiple indices, figures, equations and the like in 12+ chapters.

The TeX engine uses command line switch --synctex=-1 to make main.synctex and --synctex=1 to make main.synctex.gz. If pdflatex produces main.synctex.gz, then an extra decompression step is required: gzip -d main.synctex.gz, which creates larger text file main.synctex.

Examine main.synctex with Linux terminal command less. There will be many lines that look like Input:1:/home/jmite/tex/work/main.tex Input:2:/usr/local/texlive/2021/texmf-dist/tex/latex/base/book.cls Input:3:/usr/local/texlive/2021/texmf-dist/tex/latex/base/leqno.clo Input:4:/usr/local/texlive/2021/texmf-dist/tex/latex/base/bk11.clo Input:5:/usr/local/texlive/2021/texmf-dist/tex/latex/geometry/geometry.sty In this example, there were 460 such lines out of 1.9 million lines. Lines starting with keyword Input cite files input by main.tex which the TeX engine uses to create main.pdf. Data follows certain lines, for instance: Input:86:/home/jmite/tex/work/section1-3.tex !59 {3 [86,167:4736286,43087544:21905327,38351258,267389 (86,167:4736286,4736286:0,0,0 [86,167:0,4736286:0,0,0 (86,167:0,0:0,0,0 g86,167:0,0 ) The explanation of the data is best left to Laurent's synctex article at http://www.tug.org/TUGboat/tb29-3/tb93laurens.pdf A brief explanation: the data is coded information which documents positions in the PDF which were typeset by pdflatex using file section1-3.tex.

Forward Search.

Assume section1-3.tex is not loaded into the editor. Assume the PDF viewer shows the section. A PDF forward-search (a mouse double-click) might open the text editor containing main.tex and load section1-3.tex for editing, the cursor moved to the approximate location displayed in the PDF. How was that accomplished? The PDF page and cursor location provide a hint from data in main.synctex as to which source file produced the PDF page. The editor action must use the source file name to load it, then position the editor text cursor to the approximate location corresponding to the PDF.

Inverse Search.

Assume section1-3.tex open in the editor and inverse-search is requested (a menu item or mouse-click), then the PDF viewer should display a page to match the editor text. The editor must pass the file name section1-3.tex and the text cursor location to the PDF viewer, which in turn uses main.synctex to determine which page to display and what to highlight. Further understanding of how this works can be gleaned from the man page for Laurent's terminal program synctex, which is distributed with TeXLive2021.

The accuracy of the forward and inverse search is affected by where you click. If no synctex data is available for the spot, then surprises are possible. For instance, you click on a figure in the PDF and the text editor loads a file you never expected to edit. Or, you click the PDF table of contents and the text editor switches unexpectedly to a seemingly unrelated line in main.tex.