General documentation / cheat sheets for various languages and services

Foundational syntax

Character categories

There are 16 character categories:ref

catcode Description Literal in plain TeX
0 Escape character \
1 Begin grouping {
2 End grouping }
3 Math shift $
4 Alignment tab &
5 End of line 〈return〉
6 Parameter #
7 Superscript ^
8 Subscript _
9 Ignored character 〈null〉
10 Space 〈space〉 and 〈tab〉
11 Letter only contains the letters a, …, z and A, …, Z. These characters can be used in command names
12 Other everything else not listed in the other categories
13 Active character ~
14 Comment character %
15 Invalid character 〈delete〉

Characters can be assigned a different catcode (which is local to the current group) with the \catcode command. For example, \makeatletter is a commonly used catcode manipulation helper in LaTeX. Start latex and run:

\*\*\show\makeatletter > \makeatletter=macro: ->\catcode \`\@11\relax .

I.e., makeatletter is a macro that uses \catcode to put the @ character into the “Letter” category so that it can participate in command names. We can look at its default setting:

\showthe\catcode\`\@ > 12. Which produces 12., meaning @, by default, is in the “Other” category.

TeX has ten special characters (TeXBook, ch. 7, pg. 38): \ { } $ & # ^ _ % ~

Note that @ is not one of those characters.

Control sequences

There are two types of control sequences:

  1. Control words
  2. Control symbols

A control sequence can be composed from raw tokens with the \csname and \endcsname commands. E.g., \csname TeX\endcsname is like saying \TeX.

Literal characters can be specified by ASCII code with the \char command; for example:

\char98 = \char'142 = \char"62 = b

Since decimal 98 = octal 142 = hexadecimal 62.

There is also a \symbol command, which simply wraps \char and adds a \relax after the given character code. This can be handy if you’re not sure what will follow your \char macro. The following are all equivalent and produce the Unicode character, “U+221E (INFINITY)”:

\symbol{8734} = \symbol{'21036} = \symbol{"221E} =

ASCII’s control characters can be represented by a special prefix, ^^. E.g.: ^^@ = “U+0000 NUL” (Null), ^^A = “U+0001 SOH” (Start of Heading), etc. up to ^^_ = “U+001F US” (Unit Separator)

Debugging

Calling tex and latex without arguments will bring up interactive prompts for the respective compilers. It’s probably handy to run them under rlwrap, e.g., rlwrap tex. By default, these will write a texput.dvi file when done, which can be converted to texput.pdf with dvipdf texput.dvi.

Dedicated packages

Macros

LaTeXref

Plain TeXref

Some packages or document classes facilitate configuring internal variables via proxy commands, e.g., a 3rd-party library might have this snippet:

\newcommand{\submissiondate}[1]{\gdef\@submissiondate{#1}}
% Later on, perhaps in \maketitle, call \@submissiondate{}

Which you would call in your consuming document, probably in the preamble:

\submissiondate{2014-11-20}

In such a case, not calling \submissiondate{...} in the consuming document will induce an error in the 3rd-party library, since \@submissiondate was never defined. There are two ways to handle this error.

  1. Define a default: somewhere in the 3rd-party library, call \def\@submissiondate{A long time ago}
  2. Condition on @submissiondate having been defined, using the LaTeX standard macro \@ifundefined:

     \@ifundefined{@submissiondate}{Submission date not recorded}%
                                   {Submitted on \@submissiondate}
    

N.b. We use @submissiondate, without the \. If we had used \@submissiondate, it would try to evaluate that before passing control to \@ifundefined.

Expansion

These are all primitives (i.e., they are built into the TeX compiler, not defined in latex.ltx or article.cls):

These are helpers that are defined in latex.ltx:

Constants

References: What does \z@ do?

Macro Value Source / explanation / description
\z@ 0pt \newdimen\z@ \z@=0pt; i.e., zero (equivalently, 0pt) - useful because it avoids the need to say 0\relax in some cases.
\@empty {} \def\@empty{}; i.e., nothing.

Character definitions (defined with \chardef in latex.ltx):

Macro Value
\@ne 1
\tw@ 2
\thr@@ 3
\sixt@@n 16
\@cclv 255

Counters and Variables

References: What are the differences between TeX counts and LaTeX counters?, LaTeX/Counters

Plain TeX:

\newcount <register> defines a number variable, a.k.a., a “count”. E.g., \newcount\mycounter.

<register> <number> sets the value of a register. You’ll often also see <register>=<number>, which does the same thing.

\advance <register> <number> increases the measure/width indicated by <register> by the amount indicated. Most often you’ll see something like \advance\mycounter 1\relax, which increases mycounter by 1. The \relax apparently tells LaTeX that’s the end of the number.

\the <register> prints out (i.e., into the document) the current value of the register, e.g., \the\mycounter or \the\leftmargin. \number can be used instead of \the in this case.

LaTeX:

\newcounter{<register>} defines a new counter, and there are several functions for manipulating it.

Creating a LaTeX counter also creates a function to retrieve its value, with the name \the<register>, so that \themycounter will return the value of the “mycounter” counter, same as \value{mycounter}.

There are several helpers for formatting the current values of counters, e.g., \Roman{mycounter}:

All of these, except \arabic, will output blanks for non-positive numbers.

LaTeX counters are just wrappers around TeX counters, and can be accessed prefixed under the prefix “c@”, so the following are equivalent after calling \newcounter{somecount}:

Due to the prefix assumption, you can’t access TeX counters with LaTeX counter mechanisms unless the counter was named using the “c@” prefix.

Arithmetic

There are four primitives for doing arithmetic that operate on and return specific types.ref

Expression Result type
\numexpr integer
\dimexpr dimension
\glueexpr glue expression
\muexpr muskip value

The different expressions determine what values each will consume.

The expression is greedily captured, so expressions are often terminated with a \relax, just to be safe/explicit.

Control flow

\ifnum <left><R><right> <iftrue> /fi runs the code in the “iftrue” block if “left R right”, where R can be <, >, or =. E.g., \ifnum \the\mycounter =0 \textbf{First} \fi would insert the bold “First” if “mycounter” is 0, but do nothing for other values of “mycounter”. You can optionally specify a \else <iffalse> branch, e.g., \ifnum \the\mycounter =0 \textbf{First} \else Not first \fi

Boolean variables can be emulated with \newif:ref

\newif\ifdraft

That declares two macros for setting the “draft” variable: \drafttrue and \draftfalse.

\drafttrue
% \ifdraft now expands to \iftrue

\draftfalse
% \ifdraft now expands to \iffalse

This makes conditionals easy to read. Supposing we want to turn on double-spacing when we’re in “draft mode”, and ensure we’re using single-spacing otherwise:

\ifdraft
  \doublespacing
\else
  \singlespacing
\end

Looping over array / stack / list data structure

Use \@for:ref, better

For example, in a class or package:

\def\@coauthors{}
\newcommand{\coauthors}[1]{\gdef\@coauthors{#1}}

\newcommand{\signatureline}[1]{\parskip=12pt\par\hrulefill\parskip=-8pt\par#1}

\newcommand{\makesigntureform}{
  \signature{\@author, Corresponding Author}
  \@for\@i:=\@coauthors\do{\signatureline{\@i}}
}

Then, in your consuming document:

\author{Me}
\coauthors{Alice, Bob, Chandralan}
...
\makesignatureform
\maketitle

@

Reference: What do \makeatletter and \makeatother do?

Many packages use @ in their counter / macro names, which signals a sort of ‘private’ status.

In a class (.cls) or package (.sty) declaration, you can use @ in variable names without doing anything extra.

To use or modify these in normal LaTeX, you need to tell the compiler to treat the @ character as a normal word character:

\makeatletter

Then you can, for example, override the built-in list label command: \def\@mklab#1{#1\hfil} (which makes it align left instead of right). To set things back to normal:

\makeatother

Packages and classes

ref.

Lists

Alignment

Rules

Spacing

Predefined / globally used lengths:ref

Pagination

Fonts

Labels and references

As noted on the Wikibooks page, there are some conventional prefixes for label names in specific environments:

Prefix Environment
ch: chapter
sec: section
subsec: subsection
fig: figure
tab: table
eq: equation
lst: code listing
itm: enumerated list item
alg: algorithm
app: appendix subsection

cleveref and others are clever enough to look up the type of labeled entity when referencing it, but this makes it easier to check your label name quickly.