TeX Programming Notes
TeX Programming Notes
Abstract
This document contains notes which are intended for those who are interested in TEX programming.
It is valuable for beginners as a first start with a lot of examples, and it is also valuable for experienced
TEXnicians who are interested in details about TEX programming. However, it is neither a complete
reference, nor a complete manual of TEX.
Contents
1 Introduction 2
2 Programming in TEX 2
2.1 Variables in Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.1.1 Allocating Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.1.2 Using More than 256 Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2 Arithmetics in TEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.3 Expansion Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.3.1 Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3.2 Token Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.3.3 Summary of macro definition commands . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.3.4 Debugging Tools – Understanding and Tracing What TEX Does . . . . . . . . . . . . . 12
2.4 The Scope of a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.4.1 Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.4.2 Transporting Changes to an Outer Group . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.5 Branching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.5.1 Boolean Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.5.2 Special Cases for Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.6 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.6.1 Counting loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.6.2 Loops over list of items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.7 More On TEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4 Special Tricks 24
4.1 Handling # in Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Index 25
1
1 Introduction
This document is intended to provide a direct start with TEX programming (not necessarily TEX typesetting).
The addressed audience consists of people interested in package or library writing.
At the time of this writing, this document is far from complete. Nevertheless, it might be a good starting
point for interested readers. Consult the literature given below for more details.
2 Programming in TEX
2.1 Variables in Registers
TEX provides several different variables and associated registers which can be manipulated freely.
\counthnumi
There are 256 Integer registers which provide 32 Bit Integer arithmetics. The registers can be used for
example with \count0=42 or \count7=\macro where \macro expands to a number.
The value of a register can be typeset using \thehregister i.
The ‘=’ sign is optional and can be omitted. One thing is common among the registers: an assignment
of the form \count0=h· · · i expands everything which follows until the expansion doesn’t need more
numbers – even more than one following macro.
The precise rules can be found in [2], but it should be kept in mind that care needs to be taken here.
More than once, my code failed to produce the expected result because TEX kept expanding macros
and the registers got unexpected results. Here is the correct method:
The command \relax tells TEX to “relax”: it stops scanning for tokens, but \relax doesn’t expand to
anything.
2
\dimenhnumi
There are also 255 registers for fixed point numbers which are used pretty much in the same way as the
\count registers – but \dimen register assignments require a unit like ‘cm’ or ‘pt’.
String access with ‘\the’ works in exactly the same way as for \count registers.
The same rules with expansion of macros after assignments apply here as well.
The \dimen registers perform their arithmetics internally with 32 bit scaled integers, so called ‘scaled
point’ with unit ‘sp’. It holds 1pt=65536sp=216 sp. One of the 32 bits is used as sign. The total number
range in pt is [−(230 − 1)/216 , (230 − 1)/216 ] = [−16383.9998, +16383.9998]1 .
\tokshnumber i
There are also 255 token registers which can be thought of as special string variables. Of course, every
macro assignment \def\macro{hcontenti} is also some kind of string variable, but token registers are
special: their contents won’t be expanded when used with \the\tokshnumber i. This can be used for
fine grained expansion control, see Section 2.3 below.
Note the white space after \the\toks0: its purpose is to stop the number parsing when TEX scans for
0. The white space is discarded.
Token registers can also contain the special token # which would typically have a special meaning inside
of macros:
\toks0={#1}%
\message{Meaning is \the\toks0}%
3
\newdimen\variable
\variable=42pt
The resulting h\macronamei can now be used in the same way as if you used the register directly. In
fact, it is often simpler because you do not need to worry about the register’s number.
The allocation relies on some global integer variable which is increased for each allocation. This ensures
that variables stored in such allocated variables do not accidentally overwrite the contents of some other
variable.
Note that deallocation is impossible.
While it is perfectly reasonable to allocate long–living variables, one should avoid the allocation of a new
variable just because one needs a “new” temporary variable.
It makes sense to allocate a couple of named variables like \tempa, \tempb, or something like that and
reuse these values for every temporary evaluation. Clearly, care needs to be taken to avoid unintended
overwrites.
It is also possible to use token registers as explained above. However, the usage should be protected by
means of groups:
toks3 inside of group: Value inside of group
toks3 outside of group: Value outside of group
\toks3={Value outside of group}
\begingroup
\toks3={Value inside of group}
toks3 inside of group: \the\toks3
\endgroup
Groups constitute TEX’s concept of “scope” and are explained somewhere else in this document.
early in your preamble. This is actually a very good idea: it allows access to 65536 registers. Today’s
documents which involve lots of packages actually need etex.
Note that even etex does not justify wild and uncontrolled allocated of registers just to store temporary
variables.
If you want almost unlimited temporary variables, you should store the temporaries in macros. This,
of course, involves conversion from numbers to string, but it is the only save way which avoids the limited
number of registers.
4
\multiplyhregister i byhinteger i
\dividehregister i byhinteger i
This allows integer division by hinteger i with truncation.
This is actually all that TEX allows. One needs powerful macro packages like pgf with its
\pgfmathparse{hexpressioni} to do some “real” arithmetics.
Note that the limited number range of these registers also applies to the result of any numerical operation.
5
2.3.1 Macros
We have already seen some applications of macros above. Actually, most users who are willing to read notes
about TEX programming will have seen macros and may have written some on their own – for example using
\newcommand (\newcommand is a “more high–level” version of \def used only in LATEX).
A macro has a name and is treated as an elementary token in TEX (even if the name is very long).
A macro has replacement text. As soon as TEX encounters a macro, it replaces its occurrence with the
replacement text. Furthermore, a macro can consume one or more of the following tokens as arguments.
Executing it: ‘This here is actually the replacement text.’.
\def\macro{This here is actually the replacement text.}
Executing it: ‘\macro’.
This here is not really a surprise. What might come as a surprise is that the accepted arguments can be
pretty much anything.
Invoking it: replacement with arguments: ‘a’ and ‘sign’.
\def\macro#1-#2.{replacement with arguments: ‘#1’ and ‘#2’.}
Invoking it: \macro a-sign.
The last example \macro runs through the token list which follows the occurrence of \macro. This token list
is “a-sign.”. Macro expansion is greedy, that means the first matching pattern is used. Now, our \macro
expected something, then a minus sign ‘-’, then another (possibly long) argument, then a period ‘.’. The
argument between \macro and the minus sign is available as #1 and the tokens between the minus sign and
the period as #2.
I found arguments ‘42’, ‘43’ and ‘44’.
\def\macro(#1,#2,#3){I found arguments ‘#1’, ‘#2’ and ‘#3’.}
\macro(42,43,44)
As we have seen, macros can be used to manipulate the input tokens by expansion: they take some input
arguments (maybe none) away and insert other tokens into the input token list. These tokens will be the
next to process. We will soon learn more about that.
There is a command which helps to understand what TEX does here:
\meaninghmacroi
This command expands to the contents of hmacroi as it is seen by TEX.
The last example already shows something about \def: the replacement text can still contain other
macros.
6
\def\macroone{This is macro one}
\def\macrotwo{Macro two contains \macroone.}
Now, I execute it: \macrotwo.
\def\macroone{Redefined macroone}
Now, I execute the second macro again: \macrotwo.
Macros can be defined almost everywhere in a TEX document. They can also be invoked almost every-
where.
The hargument patterni is a token list which can contain simple strings or macro parameters ‘#hnumber i’
or other macro tokens. The hnumber i of the first parameter is always 1, the second must have 2 and
so on up to at most 9. Valid argument patterns are ‘#1#2#3’, ‘(#1,#2,#3)’ or ‘---\relax’. If TEX
executes a macro, it searches for hargument patterni in the input token list until the first match is found.
If no match can be found, it aborts with a (more or less helpful) error message.
Got ‘g’
\def\macroone abc{\macrotwo}
\def\macrotwo def{\macrothree}
\def\macrothree#1{Got ‘#1’}
\macroone abcdefg
The last example contains three macro definitions. Then, TEX encounters \macroone. The input token
list is now
‘\macroone abcdefg’.
The space(s) following \macroone are ignored by TEX, they delimit the h\macronamei. Now, TEX
attempts to find matches for hargument patterni. It expects ‘abc’ – and it finds ‘abc’. These three
tokens are removed from the input token list, and TEX inserts the replacement text of \macroone which
is \macrotwo. At that time, the input token list is
‘\macrotwo defg’.
Now, the same game continues with \macrotwo: TEX searches for the expected {hargument patterni}
which is ‘def’, erases these tokens from the input token list and inserts the replacement text of \macrotwo
instead. This yields
‘\macrothree g’.
Finally, \macrothree expects one parameter token (or a token list enclosed in parenthesis). The next
token is ‘g’, which is consumed from the input token list and the replacement text is inserted – and ‘#1’
is replaced by ‘g’. Then, the token list is
‘Got ‘g’’.
This text is finally typeset (because it doesn’t expand further).
What we have seen now is how TEX macros can be used to modify the token list. It should be noted
explicitly that macro expansion does is in no way limited to those tokens provided inside of {hreplacement
texti} – if the last argument in {hreplacement texti} is a macro which requires arguments, these arguments
will be taken from the following tokens. Using nested macros, one can even process a complete part of the
token list, in a manner of loops (but we don’t know yet how to influence macro expansion conditionally, that
comes later).
Let’s try to solve the following task. Suppose you have a macro named \point with hargument patterni
‘(#1,#2)’, i.e.
\def\point(#1,#2){we do something with #1 and #2}.
Suppose furthermore that you want to invoke \point with the contents which is stored in another macro.
After all, macros are some kind of string variables – it makes sense to accumulate or generate string vari-
ables which will then be used as input for other macros. Let’s assume we have \temp and \temp contains
‘(42,1234)’. A first choice to invoke \point would be to use \point\temp. But: \point searches for an
argument pattern which starts with ‘(’, not with \temp! The invocation fails.
\expandafterhtokenihnext tokeni
The \expandafter command is an – at first sight confusing – method to alter the input token list. But:
it solves our problem with \point\temp!
7
\def\point(#1,#2){we do something with #1 and #2}
\def\temp{(42,1234)}
\expandafter\point\temp
Why did that work!? The command \expandafter scans for the token after \expandafter in the
input token list. This is \point in our case. Then, it scans for the next token which is \temp in our
case (remember: macros are considered to be elementary tokens, just like characters ‘a’ or so). The
two scanned arguments are removed from the input token list. Then, \expandafter expands the hnext
tokeni one time. In our case, hnext tokeni is \temp. The first level of expansion of \temp is ‘(42,1234)’.
Then, \expansion inserts the (unexpanded) htokeni followed by the (expanded) contents of hnext tokeni
back into the input token list. In single steps:
1. \expandafter\point\temp
2. Expand \expandafter: next two tokens are ‘\point\temp’.
3. Use \point as htokeni and \temp as hnext tokeni.
4. Expand \temp once, which leads to the tokens ‘(42,1234)’.
5. re-insert htokeni and the expansion of hnext tokeni back into the input token list. The list is then
‘\point(42,1234)’.
6. Expand \point as next token.
Now, what happens here? Let’s apply the rules step by step again:
1. After the initial definitions, the token list is \expandafter\theimportantmacro\expandafter{\temp}.
2. TEX expands \expandafter, using \theimportantmacro as htokeni and the second \expandafter
as hnext tokeni.
3. According to the rules, TEX expands hnext tokeni once. But: hnext tokeni is again a macro, namely
\expandafter! Does that make a difference? No:
(a) The token list after the second \expandafter is ‘{\temp}’ (3 tokens).
(b) The htokeni is thus ‘{’ and hnext tokeni is ‘\temp’.
(c) The expansion of hnext tokeni is ‘xyz’.
(d) The second \expandafter re-inserts its htokeni and expanded hnext tokeni, which is
‘{xyz’.
Note that the closing brace ‘}’ has not been touched at all, TEX hasn’t even seen it so far.
We come back from the recursion. Remember: htokeni is \theimportantmacro and the top-level
expansion of hnext tokeni is – as we have seen above – ‘{xyz’.
4. TEX re-inserts htokeni and the expansion of hnext tokeni to the input token list, which leads to
‘\theimportantmacro{xyz}’.
The closing brace ‘}’ has not been touched, it simply resides in the input token list.
5. TEX expands \theimportantmacro.
The hnext tokeni is expanded exactly once. We have already seen that if hnext tokeni is a macro which
does substitutions on its own, these substitutions will be performed recursively. But what means ‘once’
exactly? We will need to use \meaning to check that (or the \tracingmacros tools) because we need
to see what TEX does.
So far, nothing has been typeset. But now: 4[This is macro one –2–].
8
\def\macroone{This is macro one \macrotwo}
\def\macrotwo{--2--}
\def\macrothree#1{\def\macrofour{4[#1]}}
\expandafter\macrothree\expandafter{\macroone}%
So far, nothing has been typeset. But now: \macrofour.
\message{We have macrofour = \meaning\macrofour}%
\def\a{3}
\def\b{2\a}
\def\c{1\b}
\def\d{value=\c}
\message{Macro ‘d’ is defined to be ‘\meaning\d’}
\edef\d{value=\c}
\message{Macro ‘d’ is e-defined to be ‘\meaning\d’}
\expandafter\def\expandafter\d\expandafter{\c}
\message{Macro ‘d’ is defined to be ‘\meaning\d’ using expandafter}
\edef\d{\count0=42}
\message{Macro ‘d’ is defined to be ‘\meaning\d’}
\def\a{1234}
\edef\d{\advance\count0 by\a}
\message{Macro ‘d’ is defined to be ‘\meaning\d’}
9
Macro ‘d’ is defined to be ‘macro:->\count 0=42’ and
Macro ‘d’ is defined to be ‘macro:->\advance \count 0 by1234’.
So, assignment and arithmetics operations are not expandable, they remain as executable tokens in the
newly defined macro. This does also hold for \let and other assignment operations.
Interestingly, conditional expressions using \if · · · \fi are expandable, but we will come to that later.
There is also a method to convert a macro temporarily into an unexpandable token: the \noexpand
macro.
\noexpandhexpandable tokeni
The \noexpand command is only useful inside of the {hreplacement texti} of an \edef command. As
soon as \edef encounters the \noexpand, the \noexpand will be removed and the hexpandable tokeni
will be converted into an unexpandable token. Thus, the code
I can’t give numbers for the first point – I have just read it in [2]. But the second point allows expansion
control. While \edef allows “infinite” expansion, token registers allow only top–level expansion, just like
\expandafter. But they can be used in a more flexible (and often more efficient) way than \expandafter.
The following examples demonstrates the second point.
\toks0={A \token list \a \b \count0=42 will never be expanded}
\edef\d{\the\toks0 }% the space token is important!
\message{Macro ‘d’ is defined to be ‘\meaning\d’}
3 The \noexpand key is actually used to implement the LATEX command \protect: LATEX’s concept of moveable arguments
is implemented with \edef.
10
The first three lines define our pieces. Each of the macros \piecea, \pieceb and \piecec contains tokens
which should not be expanded during the definition of \d. The three following lines assign the top-level
expansion of our pieces into token registers. Since \toks0={\piecea} would have stored ‘\piecea’ into the
token register, we need to use \expandafter here4 . Then, we use \the\tokshnumber i to insert the contents
of a token list somewhere – in our case, into the expanded replacement text of our macro \d. Thus, the
complete example yields the log–output
Macro ‘d’ is defined to be ‘macro:->I have \a {xyz}and \count 0=42 and string \b ’.
It is possible to get exactly the same result using (a lot of) \expandafters. Don’t try it.
\leth\newmacroi=htokeni
Defines or redefines \newmacro to be an equivalent to htokeni. For example, \let\a=\b will create a
new copy of macro \b. The copy is named \a, and it will have exactly the same {hreplacement texti}
and hargument patterni as \b.
It is also possible that htokeni is something different than a macro, for example a named register or a
single character.
\csnamehexpandable tokensi\endcsname
This command is not a macro definition, it is a definition of a macro’s name. The “cs” means “control
sequence”. The \csname, \endcsname pair defines a control sequence name (a macro name) using
hexpandable tokensi. The control sequence character ‘\’ will be prepended automatically by \csname.5
The example demonstrates that \csnamehexpandable tokensi\endcsname is actually the same as if you
had written \hexpandable tokensi directly – but the \csname construction allows much more tokens
inside of macro names:
4 We could have eliminated the \piece* macros by writing everything into token registers directly. But I think this example
is more realistic.
5 In fact, the contents of \escapechar will be used here. If its value is -1, no character will be prepended. The same holds
11
The example uses \expandafter to expand \csname one time. The top–level expansion of \csname
is a single token, namely the control sequence name. Then, \def is used to define a macro with the
prepared macro name.
When \csname is expanded, it parses all tokens up to the next \endcsname. Those tokens will be
expanded until only unexpandable tokens remain (as in \edef). The resulting string will be used to
define a macro name (with the control sequence character ‘\’ prepended). The fact that hexpandable
tokensi is expanded allows to use “indirect” macro names:
I suppose the example is self-explaining, up to the \string command which is described below.
Due do this flexibility, \csname is used to implement all (?) of the available key–value packages in TEX.
\stringh\macroi
This command does not define a macro. Instead, it returns a macro’s name as a sequence of separate
tokens, including the control sequence token ‘\’.
You can also use \string on other tokens – for example characters. That doesn’t hurt, the character
will be returned as-is.
\meaningh\macroi
\tracingmacros=2
\tracingcommands=2
\tracingrestores=1
{
++i;
int i = 5;
}
changes the value of the outer i to 43. The inner i is 5, but it will be deleted as soon as the closing brace is
encountered. It may even be possible to access both, the value of the inner i variable and the value of the
outer i variable, at the same time.
In TEX, braces are also used for scopes. But: while TEX will also destroy any variables (macros) defined
inside of a scope at the end of that scope, it will also undo any change which has been applied inside of that
scope.
12
The value of \i is now 42.
\def\i{42}
{
\def\i{43}
\def\b{2}
}
The value of \textbackslash i is now \i.
The listing above defines \i, enters a local scope (a TEX “group”) and changes \i. However, due to
TEX’s scoping rules, the old program state will be restored completely after returning from the local group!
Neither the change to \i nor the definition of \b will survive. The same holds for register changes or other
assignments.
TEX groups can be created in one of three ways: using curly braces6 , using \begingroup or using \bgroup.
Curly braces are seldom used to delimit TEX groups because the other commands are more flexible. If one
uses curly braces, they need to match up – it is forbidden to have unmatched curly braces.
\begingroup
Starts a new TEX group (a local scope). The scope will be active until it will be closed by \endgroup.
The \endgroup command can occur later in the main token list.
\endgroup
Ends a TEX group which has been opened with \begingroup.
\bgroup
A special variant of \begingroup which can also be used to delimit arguments to \hbox or \vbox (i.e.
it avoids the necessity to provide matched curly braces in this context).
The \bgroup macro is also useful to test whether the next following character is an opening brace (see
\futurelet).
If one just needs to open a TEX group, one should prefer \begingroup.
\egroup
Closes a preceding \bgroup.
TEX does not know how to write into macros of an outer scope – except for the topmost (global) scope.
This restriction is quite heavy if one needs to write complex structures: local variables should be declared
inside of local groups, but changes to the structure should be written to the outer group. There is no direct
possibility to do such a thing (except global variables).
\globalhdefinition or assignmenti
The definition which follows \global immediately will be done globally.
{
\global\def\a{123}
\global\advance\count0 by3
\global\toks0={34}
}
13
\globaldefs=-1|0|1 (initially 0)
I cite from [2]: “If the \globaldefs parameter is positive at the time of an assignment, a prefix of
\global is automatically implied; but if \globaldefs is negative at the time of the assignment, a prefix
of \global is ignored. If \globaldefs is zero (which it usually is), the appearance of nonappearance
of \global determines whether or not a global assignment is made.”
• Copy the macro into a global, temporary variable (or even token register) and get that value after the
scope.
\def\initialvalue{0}
{
% do something:
\def\initialvalue{42}
\global\let\myglobaltemporary=\initialvalue
}
\let\initialvalue=\myglobaltemporary
The idea is that \myglobaltemporary is only used temporary; its value is always undefined and can
be overwritten at any time. This allows to use a local variable \initialvalue.
Please note that you should not use variables both globally and locally. This confuses TEX and results
in a slow-down at runtime.
• “Smuggle” the result outside of the current group. I know this idea from the implementation of [4]
written by Mark Wibrow and Till Tantau. The idea is to use several \expandafters and a \def to
redefine the macro directly after the end of the group:
\def\smuggle#1\endgroup{%
\expandafter\endgroup\expandafter\def\expandafter#1\expandafter{#1}%
}
\begingroup
\def\variable{12}
\edef\variable{\variable34}
\edef\variable{\variable56}
\smuggle\variable
\endgroup
The technique relies on groups started with \begingroup and ended with \endgroup because un-
matched braces are not possible with \def. The effect is that after all those \expandafters, TEX
encounters the token list
\endgroup\def\variable{123456}
at the end of the group.
• Use the aftergroup stack. TEX has a special token stack of limited size which can be used to re-insert
tokens after the end of a group. However, this does only work efficiently if the number of tokens which
need to be transported is small and constant (say, at most three). It works by prefixing every token
with \aftergroup, compare [2] for details.
Sometimes one needs to copy other variables outside of a scope. The trick with a temporary global
variable works always, of course. But it is also possible to define a macro which contains commands to apply
any required changes and transport that macro out of the scope.
14
2.5 Branching
Here we discuss some of the available branching constructions of TEX, with emphasis on conditions involving
numbers and tokens.
Here, we have defined a token \empty to be a replacement for \empty and subsequently have compared
whether these two tokens are equal in first-level expansion. Note that the definition is actually nonsense.
If TEX ever were to go through the whole expansion – i.e. we would put \empty somewhere else – it
would do so indefinitely. However, with \ifx only first-level expansion is done and compared. Hence,
the statement evaluates to true.
Have a look at the following example:
On first glance, this should evaluate to true: \empty is defined as a replacement for \relax. But it does
not. Why?
\empty is expanded to \relax, however \relax expanded has a different meaning, namely stop scanning
and not \relax anymore. Hence, they are different and the statement is false! If the expansion in \ifx
were to be taken till maximum, both would be equal but not in the case of a comparison on first-level
expansion only.
15
ifhtoken1 ihtoken2 ihtrue-block i\elsehfalse-block i\fi
The \if comparison is closely related to the \ifx conditional, with one major exception: it expands
tokens until it finds the next two unexpandable tokens. If these two tokens are the same, it expands to
the htrue-block i, otherwise to the hfalse-block i.
The \if conditional should be handled with care as it might produce undesirable effects. Use it only if
you know what you do.
A useful example is if you know that a macro contains at most one character, and you want to test for
a particular one:
1. \if...\else...\fi is expandable (including each of the single macros \if..., \else and \fi), which
means you can even use it inside of \edef:
This example is tricky. What would have happened without the \expandafter!? Well,
\shownexttoken would be invoked with #1=\fi. This would lead to an error because the \fi would
be missing, and it would spoil the effect since we do not want the \fi to be seen – we expected #1=2.
The \expandafter first expands \fi (which simply removes the \fi without further effect) such that
\shownexttoken will see the 2 token in our example above. This would also have worked if there was
an \else branch instead of \fi.
16
2. You should generally make sure that the matching \else or \fi tokens are “directly reachable”, i.e.
without token expansion.
The background here is that TEX works on a token–based level: Whenever it encounters an \if. . .
statement, it evaluates it and scans tokens to find the matching end part (either an \else or an \fi
token). But it will not expand tokens during this scan, although it will count nested \if...\fi pairs!
Thus, if you are careless, it might become confused and your conditional will go awry.
2.6 Loops
As you have seen, in TEX we have a very specific control over token expansion. This makes it possible to
construct even loops via means of recursion. In essence, a loop consist of the following parts:
• counter or, more generally, list of items
• incrementor, or more generally, a next item picker
• threshold or, more generally, an end list marker
• a check of the threshold or end marker, respectively
Reafing through the sections above, we realize that all of this is actually in place: We do know about
counters, we do know about branching. Only the specifics of how to create these loops is still to be made
clear. We will show both cases, the counting loop and the loop over a list of items in the following in detail.
In general, for a loop done via a recursion we need two definitions: One for the loop start and another
for the loop step.
\long\def\loopcounter#1#2#3{%
#3%
\ifnum#1=#2 %
\else%
\advance#1 by1 %
\loopcounter{#1}{#2}{#3}%
\fi%
}
\countingloop{\count0} in 0:{3}{%
The current value is ‘\the\count0’\par
}
17
letters are two tokens and – ungrouped – become two arguments. Here, we have to group the threshold
to make clear what we mean.
• One last thing becomes clear first when debugging is activated: As loops are done by recursion, i. e.
by expansion followed by expansion till some threshold is reached, we will end with a lot of \fis in
the above case. If we place \tracingmacros=2 \tracingcommands=2 before the \countingloop call
and inspect the log file this will become apparent. This is bad because TEX will keep a stack frame
open for each \if. . . \fi sequence. If we now have a loop over 10.000 items . . .
• It is not good practice to use one of the system counters, here \count0, because one can never be sure
that is not used for something else or changed somewhere else. E. g. when the page is full, TEX will
interrupt the current sequence of tokens to deal with creating a new page and finishing the old one, in
this course changing \count0. Hence, we should also create our own counter.
Hence, we modify the example as follows:
The current value is ‘0’
The current value is ‘1’
The current value is ‘2’
The current value is ‘3’
\long\def\countingloop#1 in #2:#3#4{%
#1=#2 %
\loopcounter{#1}{#3}{#4}%
}
\long\def\loopcounter#1#2#3{%
#3%
\ifnum#1=#2 %
\let\next=\relax%
\else
\advance#1 by1 %
\def\next{\loopcounter{#1}{#2}{#3}}%
\fi
\next
}
\newcount\ourcounter
\countingloop{\ourcounter} in 0:{3}{%
The current value is ‘\the\ourcounter’\par
}
Principally, nothing has changed in terms of the output. However, notice that we have introduced the
macro \next which either recurses into the next level – but after the \fi statement has been given – or ends
the recursion by simply containing \relax. Also, we have declared a new counter called \ourcounter that
is safe from harm.
Finally, let us briefly summarize what happens in detail:
1. \countingloop. . . is expanded to an assignment #1=#2 and another macro \loopcounter. . . .
2. The assignment is done: \ourcounter is set to the starting value 0.
3. The actual loop macro is expanded to the command block – printing the current value – and an if
statement.
4. The current value is printed.
5. \ourcounter is compared to the threshold 3 and . . .
• False, i. e. the if statement is expanded to an \advance statement followed by defining \next to
be another call of the same macro loop.
• True, i. e. \next is set to be just \relax.
6. The statement is still false: \advance will increase \ourcounter by one, it is now 1. \next is set to
the loop macro.
7. The loop macro is again expanded, go to step 3. \ourcounter is . . . 2 . . . \ourcounter is 3.
8. Now the statement is true: \next is expanded to \relax and nothing happens.
18
2.6.2 Loops over list of items
Looping over a list of items is very similar, only we will need \ifx in place of \ifnum and we need some
end marker instead of the threshold value. However, how do we specify the list itself? Let’s make some
comma-separated list, e. g. {a,b,c,d} and call the end marker \listingloopENDMARKER.
The current item is ‘a’
The current item is ‘
b’
The current item is ‘
c’
The current item is ‘
’
The current item is ‘
d’
The current item is ‘
e’
\def\listingloopENDMARKER{\par \listingloopENDMARKER}
\long\def\listingloop#1in#2#3{%
\looppicker{#1}{#3}#2,\listingloopENDMARKER,%
}%
\long\def\looppicker#1#2#3,{%
\def\tempitem{#3}%
\ifx\tempitem\listingloopENDMARKER
\let\next=\relax%
\else
\def#1{#3}%
#2%
\def\next{\looppicker{#1}{#2}}%
\fi
\next
}%
\listingloop\x in{a,b,c,,d,e}{%
The current item is ‘\x’
}
19
In addition to the paper mentioned above and the extensive reference manual for pdfkeys in [4], I give a
brief survey over pgfkeys here. The addressed audience is primarily package writers or macro programmers.
This section should allow you to define your own user interfaces and styles for pgfplots and for pgf. It
should also improve the understanding of pgfkeys and how it is to be used. I also address the topic of key
filtering which is mainly useful for package writers.
The package pgfkeys is available as stand–alone package \usepackage{pgfkeys}. However, I believe
that you never need to load it explicitly as pgf will be loaded anyway and pgf always loads pgfkeys.
It comes with two user interfaces. I believe that it is a best–practice to use the best of both worlds;
although it might be sufficient to use just one of them. Consequently, I discuss both of them and propose a
best–practices afterwards.
\pgfkeysgetvalue{/notes/key}\temp
There is few magic around these two keys; it is just like a hashmap access with some special naming
convention for the keys (due to the key path). Note that since “hashmap access” is what TEX does all the
time when it handles macros, we could have replaced the pair \pgfkeyssetvalue/\pgfkeysgetvalue
by \def and suitable \let commands, perhaps combined with \csname...\endcsname. The advantage
of pgfKeys comes into play as soon as we inspect the high–level user interface in the next section.
Note that since \pgfkeyssetvalue is essentially the same as a suitable \def, the assignment is local to
the current TEX group. In other words: the assignment will be undone by the next closing curly brace,
or the next \endgroup, or the next \end{henvironmenti}.
7 Note that key@ is unrelated to pgfKeys.
20
\pgfkeyslet{h/key path/key namei}{h\macroi}
This is essentially the same as \pgfkeyssetvalue, except that the key’s value is already available inside
of h\macroi:
\pgfkeysgetvalue{/notes/key}\temp
Just like \pgfkeyssetvalue boils down to \def, \pgfkeyslet boils down to \let.
However, this key has one major advantage: it can be used inside of an \edef (because it is fully
expandable):
It boils down to a suitable \csname ... \endcsname. Consequently, it expands to \relax if the key
happens to be undefined (see \pgfkeysifdefined below).
Note that in this case, we have to use \pgfeov to terminate the argument list. We could have placed our
argument into curly braces, but we have to provide \pgfeov; just as we had to add the suffix /.@cmd.
8 Note that the suffix /.@cmd is part of the public Api of pgfKeys, so it is no hackery to make use of it.
21
\pgfkeysifdefined{h/key path/key namei}{htrue casei}{hfalse casei}
\pgfkeysifassignable{h/key path/key namei}{htrue casei}{hfalse casei}
These keys provide conditionals based on existance or type of a key. Please refer to the reference manual
in [4] for details.
% key usage:
\pgfkeys{
/notes/key = efg ,
}
There are some items which appear to be clear, and I will briefly confirm that it really is clear: white
spaces before and after the key name and before and after the value are stripped away. Furthermore, trailing
commas are ignored. Note that trailing commas are a best–practice: always insert trailing commas. This
simplifies the addition of further keys significantly (I can’t remember how often I added a key and wondered
why it was not properly recognised until I found the missing comma). Just add the trailing comma as a
habit. Another good practice is to indent code properly, i.e. to insert a tab stop for every new line. It is also
a good idea to provide one key per line, although all that stuff is optional.
The first thing which is strange when inspecting the actual code is the suffix ‘/.initial’. This is, in
fact, a consistent new system of pgfKeys: these suffixes allow to configure and modify the keys to which
them apply. They are called “key handlers”. Whenever you encounter hkey path/key namei followed by
‘/.hhandler i’, you can safely assume that hkey path/key namei is about to be reconfigured or modified.
Knowledge of key handlers means control over pgfKeys. In the following, I will briefly discuss the most
important handlers.
22
Key handler hkeyi/.code={hbodyi}
This key handler defines a new code–key hkeyi with hbodyi as result.
Execute the key using the simple API:Expansion with value abc—X.... execute the key using assign-
ment in the standard API:Expansion with value abc—X.
\pgfkeys{
/notes/code key/.code={Expansion with value #1---X.},
}%
We see that assignment of a code key means to executing hbodyi where #1 is set to the value assigned
in the Api.
A key defined by means of /.code is equivalent to one defined by means of \pgfkeysdef.
Note that the argument hbodyi can be surrounded by curly braces, but it does not need to be:
\pgfkeys{
/notes/code key/.code={Expansion with value #1---X.},
/notes/code key/.code=Expansion with value #1---X.,
}%
This is a common feature of pgfKeys: any kind of value assignment can use braces, but it does not
need to. You only need to use curly braces if the assigned argument (in our hbodyi) contains control
characters of pgfKeys (i.e. = or ,).
Definition has been done. Assigning the style:OK. Value of A=42, value of B=42.
\pgfkeys{
/notes/A/.initial=,
/notes/B/.initial=,
/notes/my style/.style={
/notes/A={#1},
/notes/B={#1},
},
}%
Our example is a very simple application of a style: it sets a bunch of other options.
Note that hoption listi can depend on #1.
So far, this document did always provide fully qualified key paths. However /.style explicitly supports
the notion of a “current key path”: if a “current key path” is in effect, hoption listi will be set in a
context which also makes use of the same current key path. Technically, this means that /.style uses
\pgfkeysalso to set hoption listi, i.e. it does not use \pgfkeys as claimed above.
23
The difference is how they treat keys which are relative to some current key path, a concept which will
be explained in the next subsection.
Here is the difference between the macros: \pgfkeys resets the current key path to / before processing its
argument whereas \pgfkeysalso does not change the current key path. Consequently, \pgfkeysalso
is only useful inside of the body of some code–key (like /.style).
4 Special Tricks
4.1 Handling # in Arguments
More than once, I encountered the following difficulty: I wanted to collect an argument which contains the
hash sign, ‘#’. That’s not particularly difficult, but it can lead to a lot of strange error messages when the
resulting argument shall be processed! Consider
\def\collectargument#1{%
\def\collectedcontent{#1}%
\ifx\collectedcontent\empty
It is empty.
\else
It is not empty, executing it: #1.
\fi
}%
\collectargument{}% works
\collectargument{something}% works
The code in this example is relatively simple: the \collectargument macro expects one argument and
checks if it is empty (using \ifx, which is a common and reliable check for emptiness). It is is not empty, it
executes it. The \collectargument macro works in most circumstances. More precisely: it works as long
as there is no hash sign in its argument! In our example, the third call fails with “Illegal parameter number
in definition of \collectedcontent.” which occurs during the \def\collectedcontent{#1} line (and TEX
has reasons for this message due to the special meaning of the parameter expansion).
The cure: redefine the \collectargument macro using
\def\collectargument#1{%
\toks0={#1}%
\edef\collectedcontent{\the\toks0}%
\ifx\collectedcontent\empty
It is empty.
\else
It is not empty, executing it: #1.
\fi
}%
(you may want to allocate a temporary token register for this task). What is the difference? Well, the
\toks0={#1} assignment introduces no special meaning for the hash sign #, and \the\toks0 neither. Note,
however, that this requires \edef\collectedcontent instead of \def\collectedcontent since the \the
statement needs to be expanded. Everything works as expected.
24
Index
\advance, 4
\begingroup, 13
\bgroup, 13
.code handler, 23
\count, 2
\csname, 11
\def, 6, 11
\dimen, 3, 5
\divide, 5
\edef, 9, 11
\egroup, 13
\endgroup, 13
\expandafter, 7
\gdef, 11
\global, 13
\globaldefs, 14
.initial handler, 22
Key handlers
.code, 23
.initial, 22
.style, 23
\let, 11
\meaning, 6, 12
\message, 12
\multiply, 5
\newcount, 3
\newdimen, 3
\newif, 16
\newtoks, 3
\noexpand, 10
\pgfkeys, 22
\pgfkeysalso, 23
\pgfkeysdef, 21
\pgfkeysgetvalue, 20
\pgfkeysifassignable, 22
\pgfkeysifdefined, 22
\pgfkeyslet, 21
\pgfkeyssetvalue, 20
\pgfkeysvalueof, 21
\relax, 2
\string, 12
.style handler, 23
\toks, 3
\tracingcommands, 12
\tracingmacros, 12
\tracingrestores, 12
\xdef, 11
25
References
[1] C. Feuersänger. pgfplots manual, May 15, 2021.
[2] D. Knuth. Computers & Typesetting. Addison Wesley, 2000.
[3] N. Schwartz. Einführung in TEX (german!). Addison Wesley, 1991. Also available online at http:
//www.ruhr-uni-bochum.de/www-rz/schwanbs/TeX/ as .pdf.
[4] T. Tantau. Tik Z and pgf manual. http://sourceforge.net/projects/pgf. v. ≥ 2.00.
[5] J. Wright and C. Feuersänger. Implementing keyval input: an introduction. http://pgfplots.
sourceforge.net as .pdf, 2008.
26