Synctex:使其與存取其內容的環境一起工作(例如environ或xparse +b選項)

Synctex:使其與存取其內容的環境一起工作(例如environ或xparse +b選項)

environ如果我使用and\BODYxparseand創建環境+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(它肯定回答了我的部分問題,如果我找不到更通用的答案,它肯定會很有用)。然而,這是我想要解決的另一個 MWE,其中 user202729 提出的解決方案不再起作用:

我在兩個部分之間複製文字(透過在輸入該文件之前先將內容寫入文件)。不幸的是,這會破壞synctex:不僅對於複製的文字(它轉到虛擬文件而不是主文件),而且對於初始文字(它轉到環境的末尾)。

是否有可能使synctex至少適用於第一部分的文字?如果你能讓它也適用於第二部分的文字…那就太棒了。

微量元素:

\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資料接近來源,但對於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它實際上不會將內容寫入文件,內容以某種用戶不應該接觸的特殊格式儲存。如果使用者想要手動操作內容,請使用rescansyncpackage.json 的程式設計 API。

筆記

相關內容