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_environnements [2018/05/23 20:28] joseph.wright |
2_programmation:macros:que_sont_les_environnements [2021/07/06 07:06] jejust Compilation du code d'un exemple. |
||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | --- | + | ====== |
- | ID: Q-whatenv | + | |
- | section: Bits and pieces of (La)TeX | + | |
- | revised: 2014-06-10 | + | |
- | --- | + | |
- | # What are LaTeX '' | + | |
- | While TeX makes direct provision for commands, LaTeX adds a | + | |
- | concept of '' | + | While TeX makes direct provision for commands, LaTeX adds a concept of "environment". |
- | (of something or other) rather | + | Environments |
place in your document. | place in your document. | ||
A totally trivial environment could change the font in use for a chunk | A totally trivial environment could change the font in use for a chunk | ||
of text, as | of text, as | ||
- | ```latex | + | |
+ | < | ||
\newenvironment{monoblock}% | \newenvironment{monoblock}% | ||
{\ttfamily}% | {\ttfamily}% | ||
{} | {} | ||
- | ``` | + | </ |
- | which defines a `monoblock` which may be used as | + | which defines a '' |
- | ```latex | + | |
+ | <WRAP column 50ex> | ||
+ | < | ||
\begin{monoblock} | \begin{monoblock} | ||
- | | + | |
\end{monoblock} | \end{monoblock} | ||
- | ``` | + | </ |
- | which will look like: | + | </ |
- | | + | <WRAP column 30ex> |
- | so it is a particularly simple example. | + | < |
- | environment is introduced by `\begin{document}`; it looks | + | \documentclass{article} |
+ | | ||
+ | \pagestyle{empty} | ||
+ | |||
+ | \newenvironment{monoblock}% | ||
+ | {\ttfamily}% | ||
+ | {} | ||
+ | |||
+ | \begin{document} | ||
+ | \large | ||
+ | \begin{monoblock} | ||
+ | Some text set in monospace. | ||
+ | \end{monoblock} | ||
+ | \end{document} | ||
+ | </ | ||
+ | </ | ||
+ | <WRAP clear /> | ||
+ | |||
+ | It is a particularly simple example. | ||
+ | environment is introduced by '' | ||
simple, but needs all sorts of special TeX code to make it work | simple, but needs all sorts of special TeX code to make it work | ||
- | '' | + | "transparently"; most environments are more elaborate than |
- | `monoblock` and _much_ | + | '' |
- | `document`. | + | |
- | An environment puts its content inside a TeX _group_, so that | + | An environment puts its content inside a TeX //group//, so that |
- | commands used inside the environment don' | + | commands used inside the environment don' |
- | `monoblock` environment, | + | '' |
- | its own contents (the stuff between the `\begin{monoblock}` | + | its own contents (the stuff between the '' |
- | and `\end{monoblock}`), which is just what you need for this | + | and '' |
sort of thing. | sort of thing. | ||
- | So that' | + | So that' |
doesn' | doesn' | ||
- | ```latex | + | |
+ | < | ||
{\ttfamily some text set in monospace} | {\ttfamily some text set in monospace} | ||
- | ``` | + | </ |
- | though in fact many useful environments are just as simple (to look | + | though in fact many useful environments are just as simple (to look at). |
- | at). Some, such as `verbatim`, look simple but are | + | Some, such as '' |
- | actually very tricky inside. | + | |
LaTeX also allows arguments to an environment: | LaTeX also allows arguments to an environment: | ||
- | ```latex | + | |
+ | < | ||
\newenvironment{fontblock}[1]% | \newenvironment{fontblock}[1]% | ||
{# | {# | ||
{} | {} | ||
- | ``` | + | </ |
- | and use of `fontblock` as: | + | and use of '' |
- | ```latex | + | |
+ | < | ||
\begin{fontblock}{\ttfamily} | \begin{fontblock}{\ttfamily} | ||
- | ``` | + | </ |
- | would produce the same effect as the `monoblock` | + | would produce the same effect as the '' |
- | environment. | + | |
Environments may also have optional arguments, in much the same way as | Environments may also have optional arguments, in much the same way as | ||
commands: | commands: | ||
- | ```latex | + | |
+ | < | ||
\newenvironment{normaltext}[1][\itshape]% | \newenvironment{normaltext}[1][\itshape]% | ||
{#1}% | {#1}% | ||
{} | {} | ||
- | ``` | + | </ |
which will ordinarily set its body in italic, but | which will ordinarily set its body in italic, but | ||
- | ```latex | + | |
+ | < | ||
\begin{normaltext}[\ttfamily] | \begin{normaltext}[\ttfamily] | ||
... | ... | ||
\end{normaltext} | \end{normaltext} | ||
- | ``` | + | </ |
will observe its optional argument, and behave the same as the | will observe its optional argument, and behave the same as the | ||
- | `monoblock` we started with. | + | '' |
Note that an environments argument(s) (mandatory or optional) are | Note that an environments argument(s) (mandatory or optional) are | ||
- | _not_ passed to the `\end` text of the environment& | + | // |
is specified as a macro with no arguments, so that | is specified as a macro with no arguments, so that | ||
- | ```latex | + | |
+ | < | ||
\newenvironment{normaltext}[1][\itshape]% | \newenvironment{normaltext}[1][\itshape]% | ||
{#1}% | {#1}% | ||
{\typeout{what was #1, again?} | {\typeout{what was #1, again?} | ||
- | ``` | + | </ |
produces an error message | produces an error message | ||
- | ```latex | + | |
+ | <code text> | ||
! Illegal parameter number in definition of \endnormaltext. | ! Illegal parameter number in definition of \endnormaltext. | ||
- | ``` | + | </ |
So, if you need to pass an environment argument to the end-code, you | So, if you need to pass an environment argument to the end-code, you | ||
have to wrap it in a macro of its own: | have to wrap it in a macro of its own: | ||
- | <!-- {% raw %} --> | + | |
- | ```latex | + | |
+ | < | ||
\newenvironment{normaltext}[1][Intro]% | \newenvironment{normaltext}[1][Intro]% | ||
{#1% | {#1% | ||
| | ||
{\typeout{what was \foo{}, again?} | {\typeout{what was \foo{}, again?} | ||
- | ``` | + | </code> |
- | <!-- {% endraw %} --> | + | |
+ | |||
+ | ----- | ||
+ | //Source:// [[faquk>FAQ-whatenv|What are LaTeX “environments”]] | ||
+ | |||
+ | {{htmlmetatags> | ||
+ | metatag-og: | ||
+ | metatag-og: | ||
+ | }} | ||