×
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.

Contents

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

CommsAndEnvsEx7.png

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 \\ 
 \end{tabular}

CommsAndEnvsEx11.png

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:

\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

CommsAndEnvsEx8.png

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:

\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

CommsAndEnvsEx9.png

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
\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}

NumberedEnvsEx.png

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.

\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}

CommsAndEnvsEx10.png

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

\documentclass{report}
\makeatletter
\def\thickhrulefill{\leavevmode \leaders \hrule height 1.2ex \hfill \kern \z@}
\def\@makechapterhead#1{
  \vspace*{10\p@}%
  {\parindent \z@ \centering \reset@font
        \thickhrulefill\quad 
        \scshape\bfseries\textit{\@chapapp{}  \thechapter}  
        \quad \thickhrulefill
        \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}

Chapter.png

[edit] Further reading

For more information see: