# Joining .synctex files of documents blended by \includepdf

When I typeset a journal issue, I first make the seperate articles, and I join them together (adding the Table of Contents and other stuff, that's irrelevant now).

When I read the final issue.pdf, I often find small mistakes in the articles. However, the Jump to Source in context menu in TeXworks brings me only to the instruction \includepdf in issue.tex, and not to the correct file a1/art1.tex.

Is there any way how to correctly join all the .synctex.gz files generated by all the articles and the issue itself, so that the forward-inverse-search works as I would like?

I appreciate partial or non-automated solutions and ideas as well.

MWE: File issue.tex

\documentclass{article}
\usepackage{pdfpages}
\begin{document}

\begin{center}
This is an issue 42 of Journal of funny \TeX\@ questions.\\
Contents:\\
Article One, on page 2\\
Article Two, on page 4
\end{center}

\includepdf[pages=-]{a1/art1.pdf}
\includepdf[pages=-]{a2/art2.pdf}

\end{document}

MWE: File a1/art1.tex

\documentclass{article}
\title{ARTICLE ONE}
\setcounter{page}{2}

\begin{document}
\maketitle
Some text. Some text. Some text.

\newpage
Other page of article one.
\end{document}

MWE: File a2/art2.tex

\documentclass{article}
\title{ARTICLE TWO}
\setcounter{page}{4}

\begin{document}
\maketitle
Some text. Some text. Some text.

\newpage
Other page of article two.
\end{document}
-
I don't see easy ways to do it. –  egreg Nov 24 '13 at 0:05
@egreg Well, SyncTeX is able to step in files that are \input or \included. So it might be possible to just correctly mix the XML files. After all, I keep, to some extent, the proper page numbering and stuff. I wanted to know whether there's anything done in this direction. If not, I'll consider seeing what can be done. –  tohecz Nov 24 '13 at 0:09
I don't think there is anything. But .synctex files are not XML. –  egreg Nov 24 '13 at 0:10
Damn, you're again right. I've always thought they are... –  tohecz Nov 24 '13 at 0:19
Tom is there any reason that you can't use combine or standalone? –  Harish Kumar Nov 24 '13 at 0:42

This is an algorithm how to generate the desired SyncTeX file. A full implementation is one the way but it will take a while... So for the moment I restrict my answer more or less to the application on the MWE. (A generalization seems possible in the future...)

1. Decompress the needed synctex files. The following shell script (save it as decompress-synctex) does this.

#!/bin/sh
SYNCGZ=.synctex.gz
SYNC=.synctex
for SFILE; do   # unzipping all files + save main file
gunzip -c $SFILE$SYNCGZ > $SFILE$SYNC
done
cp $1$SYNC $1-orig$SYNC
cp $1$SYNCGZ $1-orig$SYNCGZ

Run it as follows: ./decompress-synctex issue a1/art1 a2/art2

2. Insert the Input: line of art1.synctex ending with art1.tex e.g.

Input:1:/home/tux/testing/a1/./art1.tex

in issue.synctex after all the Input: lines at the beginning but before Content. Repeat this step with the other artX.synctex files.

3. In every synctex file, lines between {i until }i state the box records of page i. Such a box record line

(a) consists of "[", "(", "v", "h", "x", "k", "g", "$" followed by <input number>, ",", remaining material, or (b) is "]" or ")", or (c) starts with "!" and continues with a number (number of byte to get to the previous "!"). Now, the number after the : in a inserted Input: line has to be changed such that the numbering in all Input: lines in the entire issue.synctex is consecutive. If you change Input:1:...art1.tex to Input:42:...art1.tex, you also have to change all corresponding box records, that is, [1,... becomes [42,... but only for those pages (remember {i .... }i) that belong to Input:....art1.tex, and the same goes with "(", "v", "h", "x", "k", "g", "$" boxes.

Since this is not complicated enough, count the number of replacements in each page, too, (hopefully your editor does this for you while replacing) and add this number to the number after the "!" at the end of a page. If the number of digits increases by k digits, then finally, add k to the number after the next "!".

4. If no mistake is made, you are lucky and the inverse search will work as expected!

-
I've started writing a script to automate this algorithm. I'll add it if it is ready. –  e-birk May 24 at 13:45
Well, without the script, the algorithm is sort-of useless since the purpose is to simplify the forward-inverse-search, which is not done by manually editing files. Anyways, thanks for your input! –  tohecz May 24 at 18:00
Here is the link: synctex-inclpdf-alpha.sh. It is a shell script using awk. There is one restriction, which will be removed later, of course: the file setup must be as in your question except that the file contents can be changed if page breaks remain the same (i.e. same number of pages). After running pdflatex and before opening the issue.pdf the script must be run with ./synctex-includepdf-alpha.sh issue a1/./art1 a2/./art2 –  e-birk May 26 at 22:26