![Synctex: コンテンツにアクセスする環境で動作するようにする (例: environ または xparse +b オプション)](https://rvso.com/image/449631/Synctex%3A%20%E3%82%B3%E3%83%B3%E3%83%86%E3%83%B3%E3%83%84%E3%81%AB%E3%82%A2%E3%82%AF%E3%82%BB%E3%82%B9%E3%81%99%E3%82%8B%E7%92%B0%E5%A2%83%E3%81%A7%E5%8B%95%E4%BD%9C%E3%81%99%E3%82%8B%E3%82%88%E3%81%86%E3%81%AB%E3%81%99%E3%82%8B%20(%E4%BE%8B%3A%20environ%20%E3%81%BE%E3%81%9F%E3%81%AF%20xparse%20%2Bb%20%E3%82%AA%E3%83%97%E3%82%B7%E3%83%A7%E3%83%B3).png)
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そこにパッケージのドキュメントがあります。