Margins – Change \textwidth and \textheight Mid-Document in LaTeX

margins

I set my \textwidth and \textheight in the preamble and would like to change both for some pages. How can this be done? E.g., the following example for \textwidth appears to work for the page number, but not for the text:

\documentclass{article} 
\textwidth=5cm \textheight=5cm
\begin{document}
A \hfill B
\eject \textwidth=10cm
A \hfill B
\end{document}

Output (true to scale):

Best Answer

You can use \newgeometry and \restoregeometry from the geometry package:

\documentclass{article}
\usepackage[textwidth=5cm,textheight=5cm]{geometry}
\usepackage{lipsum}
\begin{document}

\lipsum[1-2]
\newgeometry{textwidth=10cm,textheight=10cm}
\lipsum[1-4]
\restoregeometry
\lipsum[1-2]

\end{document}