LaTeX/Plain TeX

LaTeX

Getting Started

Common Elements

Mechanics

Technical Texts

Special Pages

Special Documents

Creating Graphics

Programming

1. Macros
2. Plain TeX
3. Creating Packages
4. Themes

Miscellaneous

Help and Recommendations

Appendices

edit this boxedit the TOC

While you play with LaTeX macros, you will notice that it is quite limited. You may wonder how all these packages you are using every day have been implemented with so little. In fact, LaTeX is a set of Plain TeX macros and most packages use Plain TeX code. Plain TeX is much more low-level, it has much more capabilities at the cost of a steep learning curve and complex programming.

Up to a few exceptions, you can use the full Plain TeX language within a valid LaTeX document whereas the opposite is false.

Vocabulary

To avoid confusion it seems necessary to explain some terms.

• A group is everything after an opening brace and before the matching closing brace.
• A token is a character, a control sequence, or a group.
• A control sequence is anything that begins with a \. It is not printed as is, it is expanded by the TeX engine according to its type.
• A command (or function or macro) is a control sequence that may expand to text, to (re)definition of control sequences, etc.
• A primitive is a command that is hard coded in the TeX engine, i.e. it is not written in Plain TeX.
• A register is the TeX way to handle variables. They are limited in numbers (256 for each type of register in classic TeX, 32767 in e-TeX).
• A length is a control sequence that contains a length (a number followed by a unit). See Lengths.
• A font is a control sequence that refers to a font file. See Fonts.
• A box is an object that is made for printing. Anything that ends on the paper is a box: letters, paragraphs, pages... See Boxes.
• A glue is a certain amount of space that is put between boxes when they are being concatenated.
• A counter is a register containing a number. See Counters.

There may be more terms, but we hope that it will do it for now.

Catcodes

In TeX some characters have a special meaning that is not to print the associated glyph. For example, \ is used to introduce a control sequence, and will not print a backslash by default.

To distinguish between different meanings of the characters, TeX split them into category codes, or catcodes for short. There are 16 category codes in TeX.

A powerful feature of TeX is its ability to redefine the language itself, since there is a \catcode function that will let you change the category code of any characters.

However, this is not recommended, as it can make code difficult to read. Should you redefine any catcode in a class or in a style file, make sure to revert it back at the end of your file.

If you redefine catcodes in your document, make sure to do it after the preamble to prevent clashes with package loading.

Code Description Default set
0 Escape character and control sequences \
1 Beginning of group {
2 End of group }

Font encoding map

We can use the above primitive to print the font encoding map.

 \count255 = 0 \loop   [\number\count255 =\char\number\count255] \ifnum\count255 < 127 \advance\count255 by 1 \repeat

Another version, with different fonts, one entry per line:

 \count255 = 0 \loop   [\number\count255 =     \char\number\count255 \     {\tt \char\number\count255}     {\it \char\number\count255}   ]   \hfil\break \ifnum\count255 < 127 \advance\count255 by 1 \repeat

Verbatim lines and spaces

It is rather confusing to discover (La)TeX treats all whitespace as the same type of spacing glue. Plain TeX provides some commands to preserve the spacing and newlines as you wrote it:

 \begingroup \obeylines \obeyspaces Relevant text here \endgroup

which means that you will probably need to combine your own verbatim environment, and your command:

 \newenvironment{myverbatim}{\begingroup \obeylines \obeyspaces}{\endgroup} \newcommand{\mycommand}[n]{do something with #1 .. #n}

and then in your tex file:

 \begin{myverbatim} \mycommand{ whichever text it is important you preserve the spacing and newslines for, like when you want to generate a verbatim block later on. } \end{myverbatim}

Macros defining macros

This is useful in some case, for example to define language commands as explained in Multilingual versions, where the end user can write

 \en{some english text} \de{some german text}

and make sure it switches to the appropriate Babel language.

Let's define a macros that will define language commands for instance. These commands are simple: if the argument is the value of the \locale variable, then the corresponding macro prints its content directly. Otherwise, it does nothing.

Basicly, what we want to do is extremely simple: define a bunch of macros like this:


In the previous snippet of code, only the \de command in going to output its content, \en and \fr will print nothing at all. That's what we want. The problem arises when you want to automate the task, or if you have a lot of languages, and you want to change the language selection. You just have to move the #1, but that's not convenient and it makes it impossible to choose the Babel language from command line. Think this out...

What we are going to do is to define the language commands dynamically following the value of the \locale variable (or any variable of your choice). Hence the use of the \equal command from the ifthen package.

Since it is hardly possible to write it in LaTeX, we will use some Plain TeX.

 \def\locale{de} \def\localedef#1{   \ifthenelse{ \equal{\locale}{#1} }{     %% Set the Babel language.     %% Define the command to print the content.   }{     %% Define the command to print nothing.   } }

Another problem arises: how to define a command whose name is a variable? In most programming languages that's not possible at all. What we could be tempted to write is

 \def\#1 #1{#1}

It will fail for two reasons.

1. The two last '#1' are supposed to refer to the arguments of the new macro, but they get expanded to the \localedef macro first argument because they are in the body of that macro.
2. \#1 gets expanded to two tokens: '#' and '1', and the \def command will fail as it requires a valid control sequence name.

The solution to problem 1 is simple: use '##1', which will expand to '#1' when the macro is executed.

For problem 2, it is a little bit tricky. It is possible to tell tex that a specific token is a control sequence. This is what the \csname...\endcsname is used for. However

 \def\csname#1\endcsname ##1{##1}

will fail because it will redefine \csname to '#1', which is not what we want, then tex will encounter \endcsname, which will result in an error.

We need to delay the expansion of \def, i.e. to tell tex to expand the \csname stuff first, then to apply \def on it. There is a command for that: \expandafter{token1}{token2}. It will expand {token2} before {token1}.

Finally if we want to set language from command line, we must be able to set the \locale variable so that the one in the source code is the default value that can be overridden by the one in the command line. This can be done with \provdecommand:

 \providecommand\locale{fr}

The final code is

 %% Required package. \usepackage{ifthen}   %% TeX function that generates the language commands. \def\localedef#1#2{   \ifthenelse{ \equal{\locale}{#1} }{     \selectlanguage{#2}     \expandafter\def\csname#1\endcsname ##1{##1}   }{     \expandafter\def\csname#1\endcsname ##1{}   }}   %% Selected language. Can be placed anywhere before the language commands. \providecommand\locale{fr}   %% Language commands. \localedef{de}{ngerman} \localedef{en}{english} \localedef{fr}{frenchb} %% ...

And you can compile with

latex '\providecommand\locale{en}\input{mydocument.tex}'


Notes and References

1. From tex.stackexchange.com: What is the difference between \let and \edef?