Tx · LaTeX & TikZ

How to Change the Margins in a LaTeX Environment

Published By the 9to5Science editorial team

At a glance

  1. 1. Load the package

    Use the changepage package to adjust the text block locally within an environment.

  2. 2. Define the environment

    Wrap adjustwidth in newenvironment and supply the left and right inset lengths.

  3. 3. Use the environment

    Place the text between begin and end commands; the inset ends with the environment.

If you want text inside a custom LaTeX environment to sit farther from the page edges, define the environment around adjustwidth from the changepage package. The two lengths control the left and right inset independently, and the adjustment ends automatically when your custom environment ends.

This changes the text block for that part of the document, not the document’s overall page margins. If you mean the margins of every page, use a page-layout package such as geometry instead.

Define an environment with local margins

Here is a complete, compilable example. The inset environment takes one argument, which sets both the left and right inset:

\documentclass{article}
\usepackage{changepage}

\newenvironment{inset}[1]{%
  \begin{adjustwidth}{#1}{#1}%
}{%
  \end{adjustwidth}%
}

\begin{document}

This paragraph uses the normal text width.

\begin{inset}{2em}
This paragraph is inset by 2em on both the left and the right. A longer
paragraph will wrap within the narrower text block.
\end{inset}

This paragraph returns to the normal text width.

\end{document}

The package needed here is changepage. The environment definition has three parts:

  • \newenvironment{inset}[1] defines an environment named inset with one required argument.
  • \begin{adjustwidth}{#1}{#1} starts a local text block with the specified left and right insets. Inside the definition, #1 means the first argument supplied when the environment is used.
  • \end{adjustwidth} closes that adjusted block when inset ends.

For example, \begin{inset}{2em} supplies 2em as the argument. Use different values when you want unequal insets. The first argument to adjustwidth is the left inset, and the second is the right inset:

\begin{adjustwidth}{1em}{3em}
Text begins 1em farther in on the left and 3em farther in on the right.
\end{adjustwidth}

You can copy that pattern into the begin and end parts of your own environment definition.

Use fixed margins instead

If the margins should always be the same, the custom environment does not need an argument:

\newenvironment{inset}{%
  \begin{adjustwidth}{2em}{2em}%
}{%
  \end{adjustwidth}%
}

Use it as \begin{inset} ... \end{inset}. This is convenient for a named block such as a quotation, note, or example whose indentation never varies.

Change the page margins instead

adjustwidth affects only the content inside the environment. To set the margins of the whole document, use geometry in the preamble, for example:

\usepackage[margin=1in]{geometry}

Avoid using a local indentation environment to imitate a global page-layout change: it narrows only selected text and can produce inconsistent page layouts.

Common mistakes

  • Using \hspace to change a paragraph’s margin. Horizontal space generally affects only the point where it is inserted; it does not change the wrapping width of an entire paragraph. adjustwidth changes the local text block.
  • Forgetting the package. The adjustwidth environment is provided by changepage, so include \usepackage{changepage} in the preamble.
  • Putting the closing code in the wrong place. The end part of \newenvironment must close the environment opened by the begin part. Here, \begin{adjustwidth} must be matched by \end{adjustwidth}.
  • Confusing an inset with a page margin. A local environment does not reset or redefine the document’s page dimensions; it adjusts only the enclosed content.

For a custom environment with margins you can choose each time, make the environment take two arguments and use them separately: \newenvironment{inset}[2]{\begin{adjustwidth}{#1}{#2}}{\end{adjustwidth}}. Then write \begin{inset}{1em}{3em} to set the left and right insets independently.

References

  1. The changepage package

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.