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/05/25 08:55] joseph.wright |
2_programmation:macros:cette_commande_est_elle_definie [2021/01/07 18:23] jejust Traduction partielle en français. |
||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | --- | + | ====== Comment tester si une commande est définie ? ====== |
- | section: Macro programming | + | |
- | subsection: '' | + | |
- | permalink: / | + | |
- | date: 2014-06-10 | + | |
- | --- | + | |
- | # Is this command defined? | + | ===== Méthodes traditionnelles ===== |
- | Macro sets from the earliest days of TeX programming may be | + | ==== En TeX ==== |
- | observed to test whether commands exist by using | + | |
- | `\ifx` `\``_command_` `\undefined` & | + | |
- | (which of course actually tests that the command _doesn' | + | |
- | exist). | + | |
- | `ifundefined{cmd name}{action1}{action2}` | + | |
- | which executes `action1` if the command is undefined, and | + | |
- | `action2` if it is defined | + | |
- | (_cmd name_ is the command name only, omitting the `\` character). | + | |
- | The `ifundefined` command is based on the sequence | + | Voici ce qu'on trouve dans les anciennes macros écrites en < |
- | ```latex | + | pour tester l' |
+ | |||
+ | '' | ||
+ | |||
+ | (ceci exécute le code si la commande **n' | ||
+ | |||
+ | |||
+ | ==== En LaTeX ==== | ||
+ | |||
+ | Quand on programme en < | ||
+ | '' | ||
+ | qui exécute ''<// | ||
+ | et ''<// | ||
+ | (''<// | ||
+ | |||
+ | La macro '' | ||
+ | |||
+ | < | ||
\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't | + | qui repose sur le fonctionnement de '' |
- | exist, it simply creates it as an alias for `\relax`. | + | si la commande n' |
- | So: what is wrong with these techniques? | + | ===== Qu' |
- | 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 `ifundefined` mechanism | + | 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. Even so (sadly) there are places in | + | names serve no purpose whatever. |
- | the code of LaTeX where the existence of the `\relax` is relied | + | |
- | upon, after the test, so we can't get away from `ifundefined` | + | |
- | altogether. | + | |
- | 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 "creates" the '' |
- | `\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 & | + | 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 74: | Ligne 74: | ||
\message{no command \string\foo}% | \message{no command \string\foo}% | ||
\fi | \fi | ||
- | ``` | + | </ |
- | However, after using the LaTeX | + | However, after using the original |
- | `ifundefined{foo}`& | + | the conditionals will detect the command as "existing" |
- | command as '' | + | (since it has been '' |
- | so it is important not to mix mechanisms for detecting the state of a | + | not to mix mechanisms for detecting the state of a command. |
- | command. | + | |
+ | In the 2018 < | ||
+ | to use the $\epsilon$-TeX '' | ||
+ | without the side effect of defining undefined commands to '' | ||
- | Since most distributions nowadays use & | + | ----- |
- | for most packages, these two primitives may be expected appear widely | + | //Source:// [[faquk> |
- | in new macro packages. | + | |
+ | {{htmlmetatags> | ||
+ | metatag-og: | ||
+ | metatag-og: | ||
+ | }} | ||