Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente Prochaine révision Les deux révisions suivantes | ||
2_programmation:macros:cette_commande_est_elle_definie [2018/06/03 13:15] d.p.carlisle |
2_programmation:macros:cette_commande_est_elle_definie [2021/01/07 18:07] jejust Ancienne révision (2020/12/09 20:57) restaurée |
||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | --- | + | ====== |
- | title: | + | |
- | category: programming | + | |
- | tags: macros | + | |
- | permalink: / | + | |
- | --- | + | |
Macro sets from the earliest days of TeX programming may be | Macro sets from the earliest days of TeX programming may be | ||
observed to test whether commands exist by using | observed to test whether commands exist by using | ||
- | `\ifx\< | + | '' |
- | (which of course actually tests that the command | + | (which of course actually tests that the command |
exist). | exist). | ||
- | `\@ifundefined{cmd name}{action1}{action2}` | + | '' |
- | which executes | + | which executes |
- | `action2` if it is defined | + | '' |
- | (_cmd name_ is the command name only, omitting the `\` character). | + | (//cmd name// |
+ | |||
+ | The '' | ||
- | The `\@ifundefined` command is based on the sequence | + | < |
- | ```latex | + | |
\expandafter \ifx \csname cmd name\endcsname \relax | \expandafter \ifx \csname cmd name\endcsname \relax | ||
- | ``` | + | </ |
- | which relies on the way `\csname` works: if the command doesn' | + | which relies on the way '' |
- | exist, it simply creates it as an alias for `\relax`. | + | exist, it simply creates it as an alias for '' |
So: what is wrong with these techniques? | So: what is wrong with these techniques? | ||
- | Using `\undefined` blithely assumes that the command is indeed not | + | Using '' |
defined. | defined. | ||
improbable, but that may simply make it more difficult to spot a | improbable, but that may simply make it more difficult to spot a | ||
problem when things go wrong. | problem when things go wrong. | ||
- | technique will typically employ | + | technique will typically employ |
level of obscurity. | level of obscurity. | ||
- | The original | + | The original |
polluting the name space: each test that turns out undefined adds a | polluting the name space: each test that turns out undefined adds a | ||
- | name to the set TeX is holding, and often all those `\relax` | + | name to the set TeX is holding, and often all those '' |
names serve no purpose whatever. | names serve no purpose whatever. | ||
David Kastrup offers the (rather tricky) | David Kastrup offers the (rather tricky) | ||
- | ```latex | + | |
+ | < | ||
{\expandafter}\expandafter\ifx \csname cmd name\endcsname\relax ... | {\expandafter}\expandafter\ifx \csname cmd name\endcsname\relax ... | ||
- | ``` | + | </ |
- | which " | + | which " |
- | `\expandafter`, therefore forgets it again once the test is done. | + | '' |
The test is about as good as you can do with macros. | The test is about as good as you can do with macros. | ||
- | The [& | + | The [[FAQ-etex|ε-TeX system]] system comes to our help here: it |
defines two new primitives: | defines two new primitives: | ||
| | ||
- | - `\ifdefined`, which tests whether a thing is defined (the | + | * '' |
- | | + | |
- | - `\ifcsname cmd name\endcsname`, which does the | + | |
- | | + | |
- | | + | |
- | So, in an ε-TeX-based system, the following two conditional clauses do | + | So, in an < |
the same thing: | the same thing: | ||
- | ```latex | + | |
+ | < | ||
\ifdefined\foo | \ifdefined\foo | ||
\message{\string\foo\space is defined}% | \message{\string\foo\space is defined}% | ||
Ligne 69: | Ligne 65: | ||
\message{no command \string\foo}% | \message{no command \string\foo}% | ||
\fi | \fi | ||
- | ``` | + | </ |
However, after using the original LaTeX | However, after using the original LaTeX | ||
- | `\@ifundefined{foo}`& | + | '' |
- | command as " | + | command as " |
so it is important not to mix mechanisms for detecting the state of a | so it is important not to mix mechanisms for detecting the state of a | ||
command. | command. | ||
- | In the 2016 LaTeX release, the definition of `\@ifundefined` was adapted | + | In the 2018 < |
- | to use the ε-TeX `\ifcsname` and now tests for a command being undefined or `\relax` | + | to use the $\epsilon$-TeX '' |
- | without the side effect of defining undefined commands to `\relax`. | + | without the side effect of defining undefined commands to '' |
+ | |||
+ | ----- | ||
+ | //Source:// [[faquk> | ||
+ | |||
+ | {{htmlmetatags> | ||
+ | metatag-og: | ||
+ | metatag-og: | ||
+ | }} | ||