# Custom geometry per page style

I use fancyhdr to create custom headers and footers. I also use textpos and Tikz to embellish those headers and footers.

I want to use textpos with absolute positioning instead of tikz current page because it does not require multiple compilation.

Now I declare several page style with variable header/footer heights. I would like to add a custom geometry for the page not overlapping the header/footer.

I got here a MWE

% -*- latex -*-
\documentclass[a4paper]{article}

\usepackage{fontspec}
\usepackage{tikz}
\usepackage{fancyhdr}
\usepackage{blindtext}
\usepackage[absolute,overlay]{textpos}
\usepackage{geometry}

\makeatletter
\begin{textblock*}{\paperwidth}[0,0](0pt, 0pt)
\begin{tikzpicture}
\node [rectangle, draw=red,
anchor=north,
minimum width=\paperwidth-\pgflinewidth,
minimum height=5cm] (box) at (0.5\paperwidth,0mm) {};
\end{tikzpicture}
\end{textblock*}
}%

\newcommand{\footer@first}{%
\begin{textblock*}{\paperwidth}[0,1](0pt, \paperheight)
\begin{tikzpicture}
\node [rectangle, draw=red,
anchor=south,
minimum width=\paperwidth-\pgflinewidth,
minimum height=8cm] (box) at (0.5\paperwidth,\paperheight) {};
\end{tikzpicture}
\end{textblock*}
}%

\fancypagestyle{firstpage}{%
\fancyhf{}
\lfoot{} \cfoot{\footer@first} \rfoot{}
\renewcommand{\footrulewidth}{0pt}
\newgeometry{top=5cm, bottom=8cm, hmargin={1cm, 1cm}, textwidth=19cm}
}

\fancypagestyle{otherpage}{%
\fancyhf{}
\lfoot{} \cfoot{} \rfoot{}
\renewcommand{\footrulewidth}{0pt}
\newgeometry{top=1cm, bottom=1cm, hmargin={5cm, 5cm}, textwidth=11cm  }
}

\pagestyle{otherpage}

\makeatother

\begin{document}
\thispagestyle{firstpage}
\Blinddocument
\newpage
\pagestyle{fancy}
\blinddocument
\end{document}


As one can see the newgeometry is not well defined per page style. BTW I am not sure that I should define a geometry in a \fancypagestyle command.

Is there a way to do this properly?

TIA.

• I tried using \AtBeginShipoutNext{\loadgeometry{otherpage}} and similar stuff but no can do. I seriously wonder if changing geometry within a fancy header is really working. – renard Aug 18 '16 at 12:55
• I guess this tex.stackexchange.com/questions/65571 explains why changing geometry does not really work here. – renard Aug 18 '16 at 13:41

Well I didn't find the way to change the full geometry for a page style but I managed to change set the correct text height for a specific style as I wanted to do at the very beginning. I post the answer with the hope it will help other people.

% -*- latex -*-
\documentclass[a4paper]{article}

\usepackage{fontspec}
\usepackage{tikz}
\usepackage{calc}
\usepackage{fancyhdr}
\usepackage{blindtext}
\usepackage[absolute,overlay]{textpos}
\usepackage[showframe,margin=3cm]{geometry}
\usepackage{layout}
\makeatletter

\def\@@top@height{5cm}
\def\@@bottom@height{10cm}

\begin{textblock*}{\paperwidth}[0,0](0pt, 0pt)%
\begin{tikzpicture}%
\node [rectangle, draw=red,%
anchor=north,%
minimum width=\paperwidth-\pgflinewidth,%
minimum height=\@@top@height] (box) at (0.5\paperwidth,0mm) {};%
\end{tikzpicture}%
\end{textblock*}%
}%

\newcommand{\footer@first}{%
\begin{textblock*}{\paperwidth}[0,1](0pt, \paperheight)%
\begin{tikzpicture}%
\node [rectangle, draw=red,%
anchor=south,%
minimum width=\paperwidth-\pgflinewidth,%
minimum height=\@@bottom@height] (box) at (0.5\paperwidth,\paperheight) {};%
\end{tikzpicture}%
\end{textblock*}%
}%

\fancypagestyle{firstpage}{%
\fancyhf{}%
\lfoot{l} \cfoot{\footer@first} \rfoot{r}%
\renewcommand{\footrulewidth}{0pt}%
}%

%% \def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1#1}
%% \def\disp#1{\convertto{cm}{#1}}

%% Grabbed using the layout package
\def\@@bottom@skip{\dimexpr(\paperheight-\@@top@skip-\textheight)\relax}

\newcommand{\setfirstpage}{%
\def\myskp{\dimexpr(\@@top@height-\@@top@skip)\relax}%
\def\myfskp{\dimexpr(\@@bottom@height-\@@bottom@skip)\relax}%
\null\vspace{\myskp}%
\thispagestyle{firstpage}%
\enlargethispage{-\myfskp}%
}%

\fancypagestyle{otherpage}{%
\fancyhf{}
\lfoot{} \cfoot{} \rfoot{}
\renewcommand{\footrulewidth}{0pt}
}

\pagestyle{otherpage}

\makeatother

\begin{document}
%% \layout%
\setfirstpage%
\Blinddocument
%% \newpage
\pagestyle{fancy}
\blinddocument
\end{document}