General documentation / cheat sheets for various languages and services

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}: * \arabic: 1, 2, 3… * \alph: a, b, c… * \Alph: A, B, C… * \roman: i, ii, iii… * \Roman: I, II, III… * \fnsymbol: *, †, ‡, §, ¶, ||, **, ††, …

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}: * \the\c@somecount * \thesomecount

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

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

\vspace*{\stretch{3}} Footer Fragment Here \vspace*{\stretch{1}}

Rules

Spacing

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.