CERN Accelerating science

This website is no longer maintained. Its content may be obsolete. Please visit http://home.cern/ for current CERN information.



next up previous
Next: Playing with lengths Up: LaTeXeAn Overview Previous: Defining commands

Defining New Environments

In LaTeX 2.09, environments are defined or redefined with the commands:

\newenvironment{name}[narg]{begdef}{enddef}     
\renewenvironment{name}[narg]{begdef}{enddef} 

The number of arguments is in the range 0\(\le\)\Larg{narg ( )9}; and, in the case of no parameters, you can omit [0]. Inside the definition part, begdef, these parameters are referenced as #1 to #\Larg{narg}. If arguments are present, then they are defined when entering the environment by specifying them on the command \begin{myenv} as shown below.

\begin{myenv}{arg (_1 )}...{arg (_k )}

When exiting an environment with the command \end{myenv} no parameters can be specified. Moreover, the parameters specified with the \begin{myenv} command when entering the environment (see above) are no longer available in the definition part enddef where you define the actions which should take place when leaving the myenv environment.

As with commands, in LaTeXe you can now also define environments with an optional (first) argument.

\newenvironmentmyenv[narg][default]%  
     begdefenddef

The default for the optional argument is given between the second pair of square brackets [default]. Inside the begdef part, which is executed when the environment myenv is entered, the optional argument can be accessed with #1, while the mandatory arguments (when present) are addressed as #2 to #\Larg{narg}. When the myenv environment is used without an optional parameter, #1 will contain the string specified as [default].

As an example, a variant, deflist, of a description environment will be constructed. The deflist environment behaves somewhat like a standard LaTeX description environment if it is used without an optional argument. If an optional argument is specified, then the width of the description label will be put equal to the width of the argument. Thus, by specifying the widest entry in the list as an optional argument, you ensure that the description parts of all entries line up nicely.

The result below first shows the (default) behaviour of the deflist list and then what it looks like when using the optional argument.

\newenvironment{deflist}[1][\quad]%
 {\begin{list}{}{
  \renewcommand{\makelabel}[1]{\textbf{##1}\hfil}%
  \settowidth{\labelwidth}{\textbf{#1}}% 
  \setlength{\leftmargin}{\labelwidth+\labelsep}}}
 {\end{list}}
\begin{deflist}
\item[First] This is a short term.
\item[Long term] This is a long term.
\item[Even longer term] A very long term.
\end{deflist}
\begin{deflist}[Even longer term]
    .....  
\end{deflist}

[]

[First] This is a short term. [Long term] This is a long term. [Even longer term] A very long term.

[Even longer term] [First] This is a short term. [Long term] This is a long term. [Even longer term] A very long term.



next up previous
Next: Playing with lengths Up: LaTeXeAn Overview Previous: Defining commands



Janne Saarela
Wed May 17 14:38:58 METDST 1995