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 Dernière révision Les deux révisions suivantes | ||
2_programmation:macros:def_newcommand_providecommand [2018/05/25 19:32] joseph.wright |
2_programmation:macros:def_newcommand_providecommand [2018/12/03 23:26] jejust |
||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | --- | + | ====== Transcribing LaTeX command definitions ====== |
- | category: programming | + | |
- | tags: macros | + | |
- | permalink: / | + | |
- | date: 2014-06-10 | + | |
- | --- | + | |
- | # Transcribing LaTeX command definitions | ||
At several places in this FAQ, questions are answered in terms | At several places in this FAQ, questions are answered in terms | ||
Ligne 14: | Ligne 8: | ||
definitions for use in other packages. | definitions for use in other packages. | ||
- | The reason LaTeX has commands that replace | + | The reason LaTeX has commands that replace |
there' | there' | ||
protected from himself: the user has different commands according to | protected from himself: the user has different commands according to | ||
- | whether the command to be defined exists (`\renewcommand`) or not | + | whether the command to be defined exists ('' |
- | (`\newcommand`), and if its status proves not as the user expected, | + | ('' |
an error is reported. | an error is reported. | ||
- | `\providecommand`, only defines if the target is not already | + | '' |
- | defined; LaTeX has no direct equivalent of `\def`, which ignores | + | defined; LaTeX has no direct equivalent of '' |
the present state of the command. | the present state of the command. | ||
- | `\DeclareRobustCommand`, which creates a command which is '' | + | '' |
- | (i.e., will not expand if subjected to LaTeX '' | + | (i.e., will not expand if subjected to LaTeX "protected expansion"); |
- | expansion'' | + | from the Plain TeX user's point of view, |
- | `\DeclareRobustCommand` should be treated as a non-checking version | + | '' |
- | of `\newcommand`. | + | of '' |
- | LaTeX commands are, by default, defined | + | LaTeX commands are, by default, defined |
- | between the `\newcommand` and its (other) arguments specifies that | + | between the '' |
- | the command is _not_ to be defined | + | the command is // |
- | detected by a command | + | detected by a command |
- | between two branches, and gobbles the `*`: LaTeX users are | + | between two branches, and gobbles the '' |
- | encouraged to think of the `*` as part of the command name. | + | encouraged to think of the '' |
- | LaTeX' | + | LaTeX' |
- | of a `\csname` construction with `\relax`; since the command name | + | of a '' |
argument is the desired control sequence name, this proves a little | argument is the desired control sequence name, this proves a little | ||
- | long-winded. | + | long-winded. |
- | ```latex | + | |
+ | < | ||
\expandafter\ifx | \expandafter\ifx | ||
\csname\expandafter\@gobble\string# | \csname\expandafter\@gobble\string# | ||
\relax | \relax | ||
... | ... | ||
- | ``` | + | </ |
- | (`gobble` simply throws away its argument). | + | ('' |
The arguments of a LaTeX command are specified by two optional | The arguments of a LaTeX command are specified by two optional | ||
- | arguments to the defining command: a count of arguments (0–9: if the | + | arguments to the defining command: a count of arguments (0--9: if the |
count is 0, the optional count argument may be omitted), and a default | count is 0, the optional count argument may be omitted), and a default | ||
value for the first argument, if the defined command' | value for the first argument, if the defined command' | ||
is to be optional. | is to be optional. | ||
- | ```latex | + | |
+ | < | ||
\newcommand\foo{...} | \newcommand\foo{...} | ||
\newcommand\foo[0]{...} | \newcommand\foo[0]{...} | ||
\newcommand\foo[1]{...# | \newcommand\foo[1]{...# | ||
\newcommand\foo[2][boo]{...# | \newcommand\foo[2][boo]{...# | ||
- | ``` | + | </ |
- | In the last case, `\foo` may be called as `\foo{goodbye}`, | + | In the last case, '' |
- | which is equivalent to `\foo[boo]{goodbye}` (employing the | + | which is equivalent to '' |
default value given for the first argument), or as | default value given for the first argument), or as | ||
- | `\foo[hello]{goodbye}` (with an explicit first argument). | + | '' |
Coding of commands with optional arguments is exemplified by the | Coding of commands with optional arguments is exemplified by the | ||
- | coding of the last `\foo` above: | + | coding of the last '' |
- | <!-- {% raw %} --> | + | |
- | ```latex | + | |
+ | < | ||
\def\foo{\futurelet\next\@r@foo} | \def\foo{\futurelet\next\@r@foo} | ||
\def\@r@foo{\ifx\next[% | \def\@r@foo{\ifx\next[% | ||
Ligne 77: | Ligne 74: | ||
} | } | ||
\def\@x@foo[# | \def\@x@foo[# | ||
- | ``` | + | </code> |
- | <!-- {% endraw %} --> | + | |
+ | |||
+ | |||
+ | ----- | ||
+ | |||
+ | //Source:// [[faquk>FAQ-cvtlatex|Transcribing LaTeX command definitions]] | ||
+ | {{htmlmetatags> | ||
+ | metatag-og: | ||
+ | metatag-og: | ||
+ | }} | ||