LaTeX uses hooks extensively, one method employed is the command g@addto@macro
. It is defined in source2e as:
\long\def\g@addto@macro#1#2{%
\begingroup
\toks@\expandafter{#1#2}%
\xdef#1{\the\toks@}%
\endgroup}
I have used the g@addto@macro
in the minimal example below:
\documentclass{article}
\begin{document}
\long\def\mainmacro{Preambles in main macro. \par}
\xdef\testOne{Contents of first macro. \par }
\xdef\testTwo{Contents of second macro. \par }
\makeatletter
\g@addto@macro{\mainmacro}{\testOne}
\g@addto@macro{\mainmacro}{\testTwo \par}
\g@addto@macro{\mainmacro}{Adhoc contents (1). \par}
\g@addto@macro{\mainmacro}{\xdef\@elt{ Test } }
\g@addto@macro{\mainmacro}{Adhoc contents (2).\@elt \par}
\makeatother
\mainmacro
\end{document}
What would be the best way to add a hook to the macro – at the beginning or for that matter anywhere but the end?
Best Answer
(Complete working example; to turn it into latex, remove
\bye
and add necessary latex cruft.)