Usrguide3 PDF
Usrguide3 PDF
2022-02-19
Contents
1 Introduction 1
1 Introduction
LATEX 2ε was released in 1994 and added a number of then-new concepts to
LATEX. These are described in usrguide, which has largely remained unchanged.
Since then, the LATEX team have worked on a number of ideas, firstly a pro-
gramming language for LATEX (expl3) and then a range of tools for document
1
authors which build on that language. Here, we describe stable and widely-
usable concepts that have resulted from that work. These ‘new’ ideas have been
transferred from development packages into the LATEX 2ε kernel. As such, they
are now available to all LATEX users and have the same stability as any other
part of the kernel. The fact that ‘behind the scenes’ they are built on expl3 is
useful for the development team, but is not directly important to users.
2.1 Overview
2
r Given as r⟨token1 ⟩⟨token2 ⟩, this denotes a ‘required’ delimited argument,
where the delimiters are ⟨token1 ⟩ and ⟨token2 ⟩. If the opening delimiter
⟨token1 ⟩ is missing, the default marker -NoValue- will be inserted after
a suitable error.
R Given as R⟨token1 ⟩⟨token2 ⟩{⟨default⟩}, this is a ‘required’ delimited ar-
gument as for r, but it has a user-definable recovery ⟨default⟩ instead of
-NoValue-.
3
2.3 Modifying argument descriptions
In addition to the argument types discussed above, the argument description
also gives special meaning to three other characters.
First, + is used to make an argument long (to accept paragraph tokens). In
contrast to \newcommand, this applies on an argument-by-argument basis. So
modifying the example to ‘s o o +m O{default}’ means that the mandatory
argument is now \long, whereas the optional arguments are not.
Secondly, ! is used to control whether spaces are allowed before optional argu-
ments. There are some subtleties to this, as TEX itself has some restrictions on
where spaces can be ‘detected’: more detail is given in Section 2.6.
Finally, the character > is used to declare so-called ‘argument processors’, which
can be used to modify the contents of an argument before it is passed to the
macro definition. The use of argument processors is a somewhat advanced topic,
(or at least a less commonly used feature) and is covered in Section 2.9.
This family of commands are used to create a ⟨cmd ⟩. The argument specification
for the function is given by ⟨arg spec⟩, and the command uses the ⟨code⟩ with
#1, #2, etc. replaced by the arguments found by the parser.
An example:
\NewDocumentCommand\chapter{s o m}
{%
\IfBooleanTF{#1}%
{\typesetstarchapter{#3}}%
{\typesetnormalchapter{#2}{#3}}%
}
4
• \ProvideDocumentCommand creates a new definition for ⟨function⟩ only if
one has not already been given.
• \DeclareDocumentCommand will always create the new definition, irrespec-
tive of any existing ⟨cmd ⟩ with the same name. This should be used
sparingly.
If the ⟨cmd ⟩ can’t be provided as a single token but needs “constructing”, you
can use \ExpandArgs as explained in Section 4 which also gives an example in
which this is needed.
These commands work in the same way as \NewDocumentCommand, etc., but cre-
ate environments (\begin{⟨env ⟩} . . . \end{⟨env ⟩}). Both the ⟨beg-code⟩ and
⟨end-code⟩ may access the arguments as defined by ⟨arg spec⟩. The arguments
will be given following \begin{⟨env ⟩}.
\foo[\baz[stuff]]{more stuff}
will print
\NewDocumentCommand\foo{O{#2} m}
would use the mandatory argument as the default for the leading optional one.
5
2.6 Spacing and optional arguments
TEX will find the first argument after a function name irrespective of any in-
tervening spaces. This is true for both mandatory and optional arguments. So
\foo[arg] and \foo␣[arg] are equivalent. Spaces are also ignored when col-
lecting arguments up to the last mandatory argument to be collected (as it must
exist). So after
2.7 ‘Embellishments’
The E-type argument allows one default value per test token. This is achieved
by giving a list of defaults for each entry in the list, for example:
E{^_}{{UP}{DOWN}}
If the list of default values is shorter than the list of test tokens, the special
-NoValue- marker will be returned (as for the e-type argument). Thus for
example
E{^_}{{UP}}
has default UP for the ^ test character, but will return the -NoValue- marker as
a default for _. This allows mixing of explicit defaults with testing for missing
values.
6
2.8 Testing special values
The \IfNoValue(TF) tests are used to check if ⟨argument⟩ (#1, #2, etc.) is the
special -NoValue- marker. For example
\NewDocumentCommand\foo{o m}
{%
\IfNoValueTF {#1}%
{\DoSomethingJustWithMandatoryArgument{#2}}%
{\DoSomethingWithBothArguments{#1}{#2}}%
}
will use a different internal function if the optional argument is given than if it
is not present.
Note that three tests are available, depending on which outcome branches are
required: \IfNoValueTF, \IfNoValueT and \IfNoValueF.
As the \IfNoValue(TF) tests are expandable, it is possible to test these values
later, for example at the point of typesetting or in an expansion context.
It is important to note that -NoValue- is constructed such that it will not match
the simple text input -NoValue-, i.e. that
\IfNoValueTF{-NoValue-}
will be logically false. When two optional arguments follow each other (a
syntax we typically discourage), it can make sense to allow users of the command
to specify only the second argument by providing an empty first argument.
Rather than testing separately for emptiness and for -NoValue- it is then best New
to use the argument type O with an empty default value, and then test for description
emptiness using the conditional \IfBlankTF (described below) instead. 2022/06/01
The reverse form of the \IfNoValue(TF) tests are also available as \IfValue(TF).
The context will determine which logical form makes the most sense for a given
code scenario.
7
The \IfNoValueTF command chooses the ⟨true code⟩ if the optional argument
has not been used at all (and it returns the special -NoValue- marker), but not
if it has been given an empty value. In contrast \IfBlankTF returns true if its
argument is either truly empty or only contains one or more normal blanks. For
example
\NewDocumentCommand\foo{m!o}{\par #1:
\IfNoValueTF{#2}{No optional}%
{\IfBlankTF{#2}{Blanks in or empty}%
{Real content in}}%
\space argument!}
\foo{1}[bar] \foo{2}[ ] \foo{3}[] \foo{4}[\space] \foo{5} [x]
\BooleanFalse
\BooleanTrue
The true and false flags set when searching for an optional character (using
s or t⟨char ⟩) have names which are accessible outside of code blocks.
\NewDocumentCommand\foo{sm}
{%
\IfBooleanTF {#1}%
{\DoSomethingWithStar{#2}}%
{\DoSomethingWithoutStar{#2}}%
}
checks for a star as the first argument, then chooses the action to take based on
this information.
8
2.9 Argument processors
>{\ProcessorB} >{\ProcessorA} m
This processor splits the argument given at each occurrence of the ⟨tokens⟩ up
to a maximum of ⟨number ⟩ tokens (thus dividing the input into ⟨number ⟩ + 1
parts). An error is given if too many ⟨tokens⟩ are present in the input. The
processed input is placed inside ⟨number ⟩ + 1 sets of braces for further use. If
there are fewer than {⟨number ⟩} of {⟨tokens⟩} in the argument then -NoValue-
markers are added at the end of the processed argument.
If only a single character ⟨token⟩ is used for the split, any category code 13
(active) character matching the ⟨token⟩ will be replaced before the split takes
place. Spaces are trimmed at each end of each item parsed.
The E argument type is somewhat special, because with a single E in the com-
mand declaration you may end up with several arguments in a command (one
formal argument per embellishment token). Therefore, when an argument pro-
cessor is applied to an E-type argument, all the arguments pass through that
processor before being fed to the ⟨code⟩. For example, this command
\SplitList {⟨token(s)⟩}
This processor splits the argument given at each occurrence of the ⟨token(s)⟩
where the number of items is not fixed. Each item is then wrapped in braces
within #1. The result is that the processed argument can be further processed
using a mapping function (see below).
9
\NewDocumentCommand \foo {>{\SplitList{;}} m}
{\MappingFunction#1}
If only a single character ⟨token⟩ is used for the split, any category code 13
(active) character matching the ⟨token⟩ will be replaced before the split takes
place. Spaces are trimmed at each end of each item parsed.
\ReverseBoolean
\NewDocumentCommand\foo{>{\ReverseBoolean} s m}
{%
\IfBooleanTF#1%
{\DoSomethingWithoutStar{#2}}%
{\DoSomethingWithStar{#2}}%
}
\TrimSpaces
Removes any leading and trailing spaces (tokens with character code 32 and
category code 10) for the ends of the argument. Thus for example declaring a
function
\NewDocumentCommand\foo {>{\TrimSpaces} m}
{\showtokens{#1}}
\foo{␣hello␣world␣}
will show ‘hello␣world’ at the terminal, with the space at each end removed.
\TrimSpaces will remove multiple spaces from the ends of the input in cases
where these have been included such that the standard TEX conversion of mul-
tiple spaces to a single space does not apply.
10
2.10 Body of an environment
\begin{twice}[\itshape]
Hello world!
\end{twice}
11
In general, ⟨code⟩ will also be fully expandable, although it is possible that this
will not be the case (for example, a function for use in a table might expand so
that \omit is the first non-expandable non-space token).
Parsing arguments by pure expansion imposes a number of restrictions on both
the type of arguments that can be read and the error checking available:
• The last argument (if any are present) must be one of the mandatory types
m, r or R.
In normal (non-expandable) commands, the delimited types look for the initial
delimiter by peeking ahead (using expl3’s \peek_... functions) looking for the
delimiter token. The token has to have the same meaning and ‘shape’ of the to-
ken defined as delimiter. There are three possible cases of delimiters: character
tokens, control sequence tokens, and active character tokens. For all practi-
cal purposes of this description, active character tokens will behave exactly as
control sequence tokens.
A character token is characterized by its character code, and its meaning is the
category code (\catcode). When a command is defined, the meaning of the
character token is fixed into the definition of the command and cannot change.
A command will correctly see an argument delimiter if the open delimiter has
the same character and category codes as at the time of the definition. For
example in:
(hello)
(default)<hello>
12
as the open-delimiter < changed in meaning between the two calls to \foobar,
so the second one doesn’t see the < as a valid delimiter. Commands assume
that if a valid open-delimiter was found, a matching close-delimiter will also
be there. If it is not (either by being omitted or by changing in meaning), a
low-level TEX error is raised and the command call is aborted.
\cs_set:Npn \x { abc }
\NewDocumentCommand { \foobar } { D\x\y{default} } {(#1)}
\foobar \x hello\y \par
\cs_set:Npn \x { def }
\foobar \x hello\y
(hello)
(hello)
\ProcessedArgument
\ExplSyntaxOn
\cs_new_protected:Npn \ReverseBoolean #1
{
\bool_if:NTF #1
{ \tl_set:Nn \ProcessedArgument { \c_false_bool } }
{ \tl_set:Nn \ProcessedArgument { \c_true_bool } }
}
\ExplSyntaxOff
[As an aside: the code is written in expl3, so we don’t have to worry about
spaces creeping into the definition.]
13
2.14 Access to the argument specification
The argument specifications for document commands and environments are
available for examination and use.
\GetDocumentCommandArgSpec {⟨function⟩}
\GetDocumentEnvironmentArgSpec {⟨environment⟩}
These functions transfer the current argument specification for the requested
⟨function⟩ or ⟨environment⟩ into the token list variable \ArgumentSpecification.
If the ⟨function⟩ or ⟨environment⟩ has no known argument specification then
an error is issued. The assignment to \ArgumentSpecification is local to the
current TEX group.
\ShowDocumentCommandArgSpec {⟨function⟩}
\ShowDocumentEnvironmentArgSpec {⟨environment⟩}
These functions show the current argument specification for the requested
⟨function⟩ or ⟨environment⟩ at the terminal. If the ⟨function⟩ or ⟨environment⟩
has no known argument specification then an error is issued.
\NewCommandCopy\LaTeXorig\LaTeX
\RenewDocumentCommand\LaTeX{}{\textcolor{blue}{\LaTeXorig}}
all LATEX logos generated with \LaTeX will come out in blue (assuming you have
a color package loaded).
14
The differences between \New... and \Renew... are as elsewhere: i.e., you
get an error depending on whether or not ⟨cmd ⟩ already exists, or in case of
\Declare... it is copied regardless. Note that there is no \Provide... decla-
ration, because that would be of limited value.
If the ⟨cmd ⟩ or ⟨existing-cmd ⟩ can’t be provided as a single token but need
“constructing”, you can use \ExpandArgs as explained in Section 4.
\ShowCommand {⟨cmd ⟩}
This displays the meaning of the ⟨cmd ⟩ on the terminal and then stops (just
like the primitive \show). The difference is that it correctly shows the meaning
of more complex commands, e.g., in case of robust commands it displays not
only the top-level definition but also the actual payload code and in case of
commands declared with \NewDocumentCommand, etc. it also gives you detailed
information about the argument signature.
\UseName {⟨string⟩}
\ExpandArgs {⟨spec⟩} {⟨cmd ⟩} {⟨arg1 ⟩} . . .
\UseName turns the ⟨string⟩ directly into a csname and then executes it: this
is equivalent to the long-standing LATEX 2ε internal command \@nameuse, or
the L3 programming equivalent \use:c. \ExpandArgs takes a ⟨spec⟩ which
describes how to expand the ⟨arguments⟩, carries out these operations then exe-
cutes the ⟨cmd ⟩. The ⟨spec⟩ uses the descriptions offered by the L3 programming
layer, and the relevant \exp_args:N... function must exist. Common cases
will have a ⟨spec⟩ of c, cc or Nc: see below.
As an example, the following declaration provides a method to generate copyedit
commands:
\NewDocumentCommand\newcopyedit{mO{red}}
{\newcounter{todo#1}%
\ExpandArgs{c}\NewDocumentCommand{#1}{s m}%
{\stepcounter{todo#1}%
\IfBooleanTF {##1}%
{\todo[color=#2!10]{\UseName{thetodo#1}: ##2}}%
{\todo[inline,color=#2!10]{\UseName{thetodo#1}: ##2}}%
}%
}
15
Given that declaration you can then write \newcopyedit{note}[blue] which
defines the command \note and the corresponding counter for you.
A second example is to copy a command by string name using \NewCommandCopy:
here we might need to construct both command names.
\NewDocumentCommand\savebyname{m}
{\ExpandArgs{cc}\NewCommandCopy{saved#1}{#1}}
In the ⟨spec⟩ each c stands for one argument that is turned into a ‘c’ommand.
An n represents a ‘n’ormal argument that is not altered and N stands for a
‘N’ormal argument which is also left unchanged, but one consisting only of a
single token (and usually unbraced). Thus, to construct a command from a
string only for the second argument of \NewCommandCopy you would write
\ExpandArgs{Nc}\NewCommandCopy\mysectionctr{c@section}
There are several other single letters supported in the L3 programming layer
that could be used in the ⟨spec⟩ to manipulate arguments in other ways. If
you are interested, take a look at the “Argument expansion” section in the L3
programming layer documentation in interface3.pdf.
The expandable command \fpeval takes as its argument a floating point ex-
pression and produces a result using the normal rules of mathematics. As this
command is expandable it can be used where TEX requires a number and for
example within a low-level \edef operation to give a purely numerical result.
Briefly, the floating point expressions may comprise:
16
• Integer factorial: fact x.
• Trigonometry: sin x, cos x, tan x, cot x, sec x, csc x expecting their
arguments in radians, and sind x, cosd x, tand x, cotd x, secd x, cscd x
expecting their arguments in degrees.
• Inverse trigonometric functions: asin x, acos x, atan x, acot x, asec x,
acsc x giving a result in radians, and asind x, acosd x, atand x, acotd x,
asecd x, acscd x giving a result in degrees.
• Extrema: max(x1 , x2 , . . .), min(x1 , x2 , . . .), abs(x).
• Rounding functions, controlled by two optional values, n (number of
places, 0 by default) and t (behavior on a tie, NaN by default):
– trunc(x, n) rounds towards zero,
– floor(x, n) rounds towards −∞,
– ceil(x, n) rounds towards +∞,
– round(x, n, t) rounds to the closest value, with ties rounded to an
even value by default, towards zero if t = 0, towards +∞ if t > 0 and
towards −∞ if t < 0.
• Random numbers: rand(), randint(m, n).
• Constants: pi, deg (one degree in radians).
• Dimensions, automatically expressed in points, e.g., pc is 12.
• Automatic conversion (no need for \number) of integer, dimension, and
skip variables to floating points numbers, expressing dimensions in points
and ignoring the stretch and shrink components of skips.
• Tuples: (x1 , . . . , xn ) that can be added together, multiplied or divided by
a floating point number, and nested.
17
• / denotes division rounded to the closest integer with ties rounded away
from zero;
• there is an error and the overall expression evaluates to zero whenever the
absolute value of any intermediate result exceeds 231 − 1, except in the
case of scaling operations a*b/c, for which a*b may be arbitrarily large;
• parentheses may not appear after unary + or -, namely placing +( or -(
at the start of an expression or after +, -, *, / or ( leads to an error.
\LaTeX{} can now compute: The sum of the numbers is $\inteval{1 + 2 + 3}$.
which results in “LATEX can now compute: The sum of the numbers is 6.”
\newcommand\calulateheight[1]{%
\setlength\textheight{\dimeval{\topskip+\baselineskip*\inteval{#1-1}}}}
sets the \textheight to the appropriate value if a page should hold a specific
number of text lines. Thus after \calulateheight{40} it is set to 478.0pt,
given the values \topskip (10.0pt) and \baselineskip (12.0pt) in the current
document.
18