TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

LaTeX uses hooks extensively, one method employed is the command g@addto@macro. It is defined in source2e as:


I have used the g@addto@macro in the minimal example below:


\long\def\mainmacro{Preambles in main macro. \par}
\xdef\testOne{Contents of first macro. \par }
\xdef\testTwo{Contents of second macro. \par }

  \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}


What would be the best way to add a hook to the macro - at the beginning or for that matter anywhere but the end?

share|improve this question
See Patching Existing Commands on the UK TeX FAQ. – Matthew Leingang Dec 7 '10 at 19:03
@MatthewLeingang: The link is dead now. – krlmlr Jul 5 '12 at 11:16
Sorry! here's a new link: cs.sunysb.edu/documentation/latex/help/faq/uktug-faq/… – Matthew Leingang Jul 9 '12 at 15:35
up vote 10 down vote accepted
\def\foo{\message{Here is the original \string\foo}}

% Add a hook to \foo (\foohook does not have be predefined):

% Demo:
\def\foohook{\message{Redefined \string\foohook}}

(Complete working example; to turn it into latex, remove \bye and add necessary latex cruft.)

share|improve this answer
Thanks. Looks so much simpler than the LaTeX @g@addto@macro. TeX is fine too. For examples that don't use LaTeX packages I rather prefer TeX myself. – Yiannis Lazarides Dec 7 '10 at 16:30

Have a look at the etoolbox package, which provides a rich set of commands for “hook management” and even “patching” of commands.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.