[Tex/LaTex] Changing top, bottom, left & right margins on the fly

indentationmargins

I’m trying to find a way of being able to modify all of the margins of a page whenever I want to and as many times as I want to on a single page in order to be able to put text very easily where I want on a page.

I have thought of using the geometry package and using

 \newgeometry{left=6.75cm, right=1cm, top=1cm, bottom=1cm}
 %…
 \restoregeometry

but unfortunately this only works for whole pages, so I can only change the margins once per page (and then they are valid for the whole page without having the possibility of changing them again).

I found the following script on the internet which enables users to change margins on the fly, just as I want to. The code below works fine and enables me to change the left and right margins of my page as many times as I want to, but unfortunately, it doesn’t give me the option of changing the top and bottom margins.

% placed before \begin{document}

\newenvironment{changemargin}[2]{%
\begin{list}{}{%
\setlength{\leftmargin}{#1}%
\setlength{\rightmargin}{#2}%
}%
\item[]}
{\end{list}}

% And then inside the document, whenever I want to use
% the command to change the margins on the current page

\begin{changemargin}{1.9875cm}{-3.7625cm}
% this means the left margin increases by 1.9875cm compared
% to the default left margin (4.7625cm) (so left margin=6.75cm here)
% and the right margin decreases by 3.7625cm (so right margin=1cm)

% ... (text or pictures in new environment)

\end{changemargin}

For this reason, I tried to adapt the code above and came up with the code below:

% placed before \begin{document}
\newenvironment{changemargin}[3]{%
\begin{list}{}{%
\setlength{\leftmargin}{#1}%
\setlength{\rightmargin}{#2}%
\setlength{\topmargin}{#3}%
}%
\item[]}
{\end{list}}

% And then to use the command:
\begin{changemargin}{1.9875cm}{-3.7625cm}{1cm}
% ...
\end{changemargin}

It compiles and still changes the left and right margins but the top margin remains unchanged. (I haven’t yet tried to change the bottom margin).

Can anyone help me get this code to work completely, and enable me to modify the left, right, top and bottom margins of a page? Thanks.

Best Answer

You can use a combination of geometry (to change the top and bottom margings) and the adjustwidth environment provided by the changepage package (this uses internally a list and it's similar to your changemargin environment):

\documentclass{article}
\usepackage[paperheight=14cm]{geometry}
\usepackage{changepage}
\usepackage{lipsum}

\begin{document}
\lipsum[1-3]
\newpage
\newgeometry{top=0.5cm, bottom=0.5cm}
\begin{adjustwidth}{-2cm}{-1cm}
\lipsum[4]
\end{adjustwidth}
\lipsum[1]
\begin{adjustwidth}{1cm}{2cm}
\lipsum[3]
\end{adjustwidth}
\lipsum[4]
\newpage
\restoregeometry
\lipsum[1-3]
\newpage
\newgeometry{top=2.5cm, bottom=2.5cm}
\begin{adjustwidth}{2cm}{2cm}
\lipsum[4]
\end{adjustwidth}
\lipsum[4]
\begin{adjustwidth}{3cm}{-3cm}
\lipsum[3]
\end{adjustwidth}

\end{document}

enter image description here

I strongly suggest you, as a personal opinion, not to do this.

Related Question