Wondering if there is any way to print out (for debugging purposes) the file name and the line number from where a macro is invoked (not where it is defined). My attempt to use the currfile
package only shows the main file name in the output:
**** ------ 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
The desired output is something like:
**** ------ 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
Notes:
- This is a follow-up to Redefine \newcommand to insert code at start and end of custom macros.
- I am only interested in doing this for my custom macros, not those that are part of other packages.
Code:
%\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}
\the\inputlineno
\MacroA
is invoked, not where\MacroB
is invoked, at least with my naive usage of it:Line=\the\inputlineno
.\MacroA
invokes\MacroB
, then\MacroB
will show the same line number as\MacroA
, in that call.