Les deux révisions précédentes
Révision précédente
|
|
2_programmation:macros:def_newcommand_providecommand [2018/12/03 23:26] jejust |
2_programmation:macros:def_newcommand_providecommand [2022/02/18 07:23] (Version actuelle) yannick.tanguy Traduction de la page. Ajout de liens. |
====== Transcribing LaTeX command definitions ====== | ====== Comment traduire des commandes LaTeX en commandes TeX? ====== |
| |
| À plusieurs endroits de cette FAQ sont données des réponses intégrant des définitions de commandes <latex>\LaTeX</latex>. Parfois, ces commandes peuvent également aider les utilisateurs de Plain <latex>\TeX</latex> ou d'autres [[https://faq.gutenberg.eu.org/1_generalites/glossaire/qu_est_ce_qu_un_format|formats]]. Cette page tente de fournir un guide approximatif pour transcrire ces définitions de commandes dans d'autres formats. |
| |
At several places in this FAQ, questions are answered in terms | <latex>\LaTeX</latex> propose des commandes qui remplacent ''\def'' du fait de sa philosophie générale : l'utilisateur doit être protégé de lui-même. Ainsi, l'utilisateur dispose des commandes suivantes : |
of how to program a LaTeX macro. Sometimes, these macros might | * deux commandes différentes selon que la commande à définir existe (''\renewcommand'') ou pas (''\newcommand''). Si l'état de la commande s'avère différent de celui attendu par l'utilisateur, une erreur est signalée ; |
also help users of Plain TeX or other packages; this answer | * la commande ''\providecommand'' définit uniquement une commande si la cible n'est pas déjà définie ; |
attempts to provide a rough-and-ready guide to transcribing such macro | * la commande ''\DeclareRobustCommand'' crée une commande qui est « robuste » (c'est-à-dire qui ne se développera pas si elle est soumise à un "développement protégé" par <latex>\LaTeX</latex>). Pour un utilisateur de Plain <latex>\TeX</latex>, ''\DeclareRobustCommand'' doit être traitée comme une version non vérifiable de ''\newcommand''. |
definitions for use in other packages. | |
| |
The reason LaTeX has commands that replace ''\def'', is that | Ainsi, <latex>\LaTeX</latex> n'a pas d'équivalent direct de ''\def'', qui ignore l'état actuel de la commande. |
there's a general philosophy within LaTeX that the user should be | |
protected from himself: the user has different commands according to | |
whether the command to be defined exists (''\renewcommand'') or not | |
(''\newcommand''), and if its status proves not as the user expected, | |
an error is reported. A third definition command, | |
''\providecommand'', only defines if the target is not already | |
defined; LaTeX has no direct equivalent of ''\def'', which ignores | |
the present state of the command. The final command of this sort is | |
''\DeclareRobustCommand'', which creates a command which is "robust" | |
(i.e., will not expand if subjected to LaTeX "protected expansion"); | |
from the Plain TeX user's point of view, | |
''\DeclareRobustCommand'' should be treated as a non-checking version | |
of ''\newcommand''. | |
| |
LaTeX commands are, by default, defined ''\long''; an optional ''*'' | Les commandes <latex>\LaTeX</latex> sont, par défaut, définies avec la structure ''\long'' ; un « ''*'' » optionnel entre ''\newcommand'' et ses arguments spécifie que la commande n'utilise pas la structure ''\long''. Le « ''*'' » est ainsi détecté par une commande ''\ifstar'' qui utilise ''\futurelet'' pour basculer entre deux branches et qui absorbe le « ''*'' » : les utilisateurs de <latex>\LaTeX</latex> sont encouragés à considérer le « ''*'' » comme une part du nom de la commande. |
between the ''\newcommand'' and its (other) arguments specifies that | |
the command is //not// to be defined ''\long''. The ''*'' is | |
detected by a command ''ifstar'' which uses ''\futurelet'' to switch | |
between two branches, and gobbles the ''*'': LaTeX users are | |
encouraged to think of the ''*'' as part of the command name. | |
| |
LaTeX's checks for unknown command are done by ''\ifx'' comparison | Les arguments d'une commande <latex>\LaTeX</latex> sont spécifiés par deux arguments optionnels : un nombre d'arguments (de 0 à 9 et, si le nombre est 0, l'argument optionnel peut être omis) et une valeur par défaut pour le premier argument si celui-ci peut être facultatif. Par exemple : |
of a ''\csname'' construction with ''\relax''; since the command name | |
argument is the desired control sequence name, this proves a little | |
long-winded. Since ''#1'' is the requisite argument, we have: | |
| |
<code latex> | <code latex> |
\expandafter\ifx | \newcommand\truc{...} |
\csname\expandafter\@gobble\string#1\endcsname | \newcommand\truc[0]{...} |
\relax | \newcommand\truc[1]{...#1...} |
... | \newcommand\truc[2][machin]{...#1...#2...} |
</code> | </code> |
(''\@gobble'' simply throws away its argument). | |
| |
The arguments of a LaTeX command are specified by two optional | Dans le dernier cas, ''\truc'' peut être appelé sous la forme ''\truc{blabla}'' qui équivaut à ''\truc[machin]{blabla}'' (en utilisant la valeur par défaut donnée pour le premier argument) ou sous la forme ''\foo[chose]{blabla}'' (avec un premier argument explicite). |
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 | |
value for the first argument, if the defined command's first argument | |
is to be optional. So: | |
| |
<code latex> | |
\newcommand\foo{...} | |
\newcommand\foo[0]{...} | |
\newcommand\foo[1]{...#1...} | |
\newcommand\foo[2][boo]{...#1...#2...} | |
</code> | |
In the last case, ''\foo'' may be called as ''\foo{goodbye}'', | |
which is equivalent to ''\foo[boo]{goodbye}'' (employing the | |
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 the last ''\foo'' above: | |
| |
| La manière de coder des commandes avec des arguments optionnels est illustrée ici pour notre exemple ''\truc'' : |
| |
<code latex> | <code latex> |
\def\foo{\futurelet\next\@r@foo} | \def\truc{\futurelet\next\@r@truc} |
\def\@r@foo{\ifx\next[% | \def\@r@truc{\ifx\next[% |
\let\next\@x@foo | \let\next\@x@truc |
\else | \else |
\def\next{\@x@foo[boo]}% | \def\next{\@x@truc[machin]}% |
\fi | \fi |
\next | \next |
} | } |
\def\@x@foo[#1]#2{...#1...#2...} | \def\@x@truc[#1]#2{...#1...#2...} |
</code> | </code> |
| |
| |
| |
----- | ----- |
| //Source :// [[faquk>FAQ-cvtlatex|Transcribing LaTeX command definitions]] |
//Source:// [[faquk>FAQ-cvtlatex|Transcribing LaTeX command definitions]] | |
| |
{{htmlmetatags>metatag-keywords=(LaTeX,programming) | {{htmlmetatags>metatag-keywords=(LaTeX,programming) |
metatag-og:title=(Transcribing LaTeX command definitions) | metatag-og:title=(Comment traduire des commandes LaTeX en commandes TeX) |
metatag-og:site_name=(FAQ LaTeX francophone) | metatag-og:site_name=(FAQ LaTeX francophone) |
}} | }} |
| |