确定调用宏的文件名和行号

确定调用宏的文件名和行号

想知道是否有任何方法可以打印出(出于调试目的)文件名和宏所在的行号援引(不是定义的地方)。我尝试使用包裹currfile仅在输出中显示主文件名:

**** ------ Enabling macro trace
**** DEBUG: (Depth=0) (File=TeX-SE.tex, Line=0) Started macro `\MacroA'.
**** DEBUG: (Depth=1) (File=TeX-SE.tex, Line=0) Started macro `\MacroB'.
**** DEBUG: (Depth=2) (File=TeX-SE.tex, Line=0) Started macro `\MacroC'.
**** DEBUG: (Depth=2) (File=TeX-SE.tex, Line=0) Completed macro `\MacroC'.
**** DEBUG: (Depth=1) (File=TeX-SE.tex, Line=0) Completed macro `\MacroB'.
**** DEBUG: (Depth=0) (File=TeX-SE.tex, Line=0) Completed macro `\MacroA'.
**** ------ Disabling macro trace

期望的输出类似于:

**** ------ Enabling macro trace
**** DEBUG: (Depth=0) (File=TeX-SE.tex, Line=59) Started macro `\MacroA'.
**** DEBUG: (Depth=1) (File=MyMacros.sty, Line=7) Started macro `\MacroB'.
**** DEBUG: (Depth=2) (File=MyMacros.sty, Line=3) Started macro `\MacroC'.
**** DEBUG: (Depth=2) (File=MyMacros.sty, Line=3) Completed macro `\MacroC'.
**** DEBUG: (Depth=1) (File=MyMacros.sty, Line=7) Completed macro `\MacroB'.
**** DEBUG: (Depth=0) (File=TeX-SE.tex, Line=59) Completed macro `\MacroA'.
**** ------ Disabling macro trace

笔记:

代码:

%\RequirePackage{filecontents}% <--- Commented out to prevent overwriting an existing file.
\begin{filecontents*}{MyMacros.sty}
    \newcommand*{\MacroC}[1]{cc#1cc}
    \newcommand*{\MacroB}[1]{\MacroC{CC}b#1b}
    \newcommand*{\MacroA}[1]{a\MacroB{#1}a}
\end{filecontents*}
%
\documentclass{article}
\usepackage{xparse}
\usepackage{currfile}

%\usepackage{letltxmacro}
%\LetLtxMacro{\OldUsepackage}{\usepackage}
%\renewcommand{\usepackage}[2][]{\edef\currfilename{#2}\OldUsepackage[#1]{#2}}%


\newcommand*{\CurrentLineNumber}{0}%

\newif\ifEnableTrace
\newcounter{NestingDepth}
\let\latexnewcommand\newcommand


\newcommand{\StartMacro}[1]{%
  \ifEnableTrace
    \typeout{**** DEBUG: (Depth=\arabic{NestingDepth}) (File=\currfilename, Line=\CurrentLineNumber) Started macro `\string#1'.}%
    \stepcounter{NestingDepth}%
  \fi
}
\newcommand{\EndMacro}[1]{%
  \ifEnableTrace
    \addtocounter{NestingDepth}{-1}%
    \typeout{**** DEBUG: (Depth=\arabic{NestingDepth}) (File=\currfilename, Line=\CurrentLineNumber) Completed macro `\string#1'.}%
  \fi
}

\NewDocumentCommand{\NewCommand}{smO{0}om}{%
  \IfBooleanTF{#1}
    {\IfNoValueTF{#4}{\latexnewcommand*{#2}[#3]}{\latexnewcommand*{#2}[#3][#4]}}%
    {\IfNoValueTF{#4}{\latexnewcommand{#2}[#3]}{\latexnewcommand{#2}[#3][#4]}}%
  {\StartMacro{#2}#5\EndMacro{#2}}%
}

% if you want to enable tracing
\let\newcommand\NewCommand

\usepackage{MyMacros}

\begin{document}

\MacroA{XXX}
\MacroB{YYY}

\EnableTracetrue
\typeout{**** ------ Enabling macro trace}
\MacroA{XXX}

\typeout{**** ------ Disabling macro trace}
\EnableTracefalse
\MacroB{YYY}

\end{document}

答案1

一般来说,这里的答案(比如它是)是“不”。TeX 的宏扩展意味着当\macroA读取时,它会被定义替换,然后定义就“在那里”。这个定义来自哪里,它是什么,ETC。并不重要。因此从 TeX 的角度来看,\macroA => \macroB这意味着\macroB无论它是否在这里定义,它都是“在这里”。同时,您必须记住,任何巧妙的重新定义以帮助跟踪材料都必须保持可扩展宏可扩展,这意味着没有分配、写入日志、ETC。,这限制了可以实现的目标。

相关内容