追蹤輸入 .tex 檔案內的所有巨集呼叫(透過 luatex?)

追蹤輸入 .tex 檔案內的所有巨集呼叫(透過 luatex?)

如果我們有一個輸入 .tex 文件,如何找到其中的所有巨集呼叫?

\documentclass{article}
\newcommand{\samplemacro}{This is a sample macro.}

\begin{document}
\samplemacro

\begin{itemize}
    \item \samplemacro
\end{itemize}
\section[\samplemacro]{\samplemacro}
\tableofcontents
\end{document}

任何類型的位置都可以,例如命令開始/結束的位元組偏移量。開始是命令開始被處理的時間,而結束是在所有參數都被消耗之後。

對於上面的例子,如果能得到以下結果就太好了:

[
  '\documentclass{article}',
  '\newcommand{\samplemacro}{This is a sample macro.}',

  '\begin{document}',
  '\samplemacro',

  '\begin{itemize}',
  '\item'
  '\samplemacro',
  '\end{itemize}',
  ['\section[\samplemacro]{\samplemacro}', '\samplemacro', '\samplemacro'],
  '\tableofcontents',
  '\end{document}'
]

確切的結構並不重要,因此可以將 \samplemacro 視為 \section 的一部分而不追蹤內部調用,或者可以將環境內容視為 \begin 的一部分。從字面上看,它是關於哪個例程消耗了哪些令牌。

由於像\section實作自訂解析這樣的宏,以某種方式使用 luatex 鉤子似乎比像這樣的硬編碼解析要容易得多德克斯湯正在努力做。

相關內容