Modifying existing environments
15.2.1 Modifying existing environments
If you do not like the name of the proof environment and would prefer to use the name demo, define
\newenvironment{demo} {\begin{proof}} {\end{proof}}
Note that this does not change how the environment is typeset, only the way it is in- voked.
15.2 User-defined environments 381
To modify an existing environment, oldenv, type \newenvironment{name }
{begin_text } {end_text }
where begin text contains the command \begin{oldenv } and end text contains the command \end{oldenv }.
Tip Do not give a new environment the name of an existing command or environment.
For instance, if you define \newenvironment{parbox}
you get the error message ! LaTeX Error: Command \parbox already defined.
If there is an error in such a user-defined environment, the message generated refers to the environment that was modified, not to your environment. For instance, if you misspell proof as prof when you define
\newenvironment{demo} {\begin{prof}} {\end{proof}}
then at the first use of the demo environment you get the message ! LaTeX Error: Environment prof undefined.
l.13 \begin{demo} If you define
\newenvironment{demo} {\begin{proof}\em} {\end{prof}}
at the first use of demo you get the message ! LaTeX Error: \begin{proof} on input line 5
ended by \end{prof}. l.14 \end{demo}
382 Chapter 15 Customizing L A TEX Here are four more examples of modified environments.
1. The command \newenvironment{demo}
{\begin{proof}\em} {\end{proof}}
defines a demo environment that typesets an emphasized proof. Note that the scope of \em is the demo environment.
2. The following example defines a very useful environment. It takes an argument to
be typeset as the name of a theorem: \newtheorem*{namedtheorem}{\theoremname}
\newcommand{\theoremname}{testing} \newenvironment{named}[1]{
\renewcommand{\theoremname}{#1} \begin{namedtheorem}} {\end{namedtheorem}}
For example, \begin{named}{Name of the theorem}
Body of theorem. \end{named}
produces Name of the theorem. Body of theorem. in the style appropriate for the \newtheorem* declaration. This type of environ-
ment is often used to produce an unnumbered Main Theorem (see Section 15.4) or when typesetting an article or book in which the theorem numbering is already
fixed, for instance, when publishing a book in L A TEX that was originally typeset by another typesetting system.
3. In Sections 6.2.4 and 12.3.1, we came across the enumerate package, which allows you to customize the enumerate environment. If the enumerate package is loaded, you can invoke the enumerate environment with an optional argument specifying how the counter should be typeset, for instance, with the option [\upshape (i)],
\begin{enumerate}[\upshape (i)] \item First item\label{First} \end{enumerate}
items are numbered (i), (ii), and so on.
15.2 User-defined environments 383
So now we define \newenvironment{enumeratei}{\begin{enumerate}%
[\upshape (i)]}% {\end{enumerate}}
and we can invoke the new environment with (see Sections 15.3 and 15.4) \begin{enumeratei}
\item \label{ } \end{enumeratei} Reference items in the enumeratei environment with the \itemref com- mand introduced in Section 15.1.2.
4. If you want to define an environment for displaying text that is numbered as an equation, you might try
\newenvironment{texteqn}
{\begin{equation} \begin{minipage}{0.9\linewidth}} {\end{minipage} \end{equation}}
But there is a problem. If you use this environment in the middle of a paragraph, an interword space appears at the beginning of the first line after the environment. To remove this unwanted space, use the \ignorespacesafterend command, as in
\newenvironment{texteqn} {\begin{equation} \begin{minipage}{0.9\linewidth}} {\end{minipage} \end{equation} \ignorespacesafterend}
Examples 2 and 3 are included in the newlattice.sty command file (see Sec- tion 15.3). See the sample article, sampartu.tex in Section 15.4, for some instances of their use.
See Section 15.6.3 for custom lists as user-defined environments. Redefine an existing environment with the \renewenvironment command. It is
similar to the \renewcommand command (see Section 15.1.5). There are some environments you cannot redefine; for instance, verbatim and all the AMS multiline math environments.