TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

Again and again I see, that TeXnicians define commands and do several things with them in the reverse order, I would do this as amateur. See this example:

In his answer to scrpage2: Three "marks" for headings egreg defines a \parttitle, and even more he applies also a patch:

\def\theparttitle{} % initialization

I would have done it in this way - first initialization, then real definition, and last patching:

\def\theparttitle{} % initialization

A test shows, that this works, as well. So, my question is:

What is the advantage of defining TeX commands backwards?
(At least from an amateur’s point of view …)

share|improve this question
+1 Great question. – Jubobs Mar 9 '14 at 18:44
up vote 19 down vote accepted

There's no technical difference, as long as content is available/declared when they are used. That's because TeX is a macro language where macros and their arguments are substituted for a replacement text - the latter is only evaluated when used (unless evaluated at definition, like with \edef or \xdef).

For example, looking at

\def\theparttitle{} % initialization

Following the lines above:

  1. \apptocmd adds stuff to \@part, so \@part should exist. At the time of definition (this insertion) though, \parttitle is not evaluated, and therefore does not have to exist.
  2. \def just defines \parttitle and doesn't really care about whether anything inside its replacement text exists.
  3. Some as (2).

From a TeXnician's point of view one might write the sequence this way by considering the source of the change as being \@part - the most important thing that requires a change; perhaps the last macro that actually sets the content. Subsequently, the change requires \parttitle, which is defined next. And \parttitle requires \theparttitle, which is defined next (as a safety/default). Viewing it differently makes just as much sense, by starting with the definition at the deepest level.

I think the main thing here is to understand that the TeX's macro-language uses

\<csname><argument text>{<replacement text>}

where <replacement text> is only considered/evaluated with a correct call to \<csname> (again, unless defined using some expansive technique (like \edef or \xdef).

share|improve this answer

The real truth is that I first had


with no initialization. Then I realized it was better to have \parttitle{#2} and a macro for setting \theparttitle anywhere needed. Then I remembered that an initialization was necessary.

But this style is very common in Knuth's macros. For example, in plain.tex

225 \outer\def\newcount{\alloc@0\count\countdef\insc@unt}
226 \outer\def\newdimen{\alloc@1\dimen\dimendef\insc@unt}
    <other similar lines>
236 \outer\def\newlanguage{\alloc@9\language\chardef\@cclvi}
237 \def\alloc@#1#2#3#4#5{\global\advance\count1#1by\@ne
238   \ch@ck#1#4#2% make sure there's still room
239   \allocationnumber=\count1#1%
240   \global#3#5=\allocationnumber
241   \wlog{\string#5=\string#2\the\allocationnumber}}


264 \outer\def\newif#1{\count@\escapechar \escapechar\m@ne
265   \expandafter\expandafter\expandafter
266    \def\@if#1{true}{\let#1=\iftrue}%
267   \expandafter\expandafter\expandafter
268    \def\@if#1{false}{\let#1=\iffalse}%
269   \@if#1{false}\escapechar\count@} % the condition starts out false
270 \def\@if#1#2{\csname\expandafter\if@\string#1#2\endcsname}
271 {\uccode`1=`i \uccode`2=`f \uppercase{\gdef\if@12{}}} % `if' is required

Presenting \@if before defining \newif would make it difficult to understand. It's always better to define auxiliary macros after the main one (if possible), so the role of each macro is clearer.

Last example

1017 \newbox\rootbox
1018 \def\root#1\of{\setbox\rootbox
1019   \hbox{$\m@th\scriptscriptstyle{#1}$}\mathpalette\r@@t}
1020 \def\r@@t#1#2{\setbox\z@\hbox{$\m@th#1\sqrt{#2}$}\dimen@\ht\z@
1021   \advance\dimen@-\dp\z@
1022   \mkern5mu\raise.6\dimen@\copy\rootbox \mkern-10mu\box\z@}

Allocating registers is best done before they are used, like declaring variables in programming languages. One might argue that in the code that started the discussion, \theparttitle is like a variable. However, TeX is based on macro expansion, so \theparttitle is actually a helper macro. If this had been written in LaTeX3, I'd have declared a token list variable beforehand.

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.