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:macros_a_plusieurs_arguments_optionnels [2018/06/03 13:35] d.p.carlisle |
2_programmation:macros:macros_a_plusieurs_arguments_optionnels [2018/12/03 23:28] (Version actuelle) jejust |
||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | --- | + | ====== |
- | title: | + | |
- | category: programming | + | |
- | tags: | + | |
- | - latex | + | |
- | - macros | + | |
- | permalink: / | + | |
- | --- | + | |
If you've already read | If you've already read | ||
- | " | + | "[[FAQ-moren9|breaking the 9-argument limit]]". |
you can probably guess the " | you can probably guess the " | ||
command relaying. | command relaying. | ||
LaTeX allows commands with a single optional argument thus: | LaTeX allows commands with a single optional argument thus: | ||
- | ```latex | + | |
+ | < | ||
\newcommand{\blah}[1][Default]{...} | \newcommand{\blah}[1][Default]{...} | ||
- | ``` | + | </ |
You may legally call such a command either with its optional argument | You may legally call such a command either with its optional argument | ||
present, as | present, as | ||
- | `\blah[nonDefault]` or without, as `\blah`; in the latter | + | '' |
- | case, the code of `\blah` will have an argument of `Default`. | + | case, the code of '' |
To define a command with two optional arguments, we use the relaying | To define a command with two optional arguments, we use the relaying | ||
technique, as follows: | technique, as follows: | ||
- | <!-- {% raw %} --> | + | |
- | ```latex | + | |
+ | < | ||
\newcommand{\blah}[1][Default1]{% | \newcommand{\blah}[1][Default1]{% | ||
\def\ArgI{{# | \def\ArgI{{# | ||
Ligne 37: | Ligne 33: | ||
...% | ...% | ||
} | } | ||
- | ``` | + | </code> |
- | <!-- {% endraw %} --> | + | |
- | Of course, | + | Of course, |
allowed, after allowance for the one taken up with its own | allowed, after allowance for the one taken up with its own | ||
- | optional argument& | + | optional argument |
- | Variants of `\newcommand` (and friends), with names like | + | Variants of '' |
- | `\newcommandtwoopt`, are available in the [`twoopt`](https:// | + | '' |
However, if you can, it's probably better to learn to write the commands | However, if you can, it's probably better to learn to write the commands | ||
yourself, just to see why they' | yourself, just to see why they' | ||
Ligne 52: | Ligne 48: | ||
sensible: obviously you can extend the technique to provide as many | sensible: obviously you can extend the technique to provide as many | ||
optional arguments as your fevered imagination can summon. | optional arguments as your fevered imagination can summon. | ||
- | see the comments on the use of the [`keyval`](https:// | + | see the comments on the use of the [[ctanpkg> |
- | " | + | "[[FAQ-moren9|breaking the 9-argument limit]]", |
which offers an alternative way forward. | which offers an alternative way forward. | ||
- | If you must, however, consider the [`optparams`](https:// | + | If you must, however, consider the [[ctanpkg> |
- | [`xargs`](https:// | + | [[ctanpkg> |
- | provides a `\optparams` command that you use as an intermediate in | + | provides a '' |
defining commands with up to nine optional arguments. | defining commands with up to nine optional arguments. | ||
documentation shows examples of commands with four optional arguments | documentation shows examples of commands with four optional arguments | ||
(and this from an author who has his own key-value package!). | (and this from an author who has his own key-value package!). | ||
- | The [`xargs`](https:// | + | The [[ctanpkg> |
- | ([`xkeyval`](https://ctan.org/pkg/xkeyval)) to _define_ | + | ([[ctanpkg> |
arguments. | arguments. | ||
- | ```latex | + | |
+ | < | ||
\usepackage{xargs} | \usepackage{xargs} | ||
... | ... | ||
\newcommandx{\foo}[3][1=1, | \newcommandx{\foo}[3][1=1, | ||
- | ``` | + | </ |
- | defines a command | + | defines a command |
(default 1), a mandatory second argument, and an optional third | (default 1), a mandatory second argument, and an optional third | ||
argument (default n). | argument (default n). | ||
An alternative approach is offered by Scott Pakin' | An alternative approach is offered by Scott Pakin' | ||
- | `newcommand` program, which takes a command name and a | + | '' |
definition of a set of command arguments (in a fairly | definition of a set of command arguments (in a fairly | ||
readily-understood language), and emits (La)TeX macros which enable | readily-understood language), and emits (La)TeX macros which enable | ||
the command to be defined. | the command to be defined. | ||
- | `Python` interpreter (etc.) be installed on your computer. | + | Python interpreter (etc.) be installed on your computer. |
+ | |||
+ | |||
+ | ----- | ||
+ | |||
+ | //Source:// [[faquk> | ||
+ | {{htmlmetatags> | ||
+ | metatag-og: | ||
+ | metatag-og: | ||
+ | }} | ||