While TeX makes direct provision for commands, LaTeX adds a concept of “environment”; environments perform an action on a block (of something or other) rather than than just doing something at one place in your document.
A totally trivial environment could change the font in use for a chunk of text, as
which defines a\newenvironment{monoblock}% {\ttfamily}% {}
monoblock
which may be used as
which will look like:\begin{monoblock} some text set in monospace \end{monoblock}
some text set in monospaceso it is a particularly simple example. A rather complicated environment is introduced by
\begin
{document}
; it looks
simple, but needs all sorts of special TeX code to make it work
‘transparently’; most environments are more elaborate than
monoblock
and much simpler than
document
.
An environment puts its content inside a TeX group, so that
commands used inside the environment don’t ‘leak out’ — the
monoblock
environment, above, restricts its effect to
its own contents (the stuff between the \begin
{monoblock}
and \end
{monoblock}
), which is just what you need for this
sort of thing.
So that’s “simple” environments; the monoblock
, above
doesn’t actually gain us much over
though in fact many useful environments are just as simple (to look at). Some, such as{\ttfamily some text set in monospace}
verbatim
, look simple but are
actually very tricky inside.
LaTeX also allows arguments to an environment:
and use of\newenvironment{fontblock}[1]% {#1\selectfont}% {}
fontblock
as:
would produce the same effect as the\begin{fontblock}{\ttfamily}
monoblock
environment.
Environments may also have optional arguments, in much the same way as commands:
which will ordinarily set its body in italic, but\newenvironment{normaltext}[1][\itshape]% {#1}% {}
will observe its optional argument, and behave the same as the\begin{normaltext}[\ttfamily] ... \end{normaltext}
monoblock
we started with.
Note that an environments argument(s) (mandatory or optional) are
not passed to the ‘\end
’ text of the environment — that
is specified as a macro with no arguments, so that
produces an error message\newenvironment{normaltext}[1][\itshape]% {#1}% {\typeout{what was #1, again?}
So, if you need to pass an environment argument to the end-code, you have to wrap it in a macro of its own:! Illegal parameter number in definition of \endnormaltext.
\newenvironment{normaltext}[1][Intro]% {#1% \newcommand{\foo}{#1}}% {\typeout{what was \foo{}, again?}
This answer last edited: 2013-02-20
This question on the Web: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=whatenv