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:que_sont_les_macros [2018/06/03 14:18] samcarter |
2_programmation:macros:que_sont_les_macros [2021/04/27 11:52] yannick.tanguy Traduction de l'article anglais, mise à jour et ajout de liens. |
||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | --- | + | ====== Comment définir des commandes? ====== |
- | title: What are (TeX) macros | + | |
- | category: concepts | + | |
- | permalink: / | + | |
- | date: 2014-06-10 | + | |
- | --- | + | |
- | TeX is a _macro processor_: this is a computer-science-y term | + | < |
- | meaning "text expander" | + | |
- | goes along, but _expands_ each macro it finds. TeX's macros | + | |
- | may include | + | |
- | generation one might expect. | + | |
- | Macros are a _good thing_, since they allow the user to | + | Les commandes sont //une bonne chose// car elles permettent à l' |
- | manipulate | + | |
- | `\TeX` is usually defined to produce " | + | |
- | (the original idea was Knuth's), | + | |
- | but in these FAQs the default definition of the macro is | + | |
- | overridden, and it simply expands to the letters " | + | |
- | may not think this a good thing, but the author of the macros has his | + | |
- | reasons& | + | |
- | Macro names are conventionally built from a `\` | + | ===== Le format des commandes ===== |
- | followed by a sequence of letters, which may be upper or lower case | + | |
- | (as in `\TeX`, mentioned above). | + | |
- | `<any single character> | + | |
- | sorts of oddities (many built in to most TeX macro sets, all the | + | |
- | way up from the apparently simple `\ ` meaning " | + | |
- | here" | + | |
- | Macro programming can be a complicated business, but at their very | + | Les noms de commande débutent traditionnellement par un ''\'' |
- | simplest they need little introduction& | + | |
- | told that: | + | |
- | ```latex | + | |
- | \def\foo{bar} | + | |
- | ``` | + | |
- | replaces each instance of `\foo` with the text " | + | |
- | command `\def` is Plain TeX syntax for defining commands; | + | |
- | LaTeX offers a macro `\newcommand` that goes some way towards | + | |
- | protecting users from themselves, but basically does the same thing: | + | |
- | ```latex | + | |
- | \newcommand{\foo}{bar} | + | |
- | ``` | + | |
- | Macros may have " | + | |
- | bits of the macro expansion: | + | |
- | ```latex | + | |
- | \def\foo# | + | |
- | ... | + | |
- | \foo{2/ | + | |
- | ``` | + | |
- | which produces: | + | |
- | This is a 2/4 bar. | + | |
- | or, in LaTeX speak: | + | |
- | ```latex | + | |
- | \newcommand{\foo}[1]{This is a #1 bar} | + | |
- | ... | + | |
- | \foo{3/ | + | |
- | ``` | + | |
- | which produces: | + | |
- | This is 3/4 bar. | + | |
- | (LaTeX users waltz through life, perhaps?) | + | |
- | You will have noticed that the arguments, above, were enclosed in | + | ===== Les commandes de définition ===== |
- | braces (`{`& | + | |
- | normal way of typing arguments, though TeX is enormously flexible, | + | |
- | and you may find all sorts of other ways of passing arguments (if you | + | |
- | stick with it). | + | |
- | Macro writing can get very complicated, | + | La programmation de commandes peut être parfois compliquée mais, réduite à sa plus simple expression, elle est assez intuitive : |
- | beginner (La)TeX programmer, you are well advised to read something | + | |
- | along the lines of the [TeXbook](FAQ-tex-books); | + | |
- | way, [TeX by Topic](FAQ-ol-books) is possibly a more satisfactory | + | |
- | choice. | + | |
- | about various issues of how to write macros. | + | |
+ | <code latex> | ||
+ | \def\truc{élément d' | ||
+ | </ | ||
+ | Cette instruction remplace chaque commande '' | ||
+ | |||
+ | <code latex> | ||
+ | \newcommand{\truc}{élément d' | ||
+ | </ | ||
+ | |||
+ | ===== Les commandes avec des arguments ===== | ||
+ | |||
+ | Les commandes peuvent avoir des // | ||
+ | |||
+ | <WRAP column 45ex> | ||
+ | <code latex> | ||
+ | \def\truc# | ||
+ | Un \truc{exceptionnel}. | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | <WRAP column 45ex> | ||
+ | < | ||
+ | \def\truc# | ||
+ | Un \truc{exceptionnel}. | ||
+ | </ | ||
+ | </ | ||
+ | <WRAP clear /> | ||
+ | |||
+ | En < | ||
+ | |||
+ | <WRAP column 45ex> | ||
+ | <code latex> | ||
+ | \newcommand{\truc}[1]{élément vraiment #1} | ||
+ | Un \truc{génial}. | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | <WRAP column 45ex> | ||
+ | < | ||
+ | \newcommand{\truc}[1]{élément vraiment #1} | ||
+ | Un \truc{génial}. | ||
+ | </ | ||
+ | </ | ||
+ | <WRAP clear /> | ||
+ | |||
+ | Vous aurez remarqué que les arguments ci-dessus étaient entre accolades « '' | ||
+ | |||
+ | ===== Apprendre à faire des commandes ===== | ||
+ | |||
+ | L' | ||
+ | |||
+ | ----- | ||
+ | //Source :// [[faquk> | ||
+ | |||
+ | {{htmlmetatags> | ||
+ | metatag-og: | ||
+ | metatag-og: | ||
+ | }} |