6

I would like to define an environment, that inserts a page with different margins than defined in the preamble.

I tried using the following LaTeX code.

\documentclass[a4paper,10pt]{article}

\usepackage[left=5.2cm,top=2cm,right=1.5cm,bottom=2cm,nohead,nofoot]{geometry}
\usepackage{blindtext}

\newenvironment{myenv}{
  \newgeometry{left=1.5cm,top=20cm,right=1.5cm,bottom=2cm,nohead,nofoot}
  \blindtext[2]
}{
  \newpage

  \restoregeometry
}

\begin{document}

\begin{myenv}

\end{myenv}
\blindtext[10]
\end{document}

Unfortunately on the second page there is an unexpected page break after a few lines. The following code produces the desired result.

\documentclass[a4paper,10pt]{article}

\usepackage[left=5.2cm,top=2cm,right=1.5cm,bottom=2cm,nohead,nofoot]{geometry}
\usepackage{blindtext}

\newenvironment{myenv}{
  \newgeometry{left=1.5cm,top=20cm,right=1.5cm,bottom=2cm,nohead,nofoot}
  \blindtext[2]
}{
  \newpage
}

\begin{document}

\begin{myenv}

\end{myenv}
\restoregeometry
\blindtext[10]
\end{document}

However I do not want to write \restoregeometry manually each time I use myenv. Can anyone explain me the difference between the two code pieces above and provide a solution for this problem? Thanks.

1
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Oct 2, 2013 at 15:27

1 Answer 1

7

I don't know what you are trying to do, but the command \AfterEndEnvironment from the etoolbox package can help you in this.

It inserts some code just after the environment is ended.

So, adding to your preamble

\usepackage{etoolbox}
\AfterEndEnvironment{myenv}{\restoregeometry}

should permit you to achieve what you want.

Complete MWE

\documentclass[a4paper,10pt]{article}

\usepackage[left=5.2cm,top=2cm,right=1.5cm,bottom=2cm,nohead,nofoot]{geometry}
\usepackage{blindtext}

\usepackage{etoolbox}
\AfterEndEnvironment{myenv}{\restoregeometry}

\newenvironment{myenv}{%
  \newgeometry{left=1.5cm,top=20cm,right=1.5cm,bottom=2cm,nohead,nofoot}%
  \blindtext[2]%
}{%
  \newpage%
}

\begin{document}

\begin{myenv}

\end{myenv}
\blindtext[10]
\end{document} 

To answer the question in your comment, I think that \restoregeometry doesn't work if you put it at the environment end, because the latter forms a group.

In fact, if you tell LaTeX to execute \restoregeometry after the group is finished with the command \aftergroup, you have a solution that hasn't the need for \AfterEndEnvironment:

\newenvironment{myenv}{%
  \newgeometry{left=1.5cm,top=20cm,right=1.5cm,bottom=2cm,nohead,nofoot}%
  \blindtext[2]%
}{%
  \newpage\aftergroup\restoregeometry%
}
2
  • Thanks. That seems to do the trick. But I still do not understand why there is a difference at all.
    – user26756
    Oct 2, 2013 at 18:13
  • @user26756 See the edited answer. BTW: remember that you can accept answers by clicking on the tickmark below their vote count (see How do you accept an answer?). Oct 2, 2013 at 18:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .