2

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
\newcommand{\header@first}{%
  \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{}
  \lhead{} \chead{\header@first} \rhead{}
  \lfoot{} \cfoot{\footer@first} \rfoot{}
  \renewcommand{\headrulewidth}{0pt}
  \renewcommand{\footrulewidth}{0pt}
  \newgeometry{top=5cm, bottom=8cm, hmargin={1cm, 1cm}, textwidth=19cm}
}

\fancypagestyle{otherpage}{%
  \fancyhf{}
  \lhead{} \chead{} \rhead{}
  \lfoot{} \cfoot{} \rfoot{}
  \renewcommand{\headrulewidth}{0pt}
  \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}

enter image description here enter image description here

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.

2
  • 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
    Commented Aug 18, 2016 at 12:55
  • I guess this tex.stackexchange.com/questions/65571 explains why changing geometry does not really work here.
    – renard
    Commented Aug 18, 2016 at 13:41

1 Answer 1

1

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}

\newcommand{\header@first}{%
  \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{}%
  \lhead{l} \chead{\header@first} \rhead{r}%
  \lfoot{l} \cfoot{\footer@first} \rfoot{r}%
  \renewcommand{\headrulewidth}{0pt}%
  \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\@@top@skip{\dimexpr(1in+\voffset+\topmargin+\headheight+\headsep)\relax}
\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{}
  \lhead{} \chead{} \rhead{}
  \lfoot{} \cfoot{} \rfoot{}
  \renewcommand{\headrulewidth}{0pt}
  \renewcommand{\footrulewidth}{0pt}
}

\pagestyle{otherpage}

\makeatother

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

enter image description hereenter image description here

You must log in to answer this question.

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