##  Class files

Environments are used to format blocks of text in a LaTeX documents. This article explains how to use environments and how to define new ones.

##  Introduction

Below you can see a very simple example on how to use an environment.

\begin{center}
This text will be centred since it is inside a special
environment. Environments provide a efficient way of modifying
blocks of text within your document.
\end{center}

In this example all the text inside the center environment is centred.

##  Environments

Environments are delimited by an opening tag \begin and a closing tag \end. Everything inside those tags will be formatted in a special manner depending on the type of the environment.

\begin{tabular}{ c c c }
cell1 & cell2 & cell3 \\
cell4 & cell5 & cell6 \\
cell7 & cell8 & cell9 \\
\end{tabular}

This environment tabular takes an additional parameter { c c c } to determine the alignment of the cells (See the Tables article for more information)

Environments may accept optional parameters that usually are passed inside brackets [ ]

##  Defining a new environment

Just as with commands, you can define new environments.

###  Defining simple environments

The new environment definition is achieved by the \newenvironment tag:

\newenvironment{boxed}
{\begin{center}
\begin{tabular}{|p{0.9\textwidth}|}
\hline\\
}
{
\\\\\hline
\end{tabular}
\end{center}
}
%--------------------------------------------------

Below this line a boxed environment is used

\begin{boxed}
This is the text formatted by the boxed environment
\end{boxed}

This text is again outside the environment

This environment will draw a box around the text within.

Right after the \newcommand, in between braces, you must write the name of the environment, boxed in the example. Below that are two pairs of braces. Inside the first pair of braces is set what your new environment will do before the text within, then inside the second pair of braces declare what your new environment will do after the text.

In the example, in between the before braces a horizontal line is drawn and the tabular environment is started to draw the vertical lines. Inside the after braces another horizontal line is drawn and the tabular environment is closed.

###  Defining environments with parameters

Environments that accept parameters can also be defined. Let's enhance the previous example to put a title for the box:

\newenvironment{boxed}[1]
{\begin{center}
#1\\[1ex]
\begin{tabular}{|p{0.9\textwidth}|}
\hline\\
}
{
\\\\\hline
\end{tabular}
\end{center}
}
%--------------------------------------------------

Below this line a boxed environment is used

\begin{boxed}{Title of the Box}
This is the text formatted by the boxed environment
\end{boxed}

This text is again outside the environment

As you see, the command definition is almost the same as in the example of the previous section, except for [1] that sets the number of parameters to be used in the environment; and #1\\[1ex] that inserts the parameter at the top of the box and also separates the title from the box by a 1ex blank space.

See the reference guide for a more complex example.

### Numbered environments

Numbered environments can be created either manually or with the command \newtheorem provided by the package amsmath, these commands can also include a \label tag for cross reference.

%In the preamble
---------------------------------
%Numbered environment
\newcounter{example}[section]
\newenvironment{example}[1][]{\refstepcounter{example}\par\medskip
\noindent \textbf{Example~\theexample. #1} \rmfamily}{\medskip}

%Numbered environment defined with Newtheorem
\usepackage{amsmath}
\newtheorem{SampleEnv}{Sample Environment}[section]
--------------------------------------------------------------------

\begin{example}
User-defined numbered environment
\end{example}

\begin{SampleEnv}
User-defined environment created with the \texttt{newtheorem} command.
\end{SampleEnv}

In the manually-defined environment the command \newcounter{example}[section] creates a counter called example that will be reset every time a new section is started. The counter is printed with \refstepcounter{example} within the environment definition, and its value is incremented by one. See the article about counters to learn more.

The command \newenvironment from the package amsmath also creates a numbered environment, this command takes three parameters: the name of the new environment, the text to be printed in blackbold font at the beginning of the line and an optional parameter that determines how the counter is printed and when it's reset. In the example the values are SampleEnv, Sample Environment and section respectively.

##  Overwriting existing environments

Environments can be overwritten with \renewcommand. The syntax is similar to that of the new command definition.

\renewenvironment{itemize}
{ \begin{center} \begin{em} }
{ \end{em} \end{center} }
%--------------------------------------------------

\begin{itemize}
This is now a environment that centres the text and
emphasises it
\end{itemize}

In this example we overwrite the itemize environment so instead of listing elements, this new environment centres and italicises the text within.

##  Reference guide

Advanced example: Defining a new environment for chapter

\documentclass{report}
\makeatletter
\def\thickhrulefill{\leavevmode \leaders \hrule height 1.2ex \hfill \kern \z@}
\vspace*{10\p@}%
{\parindent \z@ \centering \reset@font
\scshape\bfseries\textit{\@chapapp{}  \thechapter}
\par\nobreak
\vspace*{10\p@}%
\interlinepenalty\@M
\hrule
\vspace*{10\p@}%
\Huge \bfseries #1 \par\nobreak
\par
\vspace*{10\p@}%
\hrule
\vskip 100\p@
}}

\begin{document}
\chapter{Editing compile}
\end{document}