| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
%% File: xparse.dtx |
| 4 |
% |
| 5 |
% Copyright (C) 1999 Frank Mittelbach, Chris Rowley, David Carlisle |
| 6 |
% (C) 2004-2008 Frank Mittelbach, The LaTeX Project |
| 7 |
% (C) 2009-2025 The LaTeX Project |
| 8 |
% |
| 9 |
% It may be distributed and/or modified under the conditions of the |
| 10 |
% LaTeX Project Public License (LPPL), either version 1.3c of this |
| 11 |
% license or (at your option) any later version. The latest version |
| 12 |
% of this license is in the file |
| 13 |
% |
| 14 |
% https://www.latex-project.org/lppl.txt |
| 15 |
% |
| 16 |
% This file is part of the "l3packages bundle" (The Work in LPPL) |
| 17 |
% and all files in that bundle must be distributed together. |
| 18 |
% |
| 19 |
% ----------------------------------------------------------------------- |
| 20 |
% |
| 21 |
% The development version of the bundle can be found at |
| 22 |
% |
| 23 |
% https://github.com/latex3/latex3 |
| 24 |
% |
| 25 |
% for those people who are interested. |
| 26 |
% |
| 27 |
%<*driver|package> |
| 28 |
% The version of expl3 required is tested as early as possible, as |
| 29 |
% some really old versions do not define \ProvidesExplPackage. |
| 30 |
\RequirePackage{expl3}[2018-04-12] |
| 31 |
%<package>\@ifpackagelater{expl3}{2018-04-12} |
| 32 |
%<package> {} |
| 33 |
%<package> {% |
| 34 |
%<package> \PackageError{xparse}{Support package l3kernel too old} |
| 35 |
%<package> {% |
| 36 |
%<package> Please install an up to date version of l3kernel\MessageBreak |
| 37 |
%<package> using your TeX package manager or from CTAN.\MessageBreak |
| 38 |
%<package> \MessageBreak |
| 39 |
%<package> Loading xparse will abort!% |
| 40 |
%<package> }% |
| 41 |
%<package> \endinput |
| 42 |
%<package> } |
| 43 |
%</driver|package> |
| 44 |
%<*driver> |
| 45 |
\documentclass[full]{l3doc} |
| 46 |
\usepackage{amstext} |
| 47 |
\begin{document} |
| 48 |
\DocInput{\jobname.dtx} |
| 49 |
\end{document} |
| 50 |
%</driver> |
| 51 |
% \fi |
| 52 |
% |
| 53 |
% \providecommand\acro[1]{\textsc{\MakeLowercase{#1}}} |
| 54 |
% \newenvironment{arg-description}{% |
| 55 |
% \begin{itemize}\def\makelabel##1{\hss\llap{\bfseries##1}}}{\end{itemize}} |
| 56 |
% |
| 57 |
% \title{^^A |
| 58 |
% The \pkg{xparse} package\\ Document command parser^^A |
| 59 |
% } |
| 60 |
% |
| 61 |
% \author{^^A |
| 62 |
% The \LaTeX{} Project\thanks |
| 63 |
% {^^A |
| 64 |
% E-mail: |
| 65 |
% \href{mailto:[email protected]} |
| 66 |
% {[email protected]}^^A |
| 67 |
% }^^A |
| 68 |
% } |
| 69 |
% |
| 70 |
% \date{Released 2025-10-09} |
| 71 |
% |
| 72 |
% \maketitle |
| 73 |
% |
| 74 |
% \begin{documentation} |
| 75 |
% |
| 76 |
% \section{Introduction} |
| 77 |
% |
| 78 |
% \begin{center} |
| 79 |
% \bfseries This package is obsolete with the October 2020 \LaTeX{} release. |
| 80 |
% \end{center} |
| 81 |
% |
| 82 |
% With new formats, \cs{NewDocumentCommand}, etc., are available in |
| 83 |
% the format. Other than for a small number of now-deprecated |
| 84 |
% argument types and support functions, package authors should transition |
| 85 |
% their code to avoid loading \pkg{xparse}. An updated version of the |
| 86 |
% documentation covering the full set of functionality available in |
| 87 |
% the kernel is available in \texttt{usrguide}. |
| 88 |
% |
| 89 |
% \section{Old introduction} |
| 90 |
% |
| 91 |
% The \pkg{xparse} package provides a high-level interface for |
| 92 |
% producing document-level commands. In that way, it is intended as |
| 93 |
% a replacement for the \LaTeXe{} \cs{newcommand} macro. However, |
| 94 |
% \pkg{xparse} works so that the interface to a function (optional |
| 95 |
% arguments, stars and mandatory arguments, for example) is separate |
| 96 |
% from the internal implementation. \pkg{xparse} provides a normalized |
| 97 |
% input for the internal form of a function, independent of the |
| 98 |
% document-level argument arrangement. |
| 99 |
% |
| 100 |
% At present, the functions in \pkg{xparse} which are regarded as |
| 101 |
% \enquote{stable} are: |
| 102 |
% \begin{itemize} |
| 103 |
% \item \cs{NewDocumentCommand}\\ |
| 104 |
% \cs{RenewDocumentCommand}\\ |
| 105 |
% \cs{ProvideDocumentCommand}\\ |
| 106 |
% \cs{DeclareDocumentCommand} |
| 107 |
% \item \cs{NewDocumentEnvironment}\\ |
| 108 |
% \cs{RenewDocumentEnvironment}\\ |
| 109 |
% \cs{ProvideDocumentEnvironment}\\ |
| 110 |
% \cs{DeclareDocumentEnvironment} |
| 111 |
% \item \cs{NewExpandableDocumentCommand}\\ |
| 112 |
% \cs{RenewExpandableDocumentCommand}\\ |
| 113 |
% \cs{ProvideExpandableDocumentCommand}\\ |
| 114 |
% \cs{DeclareExpandableDocumentCommand} |
| 115 |
% \item \cs{IfNoValue(TF)} |
| 116 |
% \item \cs{IfValue(TF)} |
| 117 |
% \item \cs{IfBoolean(TF)} |
| 118 |
% \end{itemize} |
| 119 |
% with the other functions currently regarded as \enquote{experimental}. Please |
| 120 |
% try all of the commands provided here, but be aware that the |
| 121 |
% experimental ones may change or disappear. |
| 122 |
% |
| 123 |
% \section{Specifying arguments} |
| 124 |
% |
| 125 |
% Before introducing the functions used to create document commands, |
| 126 |
% the method for specifying arguments with \pkg{xparse} will be |
| 127 |
% illustrated. In order to allow each argument to be defined |
| 128 |
% independently, \pkg{xparse} does not simply need to know the |
| 129 |
% number of arguments for a function, but also the nature of each |
| 130 |
% one. This is done by constructing an \emph{argument specification}, |
| 131 |
% which defines the number of arguments, the type of each argument |
| 132 |
% and any additional information needed for \pkg{xparse} to read the |
| 133 |
% user input and properly pass it through to internal functions. |
| 134 |
% |
| 135 |
% The basic form of the argument specifier is a list of letters, where |
| 136 |
% each letter defines a type of argument. As will be described below, |
| 137 |
% some of the types need additional information, such as default values. |
| 138 |
% The argument types can be divided into two, those which define |
| 139 |
% arguments that are mandatory (potentially raising an error if not |
| 140 |
% found) and those which define optional arguments. The mandatory types |
| 141 |
% are: |
| 142 |
% \begin{itemize}[font=\ttfamily] |
| 143 |
% \item[m] A standard mandatory argument, which can either be a single |
| 144 |
% token alone or multiple tokens surrounded by curly braces |{}|. |
| 145 |
% Regardless of the input, the argument will be passed to the |
| 146 |
% internal code without the outer braces. This is the \pkg{xparse} |
| 147 |
% type specifier for a normal \TeX{} argument. |
| 148 |
% \item[r] Given as \texttt{r}\meta{token1}\meta{token2}, this denotes a |
| 149 |
% \enquote{required} delimited argument, where the delimiters are |
| 150 |
% \meta{token1} and \meta{token2}. If the opening delimiter |
| 151 |
% \meta{token1} is missing, the default marker |-NoValue-| will be |
| 152 |
% inserted after a suitable error. |
| 153 |
% \item[R] Given as \texttt{R}\meta{token1}\meta{token2}\marg{default}, |
| 154 |
% this is a \enquote{required} delimited argument as for~\texttt{r}, |
| 155 |
% but it has a user-definable recovery \meta{default} instead of |
| 156 |
% |-NoValue-|. |
| 157 |
% \item[v] Reads an argument \enquote{verbatim}, between the following |
| 158 |
% character and its next occurrence, in a way similar to the argument |
| 159 |
% of the \LaTeXe{} command \cs{verb}. Thus a \texttt{v}-type argument |
| 160 |
% is read between two identical characters, which cannot be any of |%|, |\|, |
| 161 |
% |#|, |{|, |}| or \verb*| |. |
| 162 |
% The verbatim argument can also be enclosed between braces, |{| and |}|. |
| 163 |
% A command with a verbatim |
| 164 |
% argument will produce an error when it appears within an argument of |
| 165 |
% another function. |
| 166 |
% \item[b] Only suitable in the argument specification of an |
| 167 |
% environment, it denotes the body of the environment, between |
| 168 |
% |\begin|\marg{environment} and |\end|\marg{environment}. See |
| 169 |
% Section~\ref{sec:body} for details. |
| 170 |
% \end{itemize} |
| 171 |
% The types which define optional arguments are: |
| 172 |
% \begin{itemize}[font=\ttfamily] |
| 173 |
% \item[o] A standard \LaTeX{} optional argument, surrounded with square |
| 174 |
% brackets, which will supply |
| 175 |
% the special |-NoValue-| marker if not given (as described later). |
| 176 |
% \item[d] Given as \texttt{d}\meta{token1}\meta{token2}, an optional |
| 177 |
% argument which is delimited by \meta{token1} and \meta{token2}. |
| 178 |
% As with \texttt{o}, if no |
| 179 |
% value is given the special marker |-NoValue-| is returned. |
| 180 |
% \item[O] Given as \texttt{O}\marg{default}, is like \texttt{o}, but |
| 181 |
% returns \meta{default} if no value is given. |
| 182 |
% \item[D] Given as \texttt{D}\meta{token1}\meta{token2}\marg{default}, |
| 183 |
% it is as for \texttt{d}, but returns \meta{default} if no value is given. |
| 184 |
% Internally, the \texttt{o}, \texttt{d} and \texttt{O} types are |
| 185 |
% short-cuts to an appropriated-constructed \texttt{D} type argument. |
| 186 |
% \item[s] An optional star, which will result in a value |
| 187 |
% \cs{BooleanTrue} if a star is present and \cs{BooleanFalse} |
| 188 |
% otherwise (as described later). |
| 189 |
% \item[t] An optional \meta{token}, which will result in a value |
| 190 |
% \cs{BooleanTrue} if \meta{token} is present and \cs{BooleanFalse} |
| 191 |
% otherwise. Given as \texttt{t}\meta{token}. |
| 192 |
% \item[e] Given as \texttt{e}\marg{tokens}, a set of optional |
| 193 |
% \emph{embellishments}, each of which requires a \emph{value}. |
| 194 |
% If an embellishment is not present, |-NoValue-| is returned. Each |
| 195 |
% embellishment gives one argument, ordered as for the list of |
| 196 |
% \meta{tokens} in the argument specification. All \meta{tokens} |
| 197 |
% must be distinct. \emph{This is an experimental type}. |
| 198 |
% \item[E] As for \texttt{e} but returns one or more \meta{defaults} |
| 199 |
% if values are not given: \texttt{E}\marg{tokens}\marg{defaults}. See |
| 200 |
% Section~\ref{sec:embellishment} for more details. |
| 201 |
% \end{itemize} |
| 202 |
% |
| 203 |
% Using these specifiers, it is possible to create complex input syntax |
| 204 |
% very easily. For example, given the argument definition |
| 205 |
% `|s o o m O{default}|', the input `|*[Foo]{Bar}|' would be parsed as: |
| 206 |
% \begin{itemize}[nolistsep] |
| 207 |
% \item |#1| = |\BooleanTrue| |
| 208 |
% \item |#2| = |Foo| |
| 209 |
% \item |#3| = |-NoValue-| |
| 210 |
% \item |#4| = |Bar| |
| 211 |
% \item |#5| = |default| |
| 212 |
% \end{itemize} |
| 213 |
% whereas `|[One][Two]{}[Three]|' would be parsed as: |
| 214 |
% \begin{itemize}[nolistsep] |
| 215 |
% \item |#1| = |\BooleanFalse| |
| 216 |
% \item |#2| = |One| |
| 217 |
% \item |#3| = |Two| |
| 218 |
% \item |#4| = || |
| 219 |
% \item |#5| = |Three| |
| 220 |
% \end{itemize} |
| 221 |
% |
| 222 |
% Delimited argument types (\texttt{d}, \texttt{o} and \texttt{r}) are |
| 223 |
% defined such that they require matched pairs of delimiters when collecting |
| 224 |
% an argument. For example |
| 225 |
% \begin{verbatim} |
| 226 |
% \NewDocumentCommand{\foo}{o}{#1} |
| 227 |
% \foo[[content]] % #1 = "[content]" |
| 228 |
% \foo[[] % Error: missing closing "]" |
| 229 |
% \end{verbatim} |
| 230 |
% Also note that |{| and |}| cannot be used as delimiters as they are used |
| 231 |
% by \TeX{} as grouping tokens. Implicit begin- or end-group tokens (\emph{e.g.}, |
| 232 |
% |\bgroup| and |\egroup|) are not allowed for delimited argument tipes. |
| 233 |
% Arguments to be grabbed inside these tokens |
| 234 |
% must be created as either \texttt{m}- or \texttt{g}-type arguments. |
| 235 |
% |
| 236 |
% Within delimited arguments, non-balanced or otherwise awkward tokens may |
| 237 |
% be included by protecting the entire argument with a brace pair |
| 238 |
% \begin{verbatim} |
| 239 |
% \NewDocumentCommand{\foobar}{o}{#1} |
| 240 |
% \foobar[{[}] % Allowed as the "[" is 'hidden' |
| 241 |
% \end{verbatim} |
| 242 |
% These braces will be stripped only if they surround the \emph{entire} content |
| 243 |
% of the optional argument |
| 244 |
% \begin{verbatim} |
| 245 |
% \NewDocumentCommand{\foobaz}{o}{#1} |
| 246 |
% \foobaz[{abc}] % => "abc" |
| 247 |
% \foobaz[ {abc}] % => " {abc}" |
| 248 |
% \end{verbatim} |
| 249 |
% |
| 250 |
% Two more characters have a special meaning when creating an argument |
| 251 |
% specifier. First, \texttt{+} is used to make an argument long (to |
| 252 |
% accept paragraph tokens). In contrast to \LaTeXe's \cs{newcommand}, |
| 253 |
% this applies on an argument-by-argument basis. So modifying the |
| 254 |
% example to `|s o o +m O{default}|' means that the mandatory argument |
| 255 |
% is now \cs{long}, whereas the optional arguments are not. |
| 256 |
% |
| 257 |
% Secondly, the character \texttt{>} is used to declare so-called |
| 258 |
% \enquote{argument processors}, which can be used to modify the contents of an |
| 259 |
% argument before it is passed to the macro definition. The use of |
| 260 |
% argument processors is a somewhat advanced topic, (or at least a less |
| 261 |
% commonly used feature) and is covered in Section~\ref{sec:processors}. |
| 262 |
% |
| 263 |
% When an optional argument is followed by a mandatory argument with the |
| 264 |
% same delimiter, \pkg{xparse} issues a warning because the optional |
| 265 |
% argument could not be omitted by the user, thus becoming in effect |
| 266 |
% mandatory. This can apply to \texttt{o}, \texttt{d}, \texttt{O}, |
| 267 |
% \texttt{D}, \texttt{s}, \texttt{t}, \texttt{e}, and \texttt{E} type |
| 268 |
% arguments followed by \texttt{r} or \texttt{R}-type required |
| 269 |
% arguments, but also to \texttt{g} or \texttt{G} type arguments |
| 270 |
% followed by \texttt{m} type arguments. |
| 271 |
% |
| 272 |
% As \pkg{xparse} is also used to describe interfaces that have appeared |
| 273 |
% in the wider \LaTeXe{} eco-system, it also defines additional argument |
| 274 |
% types, described in Section~\ref{sec:backwards}: the mandatory types |
| 275 |
% \texttt{l} and \texttt{u} and the optional brace group types |
| 276 |
% \texttt{g} and \texttt{G}. Their use is not recommended because it is |
| 277 |
% simpler for a user if all packages use a similar syntax. For the same |
| 278 |
% reason, delimited arguments \texttt{r}, \texttt{R}, \texttt{d} and |
| 279 |
% \texttt{D} should normally use delimiters that are naturally paired, |
| 280 |
% such as |[| and |]| or |(| and |)|, or that are identical, such as |"| |
| 281 |
% and~|"|. A very common syntax is to have one optional argument |
| 282 |
% \texttt{o} treated as a key--value list (using for instance |
| 283 |
% \pkg{l3keys}) followed by some mandatory arguments~\texttt{m} (or |
| 284 |
% \texttt{+m}). |
| 285 |
% |
| 286 |
% \subsection{Spacing and optional arguments} |
| 287 |
% |
| 288 |
% \TeX{} will find the first argument after a function name irrespective |
| 289 |
% of any intervening spaces. This is true for both mandatory and |
| 290 |
% optional arguments. So |\foo[arg]| and \verb*|\foo [arg]| are |
| 291 |
% equivalent. Spaces are also ignored when collecting arguments up |
| 292 |
% to the last mandatory argument to be collected (as it must exist). |
| 293 |
% So after |
| 294 |
% \begin{verbatim} |
| 295 |
% \NewDocumentCommand \foo { m o m } { ... } |
| 296 |
% \end{verbatim} |
| 297 |
% the user input |\foo{arg1}[arg2]{arg3}| and |
| 298 |
% \verb*|\foo{arg1} [arg2] {arg3}| will both be parsed in the same |
| 299 |
% way. |
| 300 |
% |
| 301 |
% The behavior of optional arguments \emph{after} any mandatory arguments is |
| 302 |
% selectable. The standard settings will allow spaces here, and thus |
| 303 |
% with |
| 304 |
% \begin{verbatim} |
| 305 |
% \NewDocumentCommand \foobar { m o } { ... } |
| 306 |
% \end{verbatim} |
| 307 |
% both |\foobar{arg1}[arg2]| and \verb*|\foobar{arg1} [arg2]| will find an |
| 308 |
% optional argument. This can be changed by giving the modified |!| in |
| 309 |
% the argument specification: |
| 310 |
% \begin{verbatim} |
| 311 |
% \NewDocumentCommand \foobar { m !o } { ... } |
| 312 |
% \end{verbatim} |
| 313 |
% where \verb*|\foobar{arg1} [arg2]| will not find an optional argument. |
| 314 |
% |
| 315 |
% There is one subtlety here due to the difference in handling by \TeX{} |
| 316 |
% of \enquote{control symbols}, where the command name is made up of a single |
| 317 |
% character, such as \enquote{\cmd{\\}}. Spaces are not ignored by \TeX{} |
| 318 |
% here, and thus it is possible to require an optional argument directly |
| 319 |
% follow such a command. The most common example is the use of \cmd{\\} in |
| 320 |
% \pkg{amsmath} environments. In \pkg{xparse} terms it has signature |
| 321 |
% \begin{verbatim} |
| 322 |
% \DeclareDocumentCommand \\ { !s !o } { ... } |
| 323 |
% \end{verbatim} |
| 324 |
% |
| 325 |
% \subsection{Required delimited arguments} |
| 326 |
% |
| 327 |
% The contrast between a delimited (\texttt{D}-type) and \enquote{required |
| 328 |
% delimited} (\texttt{R}-type) argument is that an error will be raised if |
| 329 |
% the latter is missing. Thus for example |
| 330 |
% \begin{verbatim} |
| 331 |
% \NewDocumentCommand {\foobaz} {r()m} {} |
| 332 |
% \foobaz{oops} |
| 333 |
% \end{verbatim} |
| 334 |
% will lead to an error message being issued. The marker |-NoValue-| |
| 335 |
% (\texttt{r}-type) or user-specified default (for \texttt{R}-type) will be |
| 336 |
% inserted to allow error recovery. |
| 337 |
% |
| 338 |
% \subsection{Verbatim arguments} |
| 339 |
% |
| 340 |
% Arguments of type~\texttt{v} are read in verbatim mode, which will |
| 341 |
% result in the grabbed argument consisting of tokens of category codes |
| 342 |
% $12$~(\enquote{other}) and $13$~(\enquote{active}), except spaces, |
| 343 |
% which are given category code $10$~(\enquote{space}). The argument is |
| 344 |
% delimited in a similar manner to the \LaTeXe{} \cs{verb} function, or |
| 345 |
% by (correctly nested) pairs of braces. |
| 346 |
% |
| 347 |
% Functions containing verbatim arguments cannot appear in the arguments |
| 348 |
% of other functions. The \texttt{v}~argument specifier includes code to check |
| 349 |
% this, and will raise an error if the grabbed argument has already been |
| 350 |
% tokenized by \TeX{} in an irreversible way. |
| 351 |
% |
| 352 |
% By default, an argument of type~\texttt{v} must be at most one line. |
| 353 |
% Prefixing with \texttt{+} allows line breaks within the argument. |
| 354 |
% |
| 355 |
% \subsection{Default values of arguments} |
| 356 |
% \label{sec:defaultvaluesofarguments} |
| 357 |
% |
| 358 |
% Uppercase argument types (\texttt{O}, \texttt{D}, \ldots{}) allow to |
| 359 |
% specify a default value to be used when the argument is missing; their |
| 360 |
% lower-case counterparts use the special marker |-NoValue-|. The |
| 361 |
% default value can be expressed in terms of the value of any other |
| 362 |
% arguments by using |#1|, |#2|, and so on. |
| 363 |
% \begin{verbatim} |
| 364 |
% \NewDocumentCommand {\conjugate} { m O{#1ed} O{#2} } {(#1,#2,#3)} |
| 365 |
% \conjugate {walk} % => (walk,walked,walked) |
| 366 |
% \conjugate {find} [found] % => (find,found,found) |
| 367 |
% \conjugate {do} [did] [done] % => (do,did,done) |
| 368 |
% \end{verbatim} |
| 369 |
% The default values may refer to arguments that appear later in the |
| 370 |
% argument specification. For instance a command could accept two |
| 371 |
% optional arguments, equal by default: |
| 372 |
% \begin{verbatim} |
| 373 |
% \NewDocumentCommand {\margins} { O{#3} m O{#1} m } {(#1,#2,#3,#4)} |
| 374 |
% \margins {a} {b} % => {(-NoValue-,a,-NoValue-,b)} |
| 375 |
% \margins [1cm] {a} {b} % => {(1cm,a,1cm,b)} |
| 376 |
% \margins {a} [1cm] {b} % => {(1cm,a,1cm,b)} |
| 377 |
% \margins [1cm] {a} [2cm] {b} % => {(1cm,a,2cm,b)} |
| 378 |
% \end{verbatim} |
| 379 |
% |
| 380 |
% \subsection{Default values for \enquote{embellishments}} |
| 381 |
% \label{sec:embellishment} |
| 382 |
% |
| 383 |
% The \texttt{E}-type argument allows one default value per test token. |
| 384 |
% This is achieved by giving a list of defaults for each entry in the |
| 385 |
% list, for example: |
| 386 |
% \begin{verbatim} |
| 387 |
% E{^_}{{UP}{DOWN}} |
| 388 |
% \end{verbatim} |
| 389 |
% If the list of default values is \emph{shorter} than the list of test tokens, |
| 390 |
% the special |-NoValue-| marker will be returned (as for the \texttt{e}-type |
| 391 |
% argument). Thus for example |
| 392 |
% \begin{verbatim} |
| 393 |
% E{^_}{{UP}} |
| 394 |
% \end{verbatim} |
| 395 |
% has default \texttt{UP} for the |^| test character, but will return the |
| 396 |
% |-NoValue-| marker as a default for |_|. This allows mixing of explicit |
| 397 |
% defaults with testing for missing values. |
| 398 |
% |
| 399 |
% \subsection{Body of an environment} |
| 400 |
% \label{sec:body} |
| 401 |
% |
| 402 |
% While environments |\begin|\marg{environment} \dots{} |
| 403 |
% |\end|\marg{environment} are typically used in cases where the code |
| 404 |
% implementing the \meta{environment} does not need to access the |
| 405 |
% contents of the environment (its \enquote{body}), it is sometimes |
| 406 |
% useful to have the body as a standard argument. |
| 407 |
% |
| 408 |
% This is achieved in \pkg{xparse} by ending the argument specification |
| 409 |
% with~\texttt{b}. The approach taken in \pkg{xparse} is |
| 410 |
% different from the earlier packages \pkg{environ} or \pkg{newenviron}: |
| 411 |
% the body of the environment is provided to the code part as a usual |
| 412 |
% argument |#1|, |#2| etc.\@, rather than stored in a macro such as |
| 413 |
% \cs[no-index]{BODY}. |
| 414 |
% |
| 415 |
% For instance |
| 416 |
% \begin{verbatim} |
| 417 |
% \NewDocumentEnvironment { twice } |
| 418 |
% { O{\ttfamily} +b } |
| 419 |
% {#2#1#2} {} |
| 420 |
% \begin{twice}[\itshape] |
| 421 |
% Hello world! |
| 422 |
% \end{twice} |
| 423 |
% \end{verbatim} |
| 424 |
% typesets \enquote{Hello world!{\itshape Hello world!}}. |
| 425 |
% |
| 426 |
% The prefix |+| is used to allow multiple paragraphs in the |
| 427 |
% environment's body. Argument processors can also be applied to |
| 428 |
% \texttt{b}~arguments. |
| 429 |
% |
| 430 |
% By default, spaces are trimmed at both ends of the body: in the |
| 431 |
% example there would otherwise be spaces coming from the ends the lines |
| 432 |
% after |[\itshape]| and |world!|. Putting the prefix |!| before |
| 433 |
% \texttt{b} suppresses space-trimming. |
| 434 |
% |
| 435 |
% When \texttt{b} is used in the argument specification, |
| 436 |
% the last argument of \cs{NewDocumentEnvironment}, which consists of |
| 437 |
% an \meta{end code} to insert at |\end|\marg{environment}, is |
| 438 |
% redundant since one can simply put that code at the end of the |
| 439 |
% \meta{start code}. Nevertheless this (empty) \meta{end code} must be |
| 440 |
% provided. |
| 441 |
% |
| 442 |
% Environments that use this feature can be nested. |
| 443 |
% |
| 444 |
% \subsection{Starred environments} |
| 445 |
% |
| 446 |
% Many packages define environments with and without \texttt{*} in their |
| 447 |
% name, for instance \texttt{tabular} and \texttt{tabular*}. At |
| 448 |
% present, \pkg{xparse} does not provide specific tools to define these: |
| 449 |
% one should simply define the two environment separately, for instance |
| 450 |
% \begin{verbatim} |
| 451 |
% \NewDocumentEnvironment { tabular } { o +m } {...} {...} |
| 452 |
% \NewDocumentEnvironment { tabular* } { m o +m } {...} {...} |
| 453 |
% \end{verbatim} |
| 454 |
% Of course the implementation of these two environments, denoted |
| 455 |
% \enquote{\texttt{...}} in this example, can rely on the same internal |
| 456 |
% commands. |
| 457 |
% |
| 458 |
% Note that this situation is different from the \texttt{s} argument |
| 459 |
% type: if the signature of an environment starts with~\texttt{s} then |
| 460 |
% the star is searched for after the argument of \cs{begin}. For |
| 461 |
% instance, the following typesets \texttt{star}. |
| 462 |
% \begin{verbatim} |
| 463 |
% \NewDocumentEnvironment { envstar } { s } |
| 464 |
% {\IfBooleanTF {#1} {star} {no star}} {} |
| 465 |
% \begin{envstar}* |
| 466 |
% \end{envstar} |
| 467 |
% \end{verbatim} |
| 468 |
% |
| 469 |
% \subsection{Backwards Compatibility} |
| 470 |
% \label{sec:backwards} |
| 471 |
% |
| 472 |
% One role of \pkg{xparse} is to describe existing \LaTeX{} interfaces, |
| 473 |
% including some that are rather unusual in \LaTeX{} (as opposed to |
| 474 |
% formats such as plain \TeX{}) such as delimited arguments. As such, |
| 475 |
% the package defines some argument specifiers that should largely be |
| 476 |
% avoided nowadays as using them in packages leads to inconsistent user |
| 477 |
% interfaces. The simplest syntax is often best, with argument |
| 478 |
% specifications such as |mmmm| or |ommmm|, namely an optional argument |
| 479 |
% followed by some standard mandatory ones. The optional argument can |
| 480 |
% be made to support key--value syntax using tools from \pkg{l3keys}. |
| 481 |
% |
| 482 |
% The argument types that are not recommended any longer are: |
| 483 |
% \begin{itemize}[font=\ttfamily] |
| 484 |
% \item[l] A mandatory argument which reads everything up to the first |
| 485 |
% begin-group token: in standard \LaTeX{} this is a left brace. |
| 486 |
% \item[u] Reads a mandatory argument \enquote{until} \meta{tokens} are encountered, |
| 487 |
% where the desired \meta{tokens} are given as an argument to the |
| 488 |
% specifier: \texttt{u}\marg{tokens}. |
| 489 |
% \item[g] An optional argument given inside a pair of \TeX{} group |
| 490 |
% tokens (in standard \LaTeX{}, |{| \ldots |}|), which returns |
| 491 |
% |-NoValue-| if not present. |
| 492 |
% \item[G] As for \texttt{g} but returns \meta{default} if no value |
| 493 |
% is given: \texttt{G}\marg{default}. |
| 494 |
% \end{itemize} |
| 495 |
% |
| 496 |
% \subsection{Details about argument delimiters} |
| 497 |
% |
| 498 |
% In normal (non-expandable) commands, the delimited types look for the |
| 499 |
% initial delimiter by peeking ahead (using \pkg{expl3}'s |\peek_...| |
| 500 |
% functions) looking for the delimiter token. The token has to have the |
| 501 |
% same meaning and \enquote{shape} of the token defined as delimiter. |
| 502 |
% There are three possible cases of delimiters: character tokens, control |
| 503 |
% sequence tokens, and active character tokens. For all practical purposes |
| 504 |
% of this description, active character tokens will behave exactly as |
| 505 |
% control sequence tokens. |
| 506 |
% |
| 507 |
% \subsubsection{Character tokens} |
| 508 |
% |
| 509 |
% A character token is characterized by its character code, and its meaning |
| 510 |
% is the category code~(|\catcode|). When a command is defined, the meaning |
| 511 |
% of the character token is fixed into the definition of the command and |
| 512 |
% cannot change. A command will correctly see an argument delimiter if |
| 513 |
% the open delimiter has the same character and category codes as at the |
| 514 |
% time of the definition. For example in: |
| 515 |
% \begin{verbatim} |
| 516 |
% \NewDocumentCommand { \foobar } { D<>{default} } {(#1)} |
| 517 |
% \foobar <hello> \par |
| 518 |
% \char_set_catcode_letter:N < |
| 519 |
% \foobar <hello> |
| 520 |
% \end{verbatim} |
| 521 |
% the output would be: |
| 522 |
% \begin{verbatim} |
| 523 |
% (hello) |
| 524 |
% (default)<hello> |
| 525 |
% \end{verbatim} |
| 526 |
% as the open-delimiter |<| changed in meaning between the two calls to |
| 527 |
% |\foobar|, so the second one doesn't see the |<| as a valid delimiter. |
| 528 |
% Commands assume that if a valid open-delimiter was found, a matching |
| 529 |
% close-delimiter will also be there. If it is not (either by being |
| 530 |
% omitted or by changing in meaning), a low-level \TeX{} error is raised |
| 531 |
% and the command call is aborted. |
| 532 |
% |
| 533 |
% \subsubsection{Control sequence tokens} |
| 534 |
% |
| 535 |
% A control sequence (or control character) token is characterized by is |
| 536 |
% its name, and its meaning is its definition. |
| 537 |
% A token cannot have two different meanings at the same time. |
| 538 |
% When a control sequence is defined as delimiter in a command, |
| 539 |
% it will be detected as delimiter whenever the control sequence name |
| 540 |
% is found in the document regardless of its current definition. |
| 541 |
% For example in: |
| 542 |
% \begin{verbatim} |
| 543 |
% \cs_set:Npn \x { abc } |
| 544 |
% \NewDocumentCommand { \foobar } { D\x\y{default} } {(#1)} |
| 545 |
% \foobar \x hello\y \par |
| 546 |
% \cs_set:Npn \x { def } |
| 547 |
% \foobar \x hello\y |
| 548 |
% \end{verbatim} |
| 549 |
% the output would be: |
| 550 |
% \begin{verbatim} |
| 551 |
% (hello) |
| 552 |
% (hello) |
| 553 |
% \end{verbatim} |
| 554 |
% with both calls to the command seeing the delimiter |\x|. |
| 555 |
% |
| 556 |
% \section{Declaring commands and environments} |
| 557 |
% |
| 558 |
% With the concept of an argument specifier defined, it is now |
| 559 |
% possible to describe the methods available for creating both |
| 560 |
% functions and environments using \pkg{xparse}. |
| 561 |
% |
| 562 |
% The interface-building commands are the preferred method for |
| 563 |
% creating document-level functions in \LaTeX3. All of the functions |
| 564 |
% generated in this way are naturally robust (using the \eTeX{} |
| 565 |
% \cs{protected} mechanism). |
| 566 |
% |
| 567 |
% \begin{function} |
| 568 |
% { |
| 569 |
% \NewDocumentCommand , |
| 570 |
% \RenewDocumentCommand , |
| 571 |
% \ProvideDocumentCommand , |
| 572 |
% \DeclareDocumentCommand |
| 573 |
% } |
| 574 |
% \begin{syntax} |
| 575 |
% \cs{NewDocumentCommand} \meta{function} \Arg{arg spec} \Arg{code} |
| 576 |
% \end{syntax} |
| 577 |
% This family of commands are used to create a document-level |
| 578 |
% \meta{function}. The argument specification for the function is |
| 579 |
% given by \meta{arg spec}, and the function expands to the |
| 580 |
% \meta{code} with |#1|, |#2|, etc.\ replaced by the arguments found |
| 581 |
% by \pkg{xparse}. |
| 582 |
% \end{function} |
| 583 |
% |
| 584 |
% As an example: |
| 585 |
% \begin{verbatim} |
| 586 |
% \NewDocumentCommand \chapter { s o m } |
| 587 |
% { |
| 588 |
% \IfBooleanTF {#1} |
| 589 |
% { \typesetstarchapter {#3} } |
| 590 |
% { \typesetnormalchapter {#2} {#3} } |
| 591 |
% } |
| 592 |
% \end{verbatim} |
| 593 |
% would be a way to define a \cs{chapter} command which would |
| 594 |
% essentially behave like the current \LaTeXe{} command (except that it |
| 595 |
% would accept an optional argument even when a \texttt{*} was parsed). |
| 596 |
% The \cs{typesetnormalchapter} could test its first argument for being |
| 597 |
% |-NoValue-| to see if an optional argument was present. |
| 598 |
% |
| 599 |
% The difference between the \cs{New\ldots} \cs{Renew\ldots}, |
| 600 |
% \cs{Provide\ldots} and \cs{Declare\ldots} versions is the behavior |
| 601 |
% if \meta{function} is already defined. |
| 602 |
% \begin{itemize} |
| 603 |
% \item \cs{NewDocumentCommand} will issue an error if \meta{function} |
| 604 |
% has already been defined. |
| 605 |
% \item \cs{RenewDocumentCommand} will issue an error if \meta{function} |
| 606 |
% has not previously been defined. |
| 607 |
% \item \cs{ProvideDocumentCommand} creates a new definition for |
| 608 |
% \meta{function} only if one has not already been given. |
| 609 |
% \item \cs{DeclareDocumentCommand} will always create the new |
| 610 |
% definition, irrespective of any existing \meta{function} with the |
| 611 |
% same name. This should be used sparingly. |
| 612 |
% \end{itemize} |
| 613 |
% |
| 614 |
% \begin{texnote} |
| 615 |
% Unlike \LaTeXe{}'s \cs{newcommand} and relatives, the |
| 616 |
% \cs{NewDocumentCommand} family of functions do not prevent creation of |
| 617 |
% functions with names starting \cs{end\ldots}. |
| 618 |
% \end{texnote} |
| 619 |
% |
| 620 |
% \begin{function} |
| 621 |
% { |
| 622 |
% \NewDocumentEnvironment , |
| 623 |
% \RenewDocumentEnvironment , |
| 624 |
% \ProvideDocumentEnvironment , |
| 625 |
% \DeclareDocumentEnvironment |
| 626 |
% } |
| 627 |
% \begin{syntax} |
| 628 |
% \cs{NewDocumentEnvironment} \Arg{environment} \Arg{arg spec} |
| 629 |
% ~~\Arg{start code} \Arg{end code} |
| 630 |
% \end{syntax} |
| 631 |
% These commands work in the same way as \cs{NewDocumentCommand}, |
| 632 |
% etc.\@, but create environments (\cs{begin}\Arg{environment} \ldots{} |
| 633 |
% \cs{end}\Arg{environment}). Both the \meta{start code} and |
| 634 |
% \meta{end code} |
| 635 |
% may access the arguments as defined by \meta{arg spec}. |
| 636 |
% The arguments will be given following \cs{begin}\Arg{environment}. |
| 637 |
% \end{function} |
| 638 |
% |
| 639 |
% \section{Other \pkg{xparse} commands} |
| 640 |
% |
| 641 |
% \subsection{Testing special values} |
| 642 |
% |
| 643 |
% Optional arguments created using \pkg{xparse} make use of dedicated |
| 644 |
% variables to return information about the nature of the argument |
| 645 |
% received. |
| 646 |
% |
| 647 |
% \begin{function}[EXP]{\IfNoValueT, \IfNoValueF, \IfNoValueTF} |
| 648 |
% \begin{syntax} |
| 649 |
% \cs{IfNoValueTF} \Arg{argument} \Arg{true code} \Arg{false code} |
| 650 |
% \cs{IfNoValueT} \Arg{argument} \Arg{true code} |
| 651 |
% \cs{IfNoValueF} \Arg{argument} \Arg{false code} |
| 652 |
% \end{syntax} |
| 653 |
% The \cs{IfNoValue(TF)} tests are used to check if \meta{argument} (|#1|, |
| 654 |
% |#2|, etc.) is the special |-NoValue-| marker For example |
| 655 |
% \begin{verbatim} |
| 656 |
% \NewDocumentCommand \foo { o m } |
| 657 |
% { |
| 658 |
% \IfNoValueTF {#1} |
| 659 |
% { \DoSomethingJustWithMandatoryArgument {#2} } |
| 660 |
% { \DoSomethingWithBothArguments {#1} {#2} } |
| 661 |
% } |
| 662 |
% \end{verbatim} |
| 663 |
% will use a different internal function if the optional argument |
| 664 |
% is given than if it is not present. |
| 665 |
% |
| 666 |
% Note that three tests are available, depending on which outcome |
| 667 |
% branches are required: \cs{IfNoValueTF}, \cs{IfNoValueT} and |
| 668 |
% \cs{IfNoValueF}. |
| 669 |
% |
| 670 |
% As the \cs{IfNoValue(TF)} tests are expandable, it is possible to |
| 671 |
% test these values later, for example at the point of typesetting or |
| 672 |
% in an expansion context. |
| 673 |
% |
| 674 |
% It is important to note that |-NoValue-| is constructed such that it |
| 675 |
% will \emph{not} match the simple text input |-NoValue-|, i.e. |
| 676 |
% that |
| 677 |
% \begin{verbatim} |
| 678 |
% \IfNoValueTF{-NoValue-} |
| 679 |
% \end{verbatim} |
| 680 |
% will be logically \texttt{false}. |
| 681 |
% |
| 682 |
% When two optional arguments follow each other (a syntax we typically |
| 683 |
% discourage), it can make sense to allow users of the command to |
| 684 |
% specify only the second argument by providing an empty first |
| 685 |
% argument. Rather than testing separately for emptiness and for |
| 686 |
% |-NoValue-| it is then best to use the argument type~|O| with an |
| 687 |
% empty default value, and simply test for emptiness using the |
| 688 |
% \pkg{expl3} conditional \cs{tl_if_blank:nTF} or its \pkg{etoolbox} |
| 689 |
% analogue \tn{ifblank}. |
| 690 |
% \end{function} |
| 691 |
% |
| 692 |
% \begin{function}[EXP]{\IfValueT, \IfValueF, \IfValueTF} |
| 693 |
% \begin{syntax} |
| 694 |
% \cs{IfValueTF} \Arg{argument} \Arg{true code} \Arg{false code} |
| 695 |
% \end{syntax} |
| 696 |
% The reverse form of the \cs{IfNoValue(TF)} tests are also available |
| 697 |
% as \cs{IfValue(TF)}. The context will determine which logical |
| 698 |
% form makes the most sense for a given code scenario. |
| 699 |
% \end{function} |
| 700 |
% |
| 701 |
% \begin{variable}{\BooleanFalse, \BooleanTrue} |
| 702 |
% The \texttt{true} and \texttt{false} flags set when searching for |
| 703 |
% an optional character (using \texttt{s} or \texttt{t\meta{char}}) have |
| 704 |
% names which are accessible outside of code blocks. |
| 705 |
% \end{variable} |
| 706 |
% |
| 707 |
% \begin{function}[EXP]{\IfBooleanT, \IfBooleanF, \IfBooleanTF} |
| 708 |
% \begin{syntax} |
| 709 |
% \cs{IfBooleanTF} \Arg{argument} \Arg{true code} \Arg{false code} |
| 710 |
% \end{syntax} |
| 711 |
% Used to test if \meta{argument} (|#1|, |#2|, etc.) is |
| 712 |
% \cs{BooleanTrue} or \cs{BooleanFalse}. For example |
| 713 |
% \begin{verbatim} |
| 714 |
% \NewDocumentCommand \foo { s m } |
| 715 |
% { |
| 716 |
% \IfBooleanTF {#1} |
| 717 |
% { \DoSomethingWithStar {#2} } |
| 718 |
% { \DoSomethingWithoutStar {#2} } |
| 719 |
% } |
| 720 |
% \end{verbatim} |
| 721 |
% checks for a star as the first argument, then chooses the action to |
| 722 |
% take based on this information. |
| 723 |
% \end{function} |
| 724 |
% |
| 725 |
% \subsection{Argument processors} |
| 726 |
% \label{sec:processors} |
| 727 |
% |
| 728 |
% \pkg{xparse} introduces the idea of an argument processor, which is |
| 729 |
% applied to an argument \emph{after} it has been grabbed by the |
| 730 |
% underlying system but before it is passed to \meta{code}. An argument |
| 731 |
% processor can therefore be used to regularize input at an early stage, |
| 732 |
% allowing the internal functions to be completely independent of input |
| 733 |
% form. Processors are applied to user input and to default values for |
| 734 |
% optional arguments, but \emph{not} to the special |-NoValue-| marker. |
| 735 |
% |
| 736 |
% Each argument processor is specified by the syntax |
| 737 |
% \texttt{>}\marg{processor} in the argument specification. Processors |
| 738 |
% are applied from right to left, so that |
| 739 |
% \begin{verbatim} |
| 740 |
% >{\ProcessorB} >{\ProcessorA} m |
| 741 |
% \end{verbatim} |
| 742 |
% would apply \cs{ProcessorA} |
| 743 |
% followed by \cs{ProcessorB} to the tokens grabbed by the \texttt{m} |
| 744 |
% argument. |
| 745 |
% |
| 746 |
% It might sometimes be useful to use the value of another argument as |
| 747 |
% one of the arguments of a processor. For example, using the |
| 748 |
% \cs{SplitList} processor defined below, |
| 749 |
% \begin{verbatim} |
| 750 |
% \NewDocumentCommand \foo { O{,} >{\SplitList{#1}} m } { \foobar{#2} } |
| 751 |
% \foo{a,b;c,d} |
| 752 |
% \end{verbatim} |
| 753 |
% results in |\foobar| receiving the argument |{a}{b;c}{d}| because |
| 754 |
% \cs{SplitList} receives as its two arguments the optional one (whose |
| 755 |
% value here is the default, a comma) and the mandatory one. To |
| 756 |
% summarize, first the arguments are searched for in the input, then any |
| 757 |
% default argument is determined as explained in |
| 758 |
% Section~\ref{sec:defaultvaluesofarguments}, then these default |
| 759 |
% arguments are passed to any processor. When referring to arguments |
| 760 |
% (through |#1|, |#2| and so on) in a processor, the arguments used are |
| 761 |
% always those before applying any processor. |
| 762 |
% |
| 763 |
% \begin{variable}{\ProcessedArgument} |
| 764 |
% \pkg{xparse} defines a very small set of processor functions. In the |
| 765 |
% main, it is anticipated that code writers will want to create their |
| 766 |
% own processors. These need to accept one argument, which is the |
| 767 |
% tokens as grabbed (or as returned by a previous processor function). |
| 768 |
% Processor functions should return the processed argument as the |
| 769 |
% variable \cs{ProcessedArgument}. |
| 770 |
% \end{variable} |
| 771 |
% |
| 772 |
% \begin{function}{\ReverseBoolean} |
| 773 |
% \begin{syntax} |
| 774 |
% \cs{ReverseBoolean} |
| 775 |
% \end{syntax} |
| 776 |
% This processor reverses the logic of \cs{BooleanTrue} and |
| 777 |
% \cs{BooleanFalse}, so that the example from earlier would become |
| 778 |
% \begin{verbatim} |
| 779 |
% \NewDocumentCommand \foo { > { \ReverseBoolean } s m } |
| 780 |
% { |
| 781 |
% \IfBooleanTF #1 |
| 782 |
% { \DoSomethingWithoutStar {#2} } |
| 783 |
% { \DoSomethingWithStar {#2} } |
| 784 |
% } |
| 785 |
% \end{verbatim} |
| 786 |
% \end{function} |
| 787 |
% |
| 788 |
% \begin{function}[updated = 2012-02-12]{\SplitArgument} |
| 789 |
% \begin{syntax} |
| 790 |
% \cs{SplitArgument} \Arg{number} \Arg{token(s)} |
| 791 |
% \end{syntax} |
| 792 |
% This processor splits the argument given at each occurrence of the |
| 793 |
% \meta{tokens} up to a maximum of \meta{number} tokens (thus |
| 794 |
% dividing the input into $\text{\meta{number}} + 1$ parts). |
| 795 |
% An error is given if too many \meta{tokens} are present in the |
| 796 |
% input. The processed input is placed inside |
| 797 |
% $\text{\meta{number}} + 1$ sets of braces for further use. |
| 798 |
% If there are fewer than \Arg{number} of \Arg{tokens} in the argument |
| 799 |
% then |-NoValue-| markers are added at the end of the processed |
| 800 |
% argument. |
| 801 |
% \begin{verbatim} |
| 802 |
% \NewDocumentCommand \foo |
| 803 |
% { > { \SplitArgument { 2 } { ; } } m } |
| 804 |
% { \InternalFunctionOfThreeArguments #1 } |
| 805 |
% \end{verbatim} |
| 806 |
% If only a single character \meta{token} is used for the split, any |
| 807 |
% category code $13$ (active) character matching the \meta{token} will |
| 808 |
% be replaced before the split takes place. |
| 809 |
% Spaces are trimmed at each end of each item parsed. |
| 810 |
% \end{function} |
| 811 |
% |
| 812 |
% \begin{function}{\SplitList} |
| 813 |
% \begin{syntax} |
| 814 |
% \cs{SplitList} \Arg{token(s)} |
| 815 |
% \end{syntax} |
| 816 |
% This processor splits the argument given at each occurrence of the |
| 817 |
% \meta{token(s)} where the number of items is not fixed. Each item is |
| 818 |
% then wrapped in braces within |#1|. The result is that the |
| 819 |
% processed argument can be further processed using a mapping function. |
| 820 |
% \begin{verbatim} |
| 821 |
% \NewDocumentCommand \foo |
| 822 |
% { > { \SplitList { ; } } m } |
| 823 |
% { \MappingFunction #1 } |
| 824 |
% \end{verbatim} |
| 825 |
% If only a single character \meta{token} is used for the split, any |
| 826 |
% category code $13$ (active) character matching the \meta{token} will |
| 827 |
% be replaced before the split takes place. |
| 828 |
% Spaces are trimmed at each end of each item parsed. |
| 829 |
% \end{function} |
| 830 |
% |
| 831 |
% \begin{function}[EXP]{\ProcessList} |
| 832 |
% \begin{syntax} |
| 833 |
% \cs{ProcessList} \Arg{list} \Arg{function} |
| 834 |
% \end{syntax} |
| 835 |
% To support \cs{SplitList}, the function \cs{ProcessList} is available |
| 836 |
% to apply a \meta{function} to every entry in a \meta{list}. The |
| 837 |
% \meta{function} should absorb one argument: the list entry. For example |
| 838 |
% \begin{verbatim} |
| 839 |
% \NewDocumentCommand \foo |
| 840 |
% { > { \SplitList { ; } } m } |
| 841 |
% { \ProcessList {#1} { \SomeDocumentFunction } } |
| 842 |
% \end{verbatim} |
| 843 |
% |
| 844 |
% \textbf{This function is experimental.} |
| 845 |
% \end{function} |
| 846 |
% |
| 847 |
% \begin{function}{\TrimSpaces} |
| 848 |
% \begin{syntax} |
| 849 |
% \cs{TrimSpaces} |
| 850 |
% \end{syntax} |
| 851 |
% Removes any leading and trailing spaces (tokens with character code~$32$ |
| 852 |
% and category code~$10$) for the ends of the argument. Thus for example |
| 853 |
% declaring a function |
| 854 |
% \begin{verbatim} |
| 855 |
% \NewDocumentCommand \foo |
| 856 |
% { > { \TrimSpaces } m } |
| 857 |
% { \showtokens {#1} } |
| 858 |
% \end{verbatim} |
| 859 |
% and using it in a document as |
| 860 |
% \begin{verbatim} |
| 861 |
% \foo{ hello world } |
| 862 |
% \end{verbatim} |
| 863 |
% will show \texttt{hello world} at the terminal, with the space at each |
| 864 |
% end removed. \cs{TrimSpaces} will remove multiple spaces from the ends of |
| 865 |
% the input in cases where these have been included such that the standard |
| 866 |
% \TeX{} conversion of multiple spaces to a single space does not apply. |
| 867 |
% |
| 868 |
% \textbf{This function is experimental.} |
| 869 |
% \end{function} |
| 870 |
% |
| 871 |
% \subsection{Fully-expandable document commands} |
| 872 |
% |
| 873 |
% There are \emph{very rare} occasion when it may be useful to create |
| 874 |
% functions using a fully-expandable argument grabber. To support this, |
| 875 |
% \pkg{xparse} can create expandable functions as well as the usual |
| 876 |
% robust ones. This imposes a number of restrictions on the nature of |
| 877 |
% the arguments accepted by a function, and the code it implements. |
| 878 |
% This facility should only be used when \emph{absolutely necessary}; |
| 879 |
% if you do not understand when this might be, \emph{do not use these |
| 880 |
% functions}! |
| 881 |
% |
| 882 |
% \begin{function} |
| 883 |
% { |
| 884 |
% \NewExpandableDocumentCommand , |
| 885 |
% \RenewExpandableDocumentCommand , |
| 886 |
% \ProvideExpandableDocumentCommand , |
| 887 |
% \DeclareExpandableDocumentCommand |
| 888 |
% } |
| 889 |
% \begin{syntax} |
| 890 |
% \cs{NewExpandableDocumentCommand} |
| 891 |
% ~~~~\meta{function} \Arg{arg spec} \Arg{code} |
| 892 |
% \end{syntax} |
| 893 |
% This family of commands is used to create a document-level \meta{function}, |
| 894 |
% which will grab its arguments in a fully-expandable manner. The |
| 895 |
% argument specification for the function is given by \meta{arg spec}, |
| 896 |
% and the function will execute \meta{code}. In general, \meta{code} will |
| 897 |
% also be fully expandable, although it is possible that this will |
| 898 |
% not be the case (for example, a function for use in a table might |
| 899 |
% expand so that \cs{omit} is the first non-expandable non-space token). |
| 900 |
% |
| 901 |
% Parsing arguments expandably imposes a number of restrictions on |
| 902 |
% both the type of arguments that can be read and the error checking |
| 903 |
% available: |
| 904 |
% \begin{itemize} |
| 905 |
% \item The last argument (if any are present) must be one of the |
| 906 |
% mandatory types \texttt{m}, \texttt{r}, \texttt{R}, \texttt{l} |
| 907 |
% or~\texttt{u}. |
| 908 |
% \item All short arguments appear before long arguments. |
| 909 |
% \item The mandatory argument types \texttt{l} and \texttt{u} may |
| 910 |
% not be used after optional arguments. |
| 911 |
% \item The optional argument types \texttt{g} |
| 912 |
% and \texttt{G} are not available. |
| 913 |
% \item The \enquote{verbatim} argument type \texttt{v} is not available. |
| 914 |
% \item Argument processors (using \texttt{>}) are not available. |
| 915 |
% \item It is not possible to differentiate between, for example |
| 916 |
% |\foo[| and |\foo{[}|: in both cases the \texttt{[} will be |
| 917 |
% interpreted as the start of an optional argument. As a |
| 918 |
% result, checking for optional arguments is less robust than |
| 919 |
% in the standard version. |
| 920 |
% \end{itemize} |
| 921 |
% \pkg{xparse} will issue an error if an argument specifier is given |
| 922 |
% which does not conform to the first six requirements. The last |
| 923 |
% item is an issue when the function is used, and so is beyond the |
| 924 |
% scope of \pkg{xparse} itself. |
| 925 |
% \end{function} |
| 926 |
% |
| 927 |
% \subsection{Access to the argument specification} |
| 928 |
% |
| 929 |
% The argument specifications for document commands and environments are |
| 930 |
% available for examination and use. |
| 931 |
% |
| 932 |
% \begin{function}{\GetDocumentCommandArgSpec, \GetDocumentEnvironmentArgSpec} |
| 933 |
% \begin{syntax} |
| 934 |
% \cs{GetDocumentCommandArgSpec} \meta{function} |
| 935 |
% \cs{GetDocumentEnvironmentArgSpec} \Arg{environment} |
| 936 |
% \end{syntax} |
| 937 |
% These functions transfer the current argument specification for the |
| 938 |
% requested \meta{function} or \meta{environment} into the token list |
| 939 |
% variable \cs{ArgumentSpecification}. If the \meta{function} or |
| 940 |
% \meta{environment} has no known argument specification then an error |
| 941 |
% is issued. The assignment to \cs{ArgumentSpecification} is local to |
| 942 |
% the current \TeX{} group. |
| 943 |
% \end{function} |
| 944 |
% |
| 945 |
% \begin{function} |
| 946 |
% {\ShowDocumentCommandArgSpec, \ShowDocumentEnvironmentArgSpec} |
| 947 |
% \begin{syntax} |
| 948 |
% \cs{ShowDocumentCommandArgSpec} \meta{function} |
| 949 |
% \cs{ShowDocumentEnvironmentArgSpec} \Arg{environment} |
| 950 |
% \end{syntax} |
| 951 |
% These functions show the current argument specification for the |
| 952 |
% requested \meta{function} or \meta{environment} at the terminal. If |
| 953 |
% the \meta{function} or \meta{environment} has no known argument |
| 954 |
% specification then an error is issued. |
| 955 |
% \end{function} |
| 956 |
% |
| 957 |
% \section{Load-time options} |
| 958 |
% |
| 959 |
% \DescribeOption{log-declarations} |
| 960 |
% The package recognizes the load-time option \texttt{log-declarations}, |
| 961 |
% which is a key--value option taking the value \texttt{true} and |
| 962 |
% \texttt{false}. By default, the option is set to \texttt{false}, meaning |
| 963 |
% that no command or environment declared is logged. By loading |
| 964 |
% \pkg{xparse} using |
| 965 |
% \begin{verbatim} |
| 966 |
% \usepackage[log-declarations=true]{xparse} |
| 967 |
% \end{verbatim} |
| 968 |
% each new, declared or renewed command or environment is logged. |
| 969 |
% |
| 970 |
% \end{documentation} |
| 971 |
% |
| 972 |
% \begin{implementation} |
| 973 |
% |
| 974 |
% \section{\pkg{xparse} implementation} |
| 975 |
% |
| 976 |
% \begin{macrocode} |
| 977 |
%<*2ekernel|package> |
| 978 |
% \end{macrocode} |
| 979 |
% |
| 980 |
% \begin{macrocode} |
| 981 |
%<@@=cmd> |
| 982 |
% \end{macrocode} |
| 983 |
% |
| 984 |
% This package file, along with the frozen versions in |
| 985 |
% \pkg{xparse-2018-04-12}, \pkg{xparse-2020-10-01}, and |
| 986 |
% |xparse-generic.tex|, is intended to work across different \LaTeX{} |
| 987 |
% releases, respected the minimum \pkg{expl3} version required by |
| 988 |
% \pkg{xparse}. |
| 989 |
% |
| 990 |
% We can't use the \pkg{latexrelease} mechanism with \cs{DeclareRelease} |
| 991 |
% here because cases of rolling forwards or backwards differ, due to the |
| 992 |
% prefix change from |xparse| to |cmd|, so we do our own checks to |
| 993 |
% ensure the right version is loaded. |
| 994 |
% |
| 995 |
% All this loading assumes that if \pkg{latexrelease} should be loaded, |
| 996 |
% it is before \pkg{xparse}. The other way around was not tested at all |
| 997 |
% and is likely to break. |
| 998 |
% |
| 999 |
% In releases prior to 2020-10-01, \cs{NewDocumentCommand} is undefined, |
| 1000 |
% so \pkg{xparse} was not loaded, thus we can load the full version from |
| 1001 |
% \pkg{xparse-2018-04-12}. Otherwise we will load only the deprecated |
| 1002 |
% argument types from \pkg{xparse-2020-10-01}. If we're in |2ekernel| |
| 1003 |
% mode, it means rolling forward so load only the code code from |
| 1004 |
% |xparse-generic.tex|. |
| 1005 |
% |
| 1006 |
% In |2ekernel| mode we anticipate the definition of two macros to parse |
| 1007 |
% dates so that we can compare \cs{fmtdate} properly. |
| 1008 |
% \begin{macrocode} |
| 1009 |
%<*2ekernel> |
| 1010 |
\def\@parse@version#1/#2/#3#4#5\@nil{% |
| 1011 |
\@parse@version@dash#1-#2-#3#4\@nil} |
| 1012 |
\def\@parse@version@dash#1-#2-#3#4#5\@nil{% |
| 1013 |
\if\relax#2\relax\else#1\fi#2#3#4 } |
| 1014 |
%</2ekernel> |
| 1015 |
\ExplSyntaxOn |
| 1016 |
\cs_set_protected:Npn \@@_tmp:w #1 |
| 1017 |
{ |
| 1018 |
\DeclareOption* { \PassOptionsToPackage { \CurrentOption } {#1} } |
| 1019 |
\ProcessOptions \relax |
| 1020 |
\RequirePackage {#1} |
| 1021 |
} |
| 1022 |
\cs_if_free:NTF \NewDocumentCommand |
| 1023 |
{ |
| 1024 |
\ExplSyntaxOff |
| 1025 |
\ifnum\expandafter |
| 1026 |
\@parse@version\fmtversion//00\@nil < |
| 1027 |
\@parse@version 2020-10-01//00\@nil |
| 1028 |
\@@_tmp:w { xparse-2018-04-12 } |
| 1029 |
\else |
| 1030 |
%<2ekernel> \@@@@input xparse-generic.tex ~ |
| 1031 |
%<package> \@@_tmp:w { xparse-2020-10-01 } |
| 1032 |
\fi |
| 1033 |
\file_input_stop: |
| 1034 |
} |
| 1035 |
% \end{macrocode} |
| 1036 |
% |
| 1037 |
% In case \cs{NewDocumentCommand} is already defined, we're either in |
| 1038 |
% \LaTeX{} 2020-10-01 or later. In the former case, the code loaded in |
| 1039 |
% the kernel has the |__xparse| prefix, so we'll load |
| 1040 |
% \pkg{xparse-2020-10-01}, otherwise we'll continue in |xparse.sty|, |
| 1041 |
% which contains the final remains of \pkg{xparse} with the |__cmd| |
| 1042 |
% prefix. To check that we simply look at an internal command with the |
| 1043 |
% |__cmd| prefix. |
| 1044 |
% \begin{macrocode} |
| 1045 |
{ |
| 1046 |
\ExplSyntaxOff |
| 1047 |
\cs_if_exist:NF \@@_start:nNNnnn |
| 1048 |
{ |
| 1049 |
\@@_tmp:w { xparse-2020-10-01 } |
| 1050 |
\file_input_stop: |
| 1051 |
} |
| 1052 |
} |
| 1053 |
\ExplSyntaxOff |
| 1054 |
% \end{macrocode} |
| 1055 |
% |
| 1056 |
% \begin{macrocode} |
| 1057 |
%</2ekernel|package> |
| 1058 |
% \end{macrocode} |
| 1059 |
% |
| 1060 |
% In older releases, the prefix was |xparse|, but since the 2021 Spring |
| 1061 |
% release of \LaTeXe, the core code is included in the kernel, and this |
| 1062 |
% file only holds the deprecated argument specifiers |G|, |l|, and |u|. |
| 1063 |
% To match the prefix in the \LaTeXe{} kernel, so that the deprecated |
| 1064 |
% types work if |xparse.sty| is loaded, the prefix has changed to |cmd|. |
| 1065 |
% |
| 1066 |
% \begin{macrocode} |
| 1067 |
%<*package> |
| 1068 |
% \end{macrocode} |
| 1069 |
% |
| 1070 |
% \begin{macrocode} |
| 1071 |
\ProvidesExplPackage{xparse}{2025-10-09}{} |
| 1072 |
{L3 Experimental document command parser} |
| 1073 |
% \end{macrocode} |
| 1074 |
% |
| 1075 |
% \subsection{Package options} |
| 1076 |
% |
| 1077 |
% \begin{variable}{\l_@@_options_clist} |
| 1078 |
% \begin{variable}{\l_@@_log_bool} |
| 1079 |
% Key--value option to log information: done by hand to keep dependencies |
| 1080 |
% down. |
| 1081 |
% \begin{macrocode} |
| 1082 |
\clist_new:N \l_@@_options_clist |
| 1083 |
\DeclareOption* { \clist_put_right:NV \l_@@_options_clist \CurrentOption } |
| 1084 |
\ProcessOptions \relax |
| 1085 |
\cs_set_protected:Npn \@@_tmp:w #1 |
| 1086 |
{ |
| 1087 |
\keys_define:nn {#1} |
| 1088 |
{ |
| 1089 |
log-declarations .bool_set:N = \l_@@_log_bool , |
| 1090 |
log-declarations .initial:n = false |
| 1091 |
} |
| 1092 |
\keys_set:nV {#1} \l_@@_options_clist |
| 1093 |
\bool_if:NTF \l_@@_log_bool |
| 1094 |
{ \msg_redirect_module:nnn {#1} { info } { } } |
| 1095 |
{ \msg_redirect_module:nnn {#1} { info } { none } } |
| 1096 |
\cs_new_protected:Npn \@@_unknown_argument_type_error:n ##1 |
| 1097 |
{ |
| 1098 |
\msg_error:nnee {#1} { unknown-argument-type } |
| 1099 |
{ \@@_environment_or_command: } { \tl_to_str:n {##1} } |
| 1100 |
} |
| 1101 |
} |
| 1102 |
\msg_if_exist:nnTF { cmd } { define-command } |
| 1103 |
{ \@@_tmp:w { cmd } } |
| 1104 |
{ \@@_tmp:w { ltcmd } } |
| 1105 |
% \end{macrocode} |
| 1106 |
% \end{variable} |
| 1107 |
% \end{variable} |
| 1108 |
% |
| 1109 |
% \subsection{Normalizing the argument specifications} |
| 1110 |
% |
| 1111 |
% \begin{macro}{\@@_normalize_arg_spec_loop:n} |
| 1112 |
% Loop through the argument specification, calling an auxiliary |
| 1113 |
% specific to each argument type. If any argument is unknown stop the |
| 1114 |
% definition. |
| 1115 |
% \begin{macrocode} |
| 1116 |
\cs_gset_protected:Npn \@@_normalize_arg_spec_loop:n #1 |
| 1117 |
{ |
| 1118 |
\quark_if_recursion_tail_stop:n {#1} |
| 1119 |
\int_incr:N \l_@@_current_arg_int |
| 1120 |
\cs_if_exist_use:cF { @@_normalize_type_ \tl_to_str:n {#1} :w } |
| 1121 |
{ |
| 1122 |
\@@_unknown_argument_type_error:n {#1} |
| 1123 |
\@@_bad_def:wn |
| 1124 |
} |
| 1125 |
} |
| 1126 |
% \end{macrocode} |
| 1127 |
% \end{macro} |
| 1128 |
% |
| 1129 |
% \begin{macro}{\@@_normalize_type_g:w} |
| 1130 |
% These argument types are aliases of more general ones, for example |
| 1131 |
% with the default argument |-NoValue-|. |
| 1132 |
% \begin{macrocode} |
| 1133 |
\cs_new_protected:Npe \@@_normalize_type_g:w |
| 1134 |
{ \exp_not:N \@@_normalize_type_G:w { \exp_not:V \c_novalue_tl } } |
| 1135 |
% \end{macrocode} |
| 1136 |
% \end{macro} |
| 1137 |
% |
| 1138 |
% \begin{macro}{\@@_normalize_type_G:w} |
| 1139 |
% Optional argument types. Check that all required data is present |
| 1140 |
% (and consists of single characters if applicable) and check for |
| 1141 |
% forbidden types for expandable commands. Then in each case |
| 1142 |
% store the data in \cs{l_@@_arg_spec_tl}, and |
| 1143 |
% for later checks store in \cs{l_@@_last_delimiters_tl} the tokens |
| 1144 |
% whose presence determines whether there is an optional argument (for |
| 1145 |
% braces store |{}|, seen later as an empty delimiter). |
| 1146 |
% \begin{macrocode} |
| 1147 |
\cs_new_protected:Npn \@@_normalize_type_G:w #1 |
| 1148 |
{ |
| 1149 |
\quark_if_recursion_tail_stop_do:nn {#1} { \@@_bad_arg_spec:wn } |
| 1150 |
\@@_normalize_check_gv:N G |
| 1151 |
\@@_add_arg_spec:n { G {#1} } |
| 1152 |
\tl_put_right:Nn \l_@@_last_delimiters_tl { { } } |
| 1153 |
\@@_normalize_arg_spec_loop:n |
| 1154 |
} |
| 1155 |
% \end{macrocode} |
| 1156 |
% \end{macro} |
| 1157 |
% |
| 1158 |
% \begin{macro}{\@@_normalize_type_l:w,\@@_normalize_type_u:w} |
| 1159 |
% Mandatory arguments. First check the required data is present, |
| 1160 |
% consists of single characters where applicable, and that the argument |
| 1161 |
% type is allowed for expandable commands if applicable. Then save data in |
| 1162 |
% \cs{l_@@_arg_spec_tl}, count the mandatory argument, and empty the |
| 1163 |
% list of last delimiters. |
| 1164 |
% \begin{macrocode} |
| 1165 |
\cs_new_protected:Npn \@@_normalize_type_l:w |
| 1166 |
{ |
| 1167 |
\@@_normalize_check_lu:N l |
| 1168 |
\@@_add_arg_spec_mandatory:n { l } |
| 1169 |
\@@_normalize_arg_spec_loop:n |
| 1170 |
} |
| 1171 |
\cs_new_protected:Npn \@@_normalize_type_u:w #1 |
| 1172 |
{ |
| 1173 |
\quark_if_recursion_tail_stop_do:nn {#1} { \@@_bad_arg_spec:wn } |
| 1174 |
\@@_normalize_check_lu:N u |
| 1175 |
\@@_add_arg_spec_mandatory:n { u {#1} } |
| 1176 |
\@@_normalize_arg_spec_loop:n |
| 1177 |
} |
| 1178 |
% \end{macrocode} |
| 1179 |
% \end{macro} |
| 1180 |
% |
| 1181 |
% \subsection{Setting up a standard signature} |
| 1182 |
% |
| 1183 |
% \begin{macro}{\@@_add_type_G:w} |
| 1184 |
% For the \texttt{G} type, the grabber and the default are added to the |
| 1185 |
% signature. |
| 1186 |
% \begin{macrocode} |
| 1187 |
\cs_new_protected:Npn \@@_add_type_G:w #1 |
| 1188 |
{ |
| 1189 |
\@@_flush_m_args: |
| 1190 |
\@@_add_default:n {#1} |
| 1191 |
\@@_add_grabber:N G |
| 1192 |
\@@_prepare_signature:N |
| 1193 |
} |
| 1194 |
% \end{macrocode} |
| 1195 |
% \end{macro} |
| 1196 |
% |
| 1197 |
% \begin{macro}{\@@_add_type_l:w} |
| 1198 |
% Finding \texttt{l} arguments is very simple: there is nothing to do |
| 1199 |
% other than add the grabber. |
| 1200 |
% \begin{macrocode} |
| 1201 |
\cs_new_protected:Npn \@@_add_type_l:w |
| 1202 |
{ |
| 1203 |
\@@_flush_m_args: |
| 1204 |
\@@_add_default: |
| 1205 |
\@@_add_grabber:N l |
| 1206 |
\@@_prepare_signature:N |
| 1207 |
} |
| 1208 |
% \end{macrocode} |
| 1209 |
% \end{macro} |
| 1210 |
% |
| 1211 |
% \begin{macro}{\@@_add_type_u:w} |
| 1212 |
% At the set up stage, the \texttt{u} type argument is identical to the |
| 1213 |
% \texttt{G} type except for the name of the grabber function. |
| 1214 |
% \begin{macrocode} |
| 1215 |
\cs_new_protected:Npn \@@_add_type_u:w #1 |
| 1216 |
{ |
| 1217 |
\@@_flush_m_args: |
| 1218 |
\@@_add_default: |
| 1219 |
\@@_add_grabber:N u |
| 1220 |
\tl_put_right:Nn \l_@@_signature_tl { {#1} } |
| 1221 |
\@@_prepare_signature:N |
| 1222 |
} |
| 1223 |
% \end{macrocode} |
| 1224 |
% \end{macro} |
| 1225 |
% |
| 1226 |
% \subsection{Setting up expandable types} |
| 1227 |
% |
| 1228 |
% \begin{macro}{\@@_add_expandable_type_l:w} |
| 1229 |
% Reuse type \texttt{u}, thanks to the fact that \TeX{} macros whose |
| 1230 |
% parameter text ends with |#| in fact end up being delimited by an |
| 1231 |
% open brace. |
| 1232 |
% \begin{macrocode} |
| 1233 |
\cs_new_protected:Npn \@@_add_expandable_type_l:w |
| 1234 |
{ \@@_add_expandable_type_u:w ## } |
| 1235 |
% \end{macrocode} |
| 1236 |
% \end{macro} |
| 1237 |
% |
| 1238 |
% \begin{macro}{\@@_add_expandable_type_u:w} |
| 1239 |
% Define an auxiliary that will be used directly in the signature. It |
| 1240 |
% grabs one argument delimited by |#1| and places it before \cs{q_@@}. |
| 1241 |
% \begin{macrocode} |
| 1242 |
\cs_new_protected:Npn \@@_add_expandable_type_u:w #1 |
| 1243 |
{ |
| 1244 |
\@@_add_default: |
| 1245 |
\bool_if:NTF \l_@@_long_bool |
| 1246 |
{ \cs_set:cpn } |
| 1247 |
{ \cs_set_nopar:cpn } |
| 1248 |
{ \l_@@_expandable_aux_name_tl } ##1 \q_@@ ##2 ##3 ##4 #1 |
| 1249 |
{ ##1 {##4} \q_@@ ##2 ##3 } |
| 1250 |
\@@_add_expandable_grabber:nn { u } |
| 1251 |
{ \exp_not:c { \l_@@_expandable_aux_name_tl } } |
| 1252 |
\@@_prepare_signature:N |
| 1253 |
} |
| 1254 |
% \end{macrocode} |
| 1255 |
% \end{macro} |
| 1256 |
% |
| 1257 |
% \subsection{Copying a command and its internal structure} |
| 1258 |
% |
| 1259 |
% The apparatus for copying commands is almost entirely in |ltcmd.dtx|, |
| 1260 |
% preloaded in the \LaTeXe{} kernel. The missing parts, regarding |
| 1261 |
% copying the deprecated argument types |G|, |l| and |u| boil down to |
| 1262 |
% copying the expandable grabber for |u|. |
| 1263 |
% |
| 1264 |
% \begin{macro}{\@@_copy_grabber_u:w} |
| 1265 |
% An expandable |u|-type uses a dedicated grabber just like a |
| 1266 |
% |D|-type, except that both its delimiter tokens are omitted, so to |
| 1267 |
% copy that we just copy a |D|-type and leave the last two arguments |
| 1268 |
% empty: |
| 1269 |
% \begin{macrocode} |
| 1270 |
\cs_new_protected:Npn \@@_copy_grabber_u:w #1 #2 #3 |
| 1271 |
{ \@@_copy_grabber_D:w {#1} {#2} {#3} { } { } } |
| 1272 |
% \end{macrocode} |
| 1273 |
% \end{macro} |
| 1274 |
% |
| 1275 |
% \subsubsection{Showing the definition of a command} |
| 1276 |
% |
| 1277 |
% \begin{macro}{\c_@@_show_type_u_tl,\c_@@_show_type_G_tl} |
| 1278 |
% Same as for copying, only small bits are missing here. Namely, two |
| 1279 |
% token lists that tell the \pkg{ltcmd} mechanism how to deal with the |
| 1280 |
% argument types defined here. Both |G| and |u| are classified |
| 1281 |
% as~|3|: commands that take a default value. That's not really true |
| 1282 |
% for~|u|, but it's a close enough approximation to get the output we |
| 1283 |
% want. |
| 1284 |
% \begin{macrocode} |
| 1285 |
\tl_const:Nn \c_@@_show_type_u_tl { 3 } |
| 1286 |
\tl_const:Nn \c_@@_show_type_G_tl { 3 } |
| 1287 |
% \end{macrocode} |
| 1288 |
% \end{macro} |
| 1289 |
% |
| 1290 |
% \subsection{Grabbing arguments} |
| 1291 |
% |
| 1292 |
% \begin{macro}{\@@_grab_G:w} |
| 1293 |
% \begin{macro}{\@@_grab_G_long:w} |
| 1294 |
% \begin{macro}{\@@_grab_G_obey_spaces:w} |
| 1295 |
% \begin{macro}{\@@_grab_G_long_obey_spaces:w} |
| 1296 |
% \begin{macro}{\@@_grab_G_aux:nNN} |
| 1297 |
% Optional groups are checked by meaning, so that the same code will |
| 1298 |
% work with, for example, \ConTeXt{}-like input. |
| 1299 |
% \begin{macrocode} |
| 1300 |
\cs_new_protected:Npn \@@_grab_G:w #1 \@@_run_code: |
| 1301 |
{ |
| 1302 |
\@@_grab_G_aux:nNN {#1} \cs_set_protected_nopar:Npn |
| 1303 |
\@@_peek_nonspace:NTF |
| 1304 |
} |
| 1305 |
\cs_new_protected:Npn \@@_grab_G_long:w #1 \@@_run_code: |
| 1306 |
{ |
| 1307 |
\@@_grab_G_aux:nNN {#1} \cs_set_protected:Npn |
| 1308 |
\@@_peek_nonspace:NTF |
| 1309 |
} |
| 1310 |
\cs_new_protected:Npn \@@_grab_G_obey_spaces:w #1 \@@_run_code: |
| 1311 |
{ |
| 1312 |
\@@_grab_G_aux:nNN {#1} \cs_set_protected_nopar:Npn |
| 1313 |
\peek_meaning:NTF |
| 1314 |
} |
| 1315 |
\cs_new_protected:Npn \@@_grab_G_long_obey_spaces:w #1 \@@_run_code: |
| 1316 |
{ |
| 1317 |
\@@_grab_G_aux:nNN {#1} \cs_set_protected:Npn |
| 1318 |
\peek_meaning:NTF |
| 1319 |
} |
| 1320 |
\cs_new_protected:Npn \@@_grab_G_aux:nNN #1#2#3 |
| 1321 |
{ |
| 1322 |
\tl_set:Nn \l_@@_signature_tl {#1} |
| 1323 |
\exp_after:wN #2 \l_@@_fn_tl ##1 |
| 1324 |
{ \@@_add_arg:n {##1} } |
| 1325 |
#3 \c_group_begin_token |
| 1326 |
{ \l_@@_fn_tl } |
| 1327 |
{ \@@_add_arg:o \c_novalue_tl } |
| 1328 |
} |
| 1329 |
% \end{macrocode} |
| 1330 |
% \end{macro} |
| 1331 |
% \end{macro} |
| 1332 |
% \end{macro} |
| 1333 |
% \end{macro} |
| 1334 |
% \end{macro} |
| 1335 |
% |
| 1336 |
% \begin{macro}{\@@_grab_l:w} |
| 1337 |
% \begin{macro}{\@@_grab_l_long:w} |
| 1338 |
% \begin{macro}{\@@_grab_l_aux:nN} |
| 1339 |
% Argument grabbers for mandatory \TeX{} arguments are pretty simple. |
| 1340 |
% \begin{macrocode} |
| 1341 |
\cs_new_protected:Npn \@@_grab_l:w #1 \@@_run_code: |
| 1342 |
{ \@@_grab_l_aux:nN {#1} \cs_set_protected_nopar:Npn } |
| 1343 |
\cs_new_protected:Npn \@@_grab_l_long:w #1 \@@_run_code: |
| 1344 |
{ \@@_grab_l_aux:nN {#1} \cs_set_protected:Npn } |
| 1345 |
\cs_new_protected:Npn \@@_grab_l_aux:nN #1#2 |
| 1346 |
{ |
| 1347 |
\tl_set:Nn \l_@@_signature_tl {#1} |
| 1348 |
\exp_after:wN #2 \l_@@_fn_tl ##1## |
| 1349 |
{ \@@_add_arg:n {##1} } |
| 1350 |
\l_@@_fn_tl |
| 1351 |
} |
| 1352 |
% \end{macrocode} |
| 1353 |
% \end{macro} |
| 1354 |
% \end{macro} |
| 1355 |
% \end{macro} |
| 1356 |
% |
| 1357 |
% \begin{macro}{\@@_grab_u:w} |
| 1358 |
% \begin{macro}{\@@_grab_u_long:w} |
| 1359 |
% \begin{macro}{\@@_grab_u_aux:nnN} |
| 1360 |
% Grabbing up to a list of tokens is quite easy: define the grabber, |
| 1361 |
% and then collect. |
| 1362 |
% \begin{macrocode} |
| 1363 |
\cs_new_protected:Npn \@@_grab_u:w #1#2 \@@_run_code: |
| 1364 |
{ \@@_grab_u_aux:nnN {#1} {#2} \cs_set_protected_nopar:Npn } |
| 1365 |
\cs_new_protected:Npn \@@_grab_u_long:w #1#2 \@@_run_code: |
| 1366 |
{ \@@_grab_u_aux:nnN {#1} {#2} \cs_set_protected:Npn } |
| 1367 |
\cs_new_protected:Npn \@@_grab_u_aux:nnN #1#2#3 |
| 1368 |
{ |
| 1369 |
\tl_set:Nn \l_@@_signature_tl {#2} |
| 1370 |
\exp_after:wN #3 \l_@@_fn_tl ##1 #1 |
| 1371 |
{ \@@_add_arg:n {##1} } |
| 1372 |
\l_@@_fn_tl |
| 1373 |
} |
| 1374 |
% \end{macrocode} |
| 1375 |
% \end{macro} |
| 1376 |
% \end{macro} |
| 1377 |
% \end{macro} |
| 1378 |
% |
| 1379 |
% \begin{macro}[EXP]{\@@_expandable_grab_u:w} |
| 1380 |
% It turns out there is nothing to do: this is followed by an |
| 1381 |
% auxiliary named after the function, that does everything. |
| 1382 |
% \begin{macrocode} |
| 1383 |
\cs_new_eq:NN \@@_expandable_grab_u:w \prg_do_nothing: |
| 1384 |
% \end{macrocode} |
| 1385 |
% \end{macro} |
| 1386 |
% |
| 1387 |
% \subsection{Access to the argument specification} |
| 1388 |
% |
| 1389 |
% At one point this was included in the kernel, so we have to use |
| 1390 |
% \texttt{gset} (or similar) throughout this subsection. |
| 1391 |
% |
| 1392 |
% \begin{macro}{\@@_get_arg_spec_error:N, \@@_get_arg_spec_error:n} |
| 1393 |
% \begin{macro}{\@@_get_arg_spec_error_aux:n} |
| 1394 |
% Provide an informative error when trying to get the argument |
| 1395 |
% specification of a non-\pkg{xparse} command or environment. |
| 1396 |
% \begin{macrocode} |
| 1397 |
\cs_gset_protected:Npn \@@_get_arg_spec_error:N #1 |
| 1398 |
{ |
| 1399 |
\bool_set_false:N \l_@@_environment_bool |
| 1400 |
\tl_set:Nn \l_@@_fn_tl {#1} |
| 1401 |
\@@_get_arg_spec_error_aux:n { \cs_if_exist:NTF #1 } |
| 1402 |
} |
| 1403 |
\cs_gset_protected:Npn \@@_get_arg_spec_error:n #1 |
| 1404 |
{ |
| 1405 |
\bool_set_true:N \l_@@_environment_bool |
| 1406 |
\str_set:Ne \l_@@_environment_str {#1} |
| 1407 |
\@@_get_arg_spec_error_aux:n |
| 1408 |
{ \cs_if_exist:cTF { \l_@@_environment_str } } |
| 1409 |
} |
| 1410 |
\cs_gset_protected:Npn \@@_get_arg_spec_error_aux:n #1 |
| 1411 |
{ |
| 1412 |
#1 |
| 1413 |
{ |
| 1414 |
\msg_error:nne { cmd } { non-xparse } |
| 1415 |
{ \@@_environment_or_command: } |
| 1416 |
} |
| 1417 |
{ |
| 1418 |
\msg_error:nne { cmd } { unknown } |
| 1419 |
{ \@@_environment_or_command: } |
| 1420 |
} |
| 1421 |
} |
| 1422 |
% \end{macrocode} |
| 1423 |
% \end{macro} |
| 1424 |
% \end{macro} |
| 1425 |
% |
| 1426 |
% \begin{macro}{\@@_get_arg_spec:NTF} |
| 1427 |
% If the command is not an \pkg{xparse} command, complain. If it is, |
| 1428 |
% its second \enquote{item} is the argument specification unless the |
| 1429 |
% command is optimized. In the latter case, we can reconstruct the |
| 1430 |
% spec: we figure out the number of arguments to yield the appropriate |
| 1431 |
% number of |m|s, and take care to prefix each |m| with a |+| if the |
| 1432 |
% command is \cs{long}. |
| 1433 |
% \begin{macrocode} |
| 1434 |
\cs_gset_protected:Npn \@@_get_arg_spec:NTF #1#2#3 |
| 1435 |
{ |
| 1436 |
\__kernel_cmd_if_xparse:NTF #1 |
| 1437 |
{ |
| 1438 |
\tl_set:Ne \ArgumentSpecification |
| 1439 |
{ |
| 1440 |
\exp_args:No \tl_if_head_eq_meaning:nNTF {#1} \@@_start_optimized: |
| 1441 |
{ |
| 1442 |
\prg_replicate:nn |
| 1443 |
{ |
| 1444 |
\str_count:e |
| 1445 |
{ |
| 1446 |
\exp_args:Nc \cs_parameter_spec:N |
| 1447 |
{ \cs_to_str:N #1 \c_space_tl code } |
| 1448 |
} / 2 |
| 1449 |
} |
| 1450 |
{ |
| 1451 |
\exp_args:Nc \token_if_protected_long_macro:NTF |
| 1452 |
{ \cs_to_str:N #1 \c_space_tl code } |
| 1453 |
{ + } |
| 1454 |
{ |
| 1455 |
\exp_args:Nc \token_if_long_macro:NT |
| 1456 |
{ \cs_to_str:N #1 \c_space_tl code } |
| 1457 |
{ + } |
| 1458 |
} |
| 1459 |
m |
| 1460 |
} |
| 1461 |
} |
| 1462 |
{ \tl_item:Nn #1 { 2 } } |
| 1463 |
} |
| 1464 |
#2 |
| 1465 |
} |
| 1466 |
{#3} |
| 1467 |
} |
| 1468 |
% \end{macrocode} |
| 1469 |
% \end{macro} |
| 1470 |
% |
| 1471 |
% \begin{variable}{\ArgumentSpecification} |
| 1472 |
% \begin{macrocode} |
| 1473 |
\tl_clear_new:N \ArgumentSpecification |
| 1474 |
% \end{macrocode} |
| 1475 |
% \end{variable} |
| 1476 |
% |
| 1477 |
% \begin{macro}{\@@_get_arg_spec:N} |
| 1478 |
% \begin{macro}{\@@_get_arg_spec:n} |
| 1479 |
% Recovering the argument specification is now trivial. |
| 1480 |
% \begin{macrocode} |
| 1481 |
\cs_gset_protected:Npn \@@_get_arg_spec:N #1 |
| 1482 |
{ |
| 1483 |
\@@_get_arg_spec:NTF #1 { } |
| 1484 |
{ \@@_get_arg_spec_error:N #1 } |
| 1485 |
} |
| 1486 |
\cs_gset_protected:Npn \@@_get_arg_spec:n #1 |
| 1487 |
{ |
| 1488 |
\exp_args:Nc \@@_get_arg_spec:NTF |
| 1489 |
{ environment~ \tl_to_str:n {#1} } |
| 1490 |
{ } |
| 1491 |
{ \@@_get_arg_spec_error:n {#1} } |
| 1492 |
} |
| 1493 |
% \end{macrocode} |
| 1494 |
% \end{macro} |
| 1495 |
% \end{macro} |
| 1496 |
% |
| 1497 |
% \begin{macro}{\@@_show_arg_spec:N} |
| 1498 |
% \begin{macro}{\@@_show_arg_spec:n} |
| 1499 |
% Showing the argument specification simply means finding it and then |
| 1500 |
% calling the \cs{tl_show:N} function. |
| 1501 |
% \begin{macrocode} |
| 1502 |
\cs_gset_protected:Npn \@@_show_arg_spec:N #1 |
| 1503 |
{ |
| 1504 |
\@@_get_arg_spec:NTF #1 |
| 1505 |
{ \tl_show:N \ArgumentSpecification } |
| 1506 |
{ \@@_get_arg_spec_error:N #1 } |
| 1507 |
} |
| 1508 |
\cs_gset_protected:Npn \@@_show_arg_spec:n #1 |
| 1509 |
{ |
| 1510 |
\exp_args:Nc \@@_get_arg_spec:NTF |
| 1511 |
{ environment~ \tl_to_str:n {#1} } |
| 1512 |
{ \tl_show:N \ArgumentSpecification } |
| 1513 |
{ \@@_get_arg_spec_error:n {#1} } |
| 1514 |
} |
| 1515 |
% \end{macrocode} |
| 1516 |
% \end{macro} |
| 1517 |
% \end{macro} |
| 1518 |
% |
| 1519 |
% \begin{macro}{\GetDocumentCommandArgSpec} |
| 1520 |
% \begin{macro}{\GetDocumentEnvironmentArgSpec} |
| 1521 |
% \begin{macro}{\ShowDocumentCommandArgSpec} |
| 1522 |
% \begin{macro}{\ShowDocumentEnvironmentArgSpec} |
| 1523 |
% Simple mappings, with a check that the argument is a single |
| 1524 |
% control sequence or active character. |
| 1525 |
% \begin{macrocode} |
| 1526 |
\cs_gset_protected:Npn \GetDocumentCommandArgSpec #1 |
| 1527 |
{ |
| 1528 |
\@@_check_definable:nNT {#1} \GetDocumentCommandArgSpec |
| 1529 |
{ \@@_get_arg_spec:N #1 } |
| 1530 |
} |
| 1531 |
\cs_gset_eq:NN \GetDocumentEnvironmentArgSpec \@@_get_arg_spec:n |
| 1532 |
\cs_gset_protected:Npn \ShowDocumentCommandArgSpec #1 |
| 1533 |
{ |
| 1534 |
\@@_check_definable:nNT {#1} \ShowDocumentCommandArgSpec |
| 1535 |
{ \@@_show_arg_spec:N #1 } |
| 1536 |
} |
| 1537 |
\cs_gset_eq:NN \ShowDocumentEnvironmentArgSpec \@@_show_arg_spec:n |
| 1538 |
% \end{macrocode} |
| 1539 |
% \end{macro} |
| 1540 |
% \end{macro} |
| 1541 |
% \end{macro} |
| 1542 |
% \end{macro} |
| 1543 |
% |
| 1544 |
% \begin{macrocode} |
| 1545 |
\msg_set:nnnn { cmd } { non-xparse } |
| 1546 |
{ \str_uppercase:n #1~not~defined~using~xparse. } |
| 1547 |
{ |
| 1548 |
You~have~asked~for~the~argument~specification~for~the~#1,~ |
| 1549 |
but~this~was~not~defined~using~xparse. |
| 1550 |
} |
| 1551 |
\msg_set:nnnn { cmd } { unknown } |
| 1552 |
{ Unknown~document~#1. } |
| 1553 |
{ |
| 1554 |
You~have~asked~for~the~argument~specification~for~the~#1,~ |
| 1555 |
but~it~is~not~defined. |
| 1556 |
} |
| 1557 |
% \end{macrocode} |
| 1558 |
% |
| 1559 |
% \begin{macrocode} |
| 1560 |
%</package> |
| 1561 |
% \end{macrocode} |
| 1562 |
% |
| 1563 |
% \end{implementation} |
| 1564 |
% |
| 1565 |
% \PrintIndex |