Expansion gives me headaches. I could use some help. The problem is identified under the heading: Log Output Snippet AtEndAfterFileList.
This is an extension of my context-based log entry question:
Goal
I want the following context-specific macros to be expanded before they are appended to \trackedmacrolist
, because I don't want to call \trackedmacrolist
until the very end.
\thenumber:\thetitle:[\thepage]:\currfilepath:\the\inputlineno
Code
\listfiles
\documentclass{article}
\usepackage{atveryend}
\usepackage{currfile}% adds \currfilepath
% Define a couple commands to track
\DeclareRobustCommand{\myproduct}[1]{MacGyver.\textit{#1}}
% The following is normally put into separate file as to modularize the application of patches
% Define a context macro as described
\def\contextinfo{\thenumber:\thetitle:[\thepage]:\currfilepath:\the\inputlineno}
\def\trackedmacro{Tracked Macro List}% initialize macro
\AtEndAfterFileList{\typeout{\trackedmacro}}
\makeatletter
% Apply Patches
\usepackage{regexpatch}
\xapptocmd{\myproduct}{\typeout{MacMessage: \contextinfo}\g@addto@macro{\trackedmacro}{^^J==> #1 (\contextinfo)}}{}{}% <-- I'd rather show MacGyver.monkey shampoo, but unsure how best to implement
% Note: \typeout is always fully expanded due to \write %https://tex.stackexchange.com/a/60976/13552
\makeatother
%\usepackage{xparse}% Loaded by regexpatch
\let\oldsection\section
\makeatletter
\RenewDocumentCommand{\section}{s o m}{%
\begingroup
% Extract the counter representation
\stepcounter{section}%
\xdef\thenumber{\thesection}%
\addtocounter{section}{-1}%
\protected@xdef\thetitle{#3}%
\endgroup
% Regular section
\IfBooleanTF{#1}
{\oldsection*{#3}}
{\IfValueTF{#2}
{\oldsection[#2]{#3}}
{\oldsection{#3}}}
}
\makeatother
\begin{document}
\section{About \myproduct{elephant shampoo}}
\section{About \myproduct{monkey shampoo}}
\section{About \myproduct{giraffe shampoo}}
\section{About \myproduct{tiger shampoo}}
\end{document}
Log Output Snippet running xelatex example.tex | grep MacMessage
Looks good here!
MacMessage: (1:About \myproduct {elephant shampoo}:[1]:example.tex:44)
MacMessage: (2:About \myproduct {monkey shampoo}:[1]:example.tex:45)
MacMessage: (3:About \myproduct {giraffe shampoo}:[1]:example.tex:46)
MacMessage: (4:About \myproduct {tiger shampoo}:[1]:example.tex:47)
Log Output Snippet AtEndAfterFileList
Not so good here
Tracked Macro List
==> elephant shampoo (4:About \myproduct {tiger shampoo}:[2]:example.tex:49)
==> monkey shampoo (4:About \myproduct {tiger shampoo}:[2]:example.tex:49)
==> giraffe shampoo (4:About \myproduct {tiger shampoo}:[2]:example.tex:49)
==> tiger shampoo (4:About \myproduct {tiger shampoo}:[2]:example.tex:49)
As you can see compared to the above output, the page numbers no longer correspond to where the macros exist in the code, rather to where the macros were first expanded: At the very end when \AtEndAfterFileList
was called.
Issues:
- All macros show section 4, which is the latest expansion
\thenumber
at the time of\end{document}
where\AtEndAfterFileList
is hooked to. - All macros expand to
\myproduct{tiger shampoo}
, which is the latest expansion of\thetitle
at the time of\end{document}
where\AtEndAfterFileList
is hooked to. - All macros show page 2, which is the latest expansion of
\thepage
at the time of\end{document}
where\AtEndAfterFileList
is hooked to. - All macros show line 49, which is the latest expansion of
\the\inputlineno
at\end{document}
where\AtEndAfterFileList
is hooked to.
Note that if I had an \input
at some point before that last \myproduct
call, I would expect a problem with the \currfilepath
too.
\myproduct
in the section title will apply\myproduct
twice -- in the body and when the ToC is written -- do you really want to have this information? – user31729 Jul 7 '16 at 13:28\thetitle
is the cause of your problem. – user31729 Jul 7 '16 at 14:09\myproduct
is just a way of creating lists of anything wrapped with that macro. If a product is obsolete, it can be quicker to identify obsolete products in the log output than reading the entire pdf. (both visually and programmatically). In reality I am generating quite an elaborate logging package calleddocdiagnostics
:). In a production environment where things are tested, maintenance costs have become an increasingly important topic. I am determined to demonstrate that (Xe)LaTeX is the tool for the job. – Jonathan Komar Jul 7 '16 at 14:11