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:definir_une_macro_a_plus_de_9_arguments [2018/05/23 15:56] d.p.carlisle |
2_programmation:macros:definir_une_macro_a_plus_de_9_arguments [2021/10/21 14:01] bdumont [Avec l'extension keyval] Renvoi vers un nouvel article dédié à cette question |
||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | # How to break the 9-argument limit | + | ====== Comment dépasser la limite des 9 arguments pour une commande? ====== |
- | If you think about it, you will realise that Knuth's command | + | Si vous y réfléchissez, vous vous rendrez compte que la syntaxe de définition de commande dans <latex>\TeX</latex> (avec ''\def'') et <latex>\LaTeX</latex> (avec ''\newcommand'') a une conséquence importante : |
- | definition syntax: | + | |
- | ```latex | + | |
- | \def\blah#1#2 ... #9{<macro body>} | + | |
- | ``` | + | |
- | is intrinsically limited to just 9 arguments. There's no direct way | + | |
- | round this: how would you express a 10th argument? — and ensure that | + | |
- | the syntax didn't gobble some other valid usage? | + | |
- | If you really must have more than 9 arguments, the way to go is: | + | <code latex> |
- | <!-- {% raw %} --> | + | \def\truc#1#2 ... #9{Contenu de la commande} |
- | ```latex | + | </code> |
- | \def\blah#1#2 ... #9{% | + | |
+ | En effet, les commandes sont intrinsèquement limitées à 9 arguments (vous ne pouvez pas mettre un ''#10'' qui sera lu comme ''#1'' suivi de ''0''). | ||
+ | |||
+ | ===== Avec des commandes de base ===== | ||
+ | |||
+ | Si vous devez vraiment avoir plus de 9 arguments, la voie à suivre est la suivante : | ||
+ | |||
+ | <code latex> | ||
+ | \def\truc#1#2 ... #9{% | ||
\def\ArgI{{#1}}% | \def\ArgI{{#1}}% | ||
\def\ArgII{{#2}}% | \def\ArgII{{#2}}% | ||
... | ... | ||
\def\ArgIX{{#9}}% | \def\ArgIX{{#9}}% | ||
- | \BlahRelay | + | \TrucBis |
} | } | ||
- | \def\BlahRelay#1#2#3{% | + | \def\TrucBis#1#2#3{% |
- | % arguments 1-9 are now in | + | % Les arguments 1 à 9 sont maintenant dans |
- | % \ArgI-\ArgIX | + | % \ArgI-\ArgIX. |
- | % arguments 10-12 are in | + | % Les arguments 10 à 12 sont maintenant dans |
- | % #1-#3 | + | % #1 à #3. |
- | <macro body>% | + | Texte de la commande% |
} | } | ||
- | ``` | + | </code> |
- | <!-- {% endraw %} --> | + | |
- | This technique is easily extendible by concert pianists of the TeX | + | |
- | keyboard, but is really hard to recommend. | + | |
- | LaTeX users have the small convenience of merely giving a number of | + | Cette technique est facilement extensible par les spécialites de <latex>\TeX</latex> mais elle n'est pas vraiment recommandée. |
- | arguments in the `\newcommand` that defines each part of the | + | |
- | relaying mechanism: Knuth's restriction applies to `\newcommand` | + | |
- | just as it does to `\def`. However, LaTeX users also have the | + | |
- | way out of such barbarous command syntax: the [`keyval`](https://ctan.org/pkg/keyval) | + | |
- | package. With [`keyval`](https://ctan.org/pkg/keyval), and a bit of programming, one can | + | |
- | write really quite sophisticated commands, whose invocation might look | + | |
- | like: | + | |
- | <!-- {% raw %} --> | + | |
- | ```latex | + | |
- | \flowerinstance{species=Primula veris, | + | |
- | family=Primulaceae, | + | |
- | location=Coldham's Common, | + | |
- | locationtype=Common grazing land, | + | |
- | date=1995/04/24, | + | |
- | numplants=50, | + | |
- | soiltype=alkaline | + | |
- | } | + | |
- | ``` | + | |
- | <!-- {% endraw %} --> | + | |
- | The merit of such verbosity is that it is self-explanatory: the typist | + | |
- | doesn't have to remember that argument twelve is `soiltype`, and so | + | |
- | on: the commands may be copied from field notes quickly and | + | |
- | accurately. | + | |
- | * faq-mac-prog.tex (q-keyval): tweak words about getoptk | + | ===== En définissant des arguments clé-valeur ===== |
+ | En [[2_programmation:macros:arguments-cle-valeur|définissant des arguments clé-valeur]], on peut s'affranchir de la limite des 9 arguments tout en facilitant l'utilisation de la commande par les utilisateurs, par exemple en permettant d'écrire ceci : | ||
+ | |||
+ | <code latex> | ||
+ | \instancefleur{espece=Primula veris, | ||
+ | famille=Primulaceae, | ||
+ | localisation=Coldham's Common, | ||
+ | typeemplacement=Paturage, | ||
+ | date=24/04/1995, | ||
+ | nombre=50, | ||
+ | typesol=alkaline | ||
+ | } | ||
+ | </code> | ||
- | ---- | + | Le mérite d'une telle débauche de code est simple : l'utilisateur n'a pas besoin de se souvenir de l'ordre des paramètres, par exemple que le douzième argument serait ''typesol''. Sans compter la lisibilité du code pour un humain ! |
- | Content last updated: 2014-06-10 | + | ----- |
+ | //Source :// [[faquk>FAQ-moren9|How to break the 9-argument limit]] | ||
+ | {{htmlmetatags>metatag-keywords=(LaTeX,programming) | ||
+ | metatag-og:title=(Comment dépasser la limite des 9 arguments pour une commande) | ||
+ | metatag-og:site_name=(FAQ LaTeX francophone) | ||
+ | }} |