Klicka här för att använda ShareLaTeX på Engelska

[edit] ShareLaTeX guides

[edit] LaTeX Basics

[edit] Mathematics

[edit] Figures and tables

[edit] References and Citations

[edit] Languages

[edit] Document structure

[edit] Formatting

[edit] Fonts

[edit] Presentations

[edit] Commands

[edit] Field specific

[edit] 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.


[edit] Introduction

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

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.


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

  Open an example in ShareLaTeX

[edit] 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 \\ 


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 [ ]

  Open an example in ShareLaTeX

[edit] Defining a new environment

Just as with commands, you can define new environments.

[edit] Defining simple environments

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

Below this line a boxed environment is used
This is the text formatted by the boxed environment
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.

  Open an example in ShareLaTeX

[edit] 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:

Below this line a boxed environment is used
\begin{boxed}{Title of the Box}
This is the text formatted by the boxed environment
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.

[edit] 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
   \noindent \textbf{Example~\theexample. #1} \rmfamily}{\medskip}
%Numbered environment defined with Newtheorem
\newtheorem{SampleEnv}{Sample Environment}[section]
User-defined numbered environment
User-defined environment created with the \texttt{newtheorem} command.


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.

  Open an example in ShareLaTeX

[edit] Overwriting existing environments

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

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


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

  Open an example in ShareLaTeX

[edit] Reference guide

Advanced example: Defining a new environment for chapter

\def\thickhrulefill{\leavevmode \leaders \hrule height 1.2ex \hfill \kern \z@}
  {\parindent \z@ \centering \reset@font
        \scshape\bfseries\textit{\@chapapp{}  \thechapter}  
        \quad \thickhrulefill
        \Huge \bfseries #1 \par\nobreak
        \vskip 100\p@
\chapter{Editing compile}


[edit] Further reading

For more information see: