Wiki: TeX

Your best resource for TeX is the TeXBook, particularly chapter 20 ("Definitions (also called Macros)") and Appendix D ("Dirty Tricks"). Appendix B provides a list of almost all macros defined at startup.

The reference for plain TeX at is also a good resource.


Differences from Plain TeX

The TeX language on the site is very similar to Knuth's plain TeX, with a few small modifications:

Looping over a string

The following code loops over all tokens until \relax, replacing all - tokens with (DASH)

    \ifx#1- (DASH)%
    \else #1%

\f 0-185186-70-\relax


Space characters can be annoying to manage. Important spacing rules:

Math operations

Math can be performed in counters. Define a counter with \newcount, and print the value with \the.

\x=15  % equivalent to the following line
\x15   % (the equals sign is optional)
\the\x % prints 15 as two tokens, "1" then "5"

Counters are 32-bit signed integers. Operations that overflow cause the program to instantly halt with no output.

\x\y            % x = y
\advance\x5     % x += 5
\advance\x by\y % equivalent to the following line
\advance\x\y    % x += y (the "by" text is optional)
\advance\x-\y   % x += -y
\multiply\x\y   % x *= y
\divide\x\y     % x /= y (floor divide)

There is no modulo operator. Modulo can be calculated (if you must) using the definition x % y = x - x / y * y, where x/y is floor division

% d = x - x % y, x = x % y (requires an extra counter)

Or modulo can be computed by repeated subtraction, assuming x and y are positive.

% x = x % y (slow)

ASCII conversions

Convert a char code to corresponding character with \char: \char97 gives the token a.

Convert a character to char code with a backtick: \number`a gives the two tokens 97, and \newcount\x\x=`a assigns 97 as the value of the counter \x.


One-byte macro ending

If a macro you define takes an argument longer than one token, you might be tempted to use curly braces. However, \def can allow for a single end character, saving one byte per usage.

\def\f#1{something #1 something}
% compare
\def\f#1;{something #1 something}


Commonly-used macros can be aliased with \let

% compare

At the top of a long solution, you might see a block of \lets like \let\N\newcount\let\I\ifnum\let\A\advance

Tilde is active

Tilde (~) is an active character (\catcode`\~=\active), so you can use it in lieu of one control sequences.

% compare

Form Feed

Form-feed (0x0c) is an active character (like tilde ~) defined as \par (\catcode`\^^L=\active \outer\def^^L{\par}). It is "outer," so you can only use it outside of constructs such as definitions and loops.

Form-feed can be typed in the editor by typing "0c" while holding down the Alt key.

% compare


% compare

Repeated macro vs loop

Repeating a macro several times is often shorter than a loop.

% compare (though kinda cheating because it uses ~)

Newline inside loop

% compare


The recursive approach saves 3 bytes.