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 Prochaine révision Les deux révisions suivantes | ||
2_programmation:macros:que_sont_les_macros [2018/06/03 15:18] samcarter |
2_programmation:macros:que_sont_les_macros [2018/12/02 23:45] jejust |
||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | --- | + | ====== What are (TeX) macros ====== |
- | title: What are (TeX) macros | + | |
- | category: concepts | + | |
- | permalink: /FAQ-whatmacros | + | |
- | date: 2014-06-10 | + | |
- | --- | + | |
- | TeX is a _macro processor_: this is a computer-science-y term | + | TeX is a //macro processor//: this is a computer-science-y term |
meaning "text expander" (more or less); TeX typesets text as it | meaning "text expander" (more or less); TeX typesets text as it | ||
- | goes along, but _expands_ each macro it finds. TeX's macros | + | goes along, but //expands// each macro it finds. TeX's macros |
may include instructions to TeX itself, on top of the simple text | may include instructions to TeX itself, on top of the simple text | ||
generation one might expect. | generation one might expect. | ||
- | Macros are a _good thing_, since they allow the user to | + | Macros are a //good thing//, since they allow the user to |
manipulate documents according to context. For example, the macro | manipulate documents according to context. For example, the macro | ||
- | `\TeX` is usually defined to produce "TEX" with the "E" lowered | + | ''\TeX'' is usually defined to produce "TEX" with the "E" lowered |
(the original idea was Knuth's), | (the original idea was Knuth's), | ||
but in these FAQs the default definition of the macro is | but in these FAQs the default definition of the macro is | ||
- | overridden, and it simply expands to the letters "TeX". (_You_ | + | overridden, and it simply expands to the letters "TeX". (//You// |
may not think this a good thing, but the author of the macros has his | may not think this a good thing, but the author of the macros has his | ||
- | reasons – see [TeX-related logos](FAQ-logos).) | + | reasons -- see [[FAQ-logos|TeX-related logos]].) |
- | Macro names are conventionally built from a `\` | + | Macro names are conventionally built from a ''\'' |
followed by a sequence of letters, which may be upper or lower case | followed by a sequence of letters, which may be upper or lower case | ||
- | (as in `\TeX`, mentioned above). They may also be | + | (as in ''\TeX'', mentioned above). They may also be |
- | `<any single character>`, which allows all | + | ''<any single character>'', which allows all |
sorts of oddities (many built in to most TeX macro sets, all the | sorts of oddities (many built in to most TeX macro sets, all the | ||
- | way up from the apparently simple `\ ` meaning "insert a space | + | way up from the apparently simple ''\ '' meaning "insert a space |
here"). | here"). | ||
Macro programming can be a complicated business, but at their very | Macro programming can be a complicated business, but at their very | ||
- | simplest they need little introduction — you'll hardly need to be | + | simplest they need little introduction --- you'll hardly need to be |
told that: | told that: | ||
- | ```latex | + | |
+ | <code latex> | ||
\def\foo{bar} | \def\foo{bar} | ||
- | ``` | + | </code> |
- | replaces each instance of `\foo` with the text "bar". The | + | replaces each instance of ''\foo'' with the text "bar". The |
- | command `\def` is Plain TeX syntax for defining commands; | + | command ''\def'' is Plain TeX syntax for defining commands; |
- | LaTeX offers a macro `\newcommand` that goes some way towards | + | LaTeX offers a macro ''\newcommand'' that goes some way towards |
protecting users from themselves, but basically does the same thing: | protecting users from themselves, but basically does the same thing: | ||
- | ```latex | + | |
+ | <code latex> | ||
\newcommand{\foo}{bar} | \newcommand{\foo}{bar} | ||
- | ``` | + | </code> |
Macros may have "arguments" , which are used to substitute for marked | Macros may have "arguments" , which are used to substitute for marked | ||
bits of the macro expansion: | bits of the macro expansion: | ||
- | ```latex | + | |
+ | <code latex> | ||
\def\foo#1{This is a #1 bar} | \def\foo#1{This is a #1 bar} | ||
... | ... | ||
\foo{2/4}. | \foo{2/4}. | ||
- | ``` | + | </code> |
which produces: | which produces: | ||
This is a 2/4 bar. | This is a 2/4 bar. | ||
or, in LaTeX speak: | or, in LaTeX speak: | ||
- | ```latex | + | |
+ | <code latex> | ||
\newcommand{\foo}[1]{This is a #1 bar} | \newcommand{\foo}[1]{This is a #1 bar} | ||
... | ... | ||
\foo{3/4}. | \foo{3/4}. | ||
- | ``` | + | </code> |
which produces: | which produces: | ||
This is 3/4 bar. | This is 3/4 bar. | ||
Ligne 62: | Ligne 62: | ||
You will have noticed that the arguments, above, were enclosed in | You will have noticed that the arguments, above, were enclosed in | ||
- | braces (`{`…`}`); this is the | + | braces (''{''...''}''); this is the |
normal way of typing arguments, though TeX is enormously flexible, | normal way of typing arguments, though TeX is enormously flexible, | ||
and you may find all sorts of other ways of passing arguments (if you | and you may find all sorts of other ways of passing arguments (if you | ||
Ligne 69: | Ligne 69: | ||
Macro writing can get very complicated, very quickly. If you are a | Macro writing can get very complicated, very quickly. If you are a | ||
beginner (La)TeX programmer, you are well advised to read something | beginner (La)TeX programmer, you are well advised to read something | ||
- | along the lines of the [TeXbook](FAQ-tex-books); once you're under | + | along the lines of the [[FAQ-tex-books|TeXbook]]; once you're under |
- | way, [TeX by Topic](FAQ-ol-books) is possibly a more satisfactory | + | way, [[FAQ-ol-books|TeX by Topic]] is possibly a more satisfactory |
choice. Rather a lot of the answers in these FAQs tell you | choice. Rather a lot of the answers in these FAQs tell you | ||
about various issues of how to write macros. | about various issues of how to write macros. | ||
+ | |||
+ | --- | ||
+ | |||
+ | //Source:// [[faquk>FAQ-whatmacros|What are (TeX) macros]] | ||
+ | |||
+ | {{htmlmetatags>metatag-keywords=(LaTeX,concepts) | ||
+ | metatag-og:title=(What are (TeX) macros) | ||
+ | metatag-og:site_name=(FAQ LaTeX francophone) | ||
+ | }} | ||