Synctex: コンテンツにアクセスする環境で動作するようにする (例: environ または xparse +b オプション)

Synctex: コンテンツにアクセスする環境で動作するようにする (例: environ または xparse +b オプション)

environおよび\BODYまたは およびxparseを使用して環境を作成すると+b、synctex 機能が壊れます。適切な行に移動するのではなく、環境の末尾に移動します。\BODYマクロに が配置されていることが LaTeX を妨害していると思いますが、何らかの方法で解決できるかどうか知りたいです (最終的には lualatex で)

ムウェ

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

編集

user202729 が提案した解決策は、上記の MWE にうまく機能します (また、私の質問の一部に確実に答えており、より一般的な回答が見つからない場合に確実に役立つことがわかります)。ただし、user202729 が提案した解決策が機能しなくなった別の MWE を解決したいと考えています。

2 つのセクション間でテキストを複製しています (最初にそのファイルを入力する前に、そのファイルにコンテンツを書き込むことによって)。残念ながら、これにより synctex が壊れます。コピーされたテキスト (メイン ファイルではなくダミー ファイルに移動します) だけでなく、初期テキスト (環境の最後に移動) も壊れます。

少なくとも最初のセクションのテキストでは synctex を機能させることは可能でしょうか? また、2 番目のセクションのテキストでも機能させることができれば... 素晴らしいと思います。

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}

答え1

マクロ拡張言語でそれが可能になる方法がわかりません。マクロの内容は処理されないので、エラーメッセージやsynctexマーカーはマクロが使用されている場所でしか取得できず、定義の近くにない可能性があります。これらの場合、環境本体を保存する内部マクロは定義の近くで使用されるため、synctexデータはソースに近くなりますが、例をtexにすると次のようになります。

\def\abc{
some text

XXX

that gets saved here
}

multiple pages of arbitrary document source

\abc

そして、synctex が XXX を の定義の途中の 4 行目に関連付け、 が使用される\abc「複数ページ後」の行には関連付けないように求めています。\abc

答え2

さて、解決策です。

lualatex でコンパイルすると、両方のセクションの synctex が保持されますが、そうでない場合は最初のセクションのみが保持されます。

%! TEX program = pdflatex

\documentclass{article}
\usepackage{saveenv}
\usepackage{currfile}
\usepackage{rescansync}

  
\ExplSyntaxOn
\NewDocumentEnvironment{duplicateContentKeepSynctex}{}{%
  \rescansyncSaveenvghostPacked \savedcontent
}{
  \endrescansyncSaveenvghostPacked
}
\ExplSyntaxOff

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

\rescansyncPacked \savedcontent

\end{document}

実際にコンテンツをファイルに書き込むわけではなく、コンテンツは\savedcontentユーザーが触れてはいけない特殊な形式で保存されます。ユーザーがコンテンツを手動で操作したい場合は、rescansyncパッケージのプログラム API を使用します。

注記

  • CTANに掲載されていないパッケージパッケージは現在 CTAN にあります:https://ctan.org/pkg/rescansyncそこにパッケージのドキュメントがあります。

関連情報