# Override synctex position

How can I override or “fake” a synctex position?

Let's say we have a file foo.tex, where we define a command:

%% foo.tex
\newcommand{\foo}{Very much content here}


and a file bar.tex, where the command is used:

%% bar.tex
\foo


In this case synctex would jump to bar.tex, but I want it to jump to foo.tex.

Is there any possibility to create a command \setSynctexPosition that means something like “synctex, please synchronize me with the file foo.tex”, like this:

%% foo.tex
\newcommand{\foo}{%
\setSynctexPosition{foo.tex}{line 3}%
Very much content here%
}


Can this be done at all, for example by appending some text to the .synctex file with, e.g., \immediate\write18{echo '<some synctex commands>' >> \jobname.synctex}?

• Background: While some people even want synctex to be accurate on word level, in my case it is not even accurate on file level, because I am using a package which stores content in an auxiliary file and outputs it much later. This way, synctex synchronizes with the auxiliary file (which is also barely readable) and not with the actual content. – Scz Mar 19 '15 at 9:16
• SyncTeX works independently of TeX's \write streams and, as far as I know, there is no way for interacting with it from the user's level, because it works at the level where lists are transformed into boxes. – egreg Mar 19 '15 at 10:47
• Can I enter this level somehow (without recompiling latex)? – Scz Mar 19 '15 at 11:49
• I don't think so. – egreg Mar 19 '15 at 11:50

The design itself of SyncTeX makes what you're asking impossible: it hooks at the level where lists are transformed into boxes and where boxes are shipped out for output, with no influence whatsoever on the workings of TeX. In particular, its output file cannot be modified by \write, because it doesn't correspond to any TeX output streams.

You can read Jerôme Laurens paper on TUGboat describing the system and better explaining how it works than I can do here.

What you want might be possible with pdfsync that uses a different model, that is, inserting whatsits in the lists TeX is making. However, this package is obsolete and has many problems that make it moderately useful only during drafting of a document, not for the final typesetting, because the whatsits influence, often very badly, box alignment and production.

• Actually, if you think outside the box, it's not impossible ☺ → see my answer – Scz Mar 22 '18 at 13:53

Finally, I found a solution, which is surprisingly easy!

For the sake of completeness:

%% bar.tex
\documentclass{article}
\input{foo}
\begin{document}
\foo
\end{document}


And the solution:

%% foo.tex
\ifdefined\fakesynctexhelper\fakesynctexhelper\fi%% Does nothing at the beginning, helps later

\newcommand{\foo}{%
{%% group, so that if will not be defined later anymore
\def\fakesynctexhelper{%
Very much content here%% The actual desired content of \foo
%
\endinput%% Stop processing the file again
}%
\input{foo}%% input this file, so that the synctex information is updated.
}%
}


At the beginning of foo.tex, a helper command is run if it's defined. In the command \foo we define the helper command to do what \foo should do and call the file foo.tex, where this command is expanded at the beginning, so that synctex knows/thinks that foo.tex is processed. Additionally, the helper command is set up to stop further processing of the file foo.tex, so that it's original content is not processed again.