If I create an environment using environ and \BODY or xparse and +b, the synctex functionality breaks: instead of going to the appropriate line, it goes at the end of the environment. I guess that the fact that \BODY is put into a macro disturbs LaTeX, but I'm curious to know if I can solve it somehow (eventually in lualatex)
MWE
\documentclass[]{article}
\usepackage{environ}% http://ctan.org/pkg/environ
%% The +b is needed because in real life the text may be moved to another file
\NewDocumentEnvironment{testSynctex}{s+b}{
\IfBooleanTF{#1}{}{#2}%
}{}
\NewEnviron{testSynctexEnviron}{%
\BODY
}
\begin{document}
\section{xparse}
\begin{testSynctex}
This
is
a
long
text
try
to synctex
me !
\end{testSynctex}
\section{xparse*}
\begin{testSynctex}*
This
text
should
be
hidden
\end{testSynctex}
\section{environ}
\begin{testSynctexEnviron}
This
is
a
long
text
try
to synctex
me !
\end{testSynctexEnviron}
\end{document}
EDIT
The solution proposed by user202729 works nicely for the above MWE (and it definitely answers part of my question and will surely turn out to be useful if I can't find a more generalizable answer). However, here is another MWE that I'd like to solve where the solution proposed by user202729 does not work anymore:
I'm duplicating a text between two sections (by writing first the content to a file before inputting that file). Unfortunately, this breaks synctex: not only for the copied text (it goes to the dummy file instead of the main file), but also for the initial text (it goes to the end of the environment).
Would it be possible to make synctex work at least for the text in the first section? And if you can make it work also for the text in the second section… it would be awesome.
MWE:
\documentclass{article}
\def\nameOfFile{mydummyfile.tex}
%% Write to a file
\newwrite\appendwrite
\NewDocumentCommand\writetofile{m+m}{%
%% Open the file
\immediate\openout\appendwrite #1\relax%
%% Write the text to the file
\immediate\write\appendwrite{#2}%
%% Close the file
\immediate\closeout\appendwrite%
}
\NewDocumentEnvironment{duplicateContentKeepSynctex}{+b}{%
#1%
\writetofile{\nameOfFile}{#1}%
}{}
\begin{document}
\section{Main body}
\begin{duplicateContentKeepSynctex}
This content is duplicated to another section.
However synctex does not work in both sections.
Ideally I'd love to make synctex work in both sections (in such a way that it always links to the main file, NOT mydummyfile).
But I guess it's impossible.
But at least, is it possible to make it work for the first section?
\end{duplicateContentKeepSynctex}
\section{Duplicated section}
\input{\nameOfFile}
\end{document}
testSynctexcan't be replaced as far as I know with a normal environment as it deletes its content when called with a star, so I guess it still works as a MWE. – tobiasBora May 29 '22 at 18:17