6

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}?

4
  • 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, 2015 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, 2015 at 10:47
  • Can I enter this level somehow (without recompiling latex)?
    – Scz
    Mar 19, 2015 at 11:49
  • I don't think so.
    – egreg
    Mar 19, 2015 at 11:50

1 Answer 1

2

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.

2
  • Yes that works, Scz, however it seems to cause synctex to point to this line: \ifdefined\fakesynctexhelper\fakesynctexhelper\fi rather than this one: Very much content here If we could make it point to the content, that would be great.
    – texfjaeger
    Feb 22, 2020 at 1:18
  • In LuaTeX it's possible to do something like tex.set_synctex_line(10) tex.set_synctex_mode(2). Documentation is a little sparse, however.
    – user202729
    Jan 29 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.