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 | ||
2_programmation:macros:cette_commande_est_elle_definie [2018/06/03 13:15] joseph.wright |
2_programmation:macros:cette_commande_est_elle_definie [2021/09/27 21:41] bdumont Traduction et remaniement pour mettre les solutions modernes et fiables en premier |
||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | --- | + | ====== Comment tester si une commande est définie? ====== |
- | title: Is this command defined? | + | |
- | category: programming | + | |
- | tags: macros | + | |
- | permalink: / | + | |
- | --- | + | |
- | Macro sets from the earliest days of TeX programming may be | + | ===== En TeX ===== |
- | observed to test whether commands exist by using | + | |
- | `\ifx\< | + | |
- | (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 | + | Le programme < |
- | ```latex | + | |
- | \expandafter \ifx \csname cmd name\endcsname \relax | + | |
- | ``` | + | |
- | which relies on the way `\csname` works: if the command doesn' | + | |
- | exist, it simply creates it as an alias for `\relax`. | + | |
- | So: what is wrong with these techniques? | + | * '' |
+ | * '' | ||
- | Using `\undefined` blithely assumes that the command is indeed not | + | Les deux commandes utilisées dans l'exemple qui suit produisent le même effet : |
- | defined. | + | |
- | improbable, but that may simply make it more difficult to spot a | + | |
- | problem when things go wrong. | + | |
- | technique will typically employ `\@undefined`, | + | |
- | level of obscurity. | + | |
- | The original `\@ifundefined` mechanism had the unfortunate property of | + | < |
- | polluting the name space: each test that turns out undefined adds a | + | |
- | name to the set TeX is holding, and often all those `\relax` | + | |
- | names serve no purpose whatever. | + | |
- | + | ||
- | David Kastrup offers the (rather tricky) | + | |
- | ```latex | + | |
- | {\expandafter}\expandafter\ifx \csname cmd name\endcsname\relax ... | + | |
- | ``` | + | |
- | which " | + | |
- | `\expandafter`, | + | |
- | The test is about as good as you can do with macros. | + | |
- | + | ||
- | The [& | + | |
- | defines two new primitives: | + | |
- | + | ||
- | + | ||
- | - `\ifdefined`, | + | |
- | negative of comparing with `\undefined`, | + | |
- | - `\ifcsname cmd name\endcsname`, | + | |
- | negative of `\@ifundefined` without the `\relax`-command | + | |
- | side-effect. | + | |
- | + | ||
- | So, in an & | + | |
- | the same thing: | + | |
- | ```latex | + | |
\ifdefined\foo | \ifdefined\foo | ||
\message{\string\foo\space is defined}% | \message{\string\foo\space is defined}% | ||
Ligne 69: | Ligne 22: | ||
\message{no command \string\foo}% | \message{no command \string\foo}% | ||
\fi | \fi | ||
- | ``` | + | </ |
- | However, after using the original LaTeX | + | |
- | `\@ifundefined{foo}`& | + | However, after using the original LaTeX '' |
- | command as " | + | the conditionals will detect the command as " |
- | so it is important not to mix mechanisms for detecting the state of a | + | (since it has been '' |
- | command. | + | not to mix mechanisms for detecting the state of a command. |
+ | |||
+ | ===== En LaTeX ===== | ||
+ | |||
+ | Quand on programme en < | ||
+ | '' | ||
+ | qui exécute ''<// | ||
+ | et ''<// | ||
+ | (''<// | ||
+ | |||
+ | Si vous utilisez une version de < | ||
+ | |||
+ | Notez également que, même après 2018, < | ||
+ | |||
+ | ===== Un peu d' | ||
+ | |||
+ | On trouve dans d' | ||
+ | pour tester l' | ||
+ | |||
+ | '' | ||
+ | |||
+ | (Ceci exécute le code si la commande **n' | ||
+ | |||
+ | Le fonctionnement de cette commande repose sur le principe que '' | ||
+ | |||
+ | La macro '' | ||
+ | |||
+ | <code latex> | ||
+ | \expandafter \ifx \csname cmd name\endcsname \relax | ||
+ | </ | ||
+ | |||
+ | Elle utilisait la propriété suivante de '' | ||
+ | si la commande n' | ||
+ | Cette approche présente deux inconvénients : | ||
+ | |||
+ | * Chaque utilisation de '' | ||
+ | * Si le même nom de commande est testé ensuite avec la primitive ε-TeX '' | ||
+ | |||
+ | Avant que '' | ||
+ | |||
+ | <code latex> | ||
+ | {\expandafter}\expandafter\ifx \csname cmd name\endcsname\relax ... | ||
+ | </ | ||
+ | |||
+ | La commande testée est créée et définie comme '' | ||
+ | '' | ||
+ | |||
+ | ----- | ||
+ | //Source:// [[faquk> | ||
- | In the 2016 LaTeX release, the definition of `\@ifundefined` was adapted | + | {{htmlmetatags> |
- | to use the & | + | metatag-og: |
- | without the side effect of defining undefined commands to `\relax`. | + | metatag-og: |
+ | }} | ||