1

What I would like to achieve is a (fairly high) header in the first page and alternate headers with different heights for even and odd in the subsequent pages.

I tried Werner's solution from this page, but I have the problem that, like I said, the subsequent headers are of different heights in even and odd pages. Here is my document, although it's not really minimal

\documentclass[11pt,a4paper,twoside]{article}

\usepackage[ngerman]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{xcolor}
\usepackage{xltabular,multirow}
\usepackage{graphicx}
\usepackage{siunitx}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage[bottom]{footmisc}
\usepackage{blindtext}
\usepackage[left=2.00cm, right=3.00cm, top=2cm, bottom=2.0cm, headsep=1.50cm]{geometry} % Tried with other bottom margins. The footer disappears at the bottom of the page.
\usepackage{hyperref}

\hypersetup{
    colorlinks=true,
    linkcolor={red!60!black},
    citecolor={blue!50!black},
    urlcolor={blue!80!black},
}

\fancyhf{}

\sisetup{output-decimal-marker={,},group-minimum-digits=4}

% Header for first page
\fancypagestyle{ersteabph}{
    \fancyhf{}% Clear header/footer
        \renewcommand{\headrulewidth}{0pt}% Remove header rule
        \setlength{\headheight}{160pt}
        \fancyhead[C]{\begin{xltabular}{\textwidth}{p{9cm} p{8mm} X}
                \raisebox{-\height}[0pt][0pt]{\textbf{{\Large Mybeautifulschool}}} & & \raisebox{-\height}[0pt][0pt]{\textbf{\Large Arbeitsblatt Physik}}\newline \raisebox{-\height}[0pt][0pt]{\textbf{\Large Elektrisches Feld}}\\
                Name: & & GPH1\\
                Datum: & & Seite \thepage/\pageref{LastPage}
            \end{xltabular}\rule{\linewidth}{.4pt}
        }
}

% Header for subsequent pages
\fancypagestyle{abph}{
    \fancyhf{}
    \setlength{\headheight}{120pt}
    \renewcommand{\headrulewidth}{0pt}
    \fancyhead[CO]{\begin{xltabular}{\linewidth}{p{9cm} p{8mm} X}
            \raisebox{-\height}[0pt][0pt]{\textbf{{\Large Mybeautifulschool}}} & & \raisebox{-\height}[0pt][0pt]{\textbf{\Large Arbeitsblatt Physik}}\newline \raisebox{-\height}[0pt][0pt]{\textbf{\Large Elektrisches Feld}}\\
            & & Seite \thepage/\pageref{LastPage}
            \end{xltabular}}
    \fancyhead[RE]{\raisebox{-\height}[0pt][0pt]{\thepage/\pageref{LastPage}}}
    
}

\pagestyle{abph}

\setlength{\parindent}{0pt}

\renewcommand{\arraystretch}{2.50}

\author{Myself}

\begin{document}
    
    %\vspace*{3cm}
    
    \section*{Versuch: die Energie des Kondensators.}
    
    \thispagestyle{ersteabph}
    
    \begin{minipage}[b]{0.5\textwidth}
        \includegraphics[width=0.98\linewidth]{example-image-a}
    \end{minipage}\hspace{0.1cm}%
    \begin{minipage}[b]{0.5\textwidth}
        \includegraphics[width=0.98\linewidth]{example-image-b}
    \end{minipage}
    
    \section*{Versuchsbeschreibung}
     
    In diesem Versuch wollen wir die Abhängigkeit der in einem Kondensator gespeicherten Energie von der Spannung und von der Kapazität untersuchen. Dafür laden wir einen Kondensator zu einer gewissen Spannung und lassen ihn auf einem elektrischen Motor entladen. Am Motor hängt ein Gewicht mit gegebener Masse $m$. Der Motor kommt zum Laufen und hebt das Gewicht zu einer gewissen Höhe $h$. Die potentielle Energie $E_{\mathrm{pot}}$, die das Gewicht beim Anheben gewinnt, ist zur gespeicherten Energie im Kondensator proportional.\\
    
    Der Versuch besteht aus zwei Teilen: zuerst wird ein Kondensator auf verschiedene Spannungen geladen. Die angegebene Spannung und die durch das Gewicht erreichte Höhe werden in eine Tabelle eingetragen. Dann werden Kondensatoren unterschiedlicher Kapazitäten auf die gleiche Spannung geladen und wir notieren wieder in eine Tabelle die Kapazität und die erreichte Höhe.\\ 
    
    Beim Versuch ist auf folgendes zu achten:
    
    \begin{itemize}
        \item Der Motor hat leider keine Bremse, sodass das Gewicht wieder nach unten kommt, wenn es nicht angehalten wird. Warte, bis das Gewicht die maximale Höhe erreicht und halte es dann an dieser Stelle, sodass die Höhenmessung durchgeführt werden kann.
        \item Beim ersten Teil wählen wir einen Kondensator mit Kapazität \SI{10}{\milli\farad} und die Ladespannungen \qtylist{4;6;8;10}{V}. Überschreitet bitte nicht die Spannung \SI{10}{V}, da sonst das Gewicht zu hoch kommt und am Ösen klemmt (außerdem würde das Ergebnis dadurch gefälscht).
        \item Beim zweiten Teil wählen wir Kondensatoren mit Kapazität \qtylist{4.7;5;6.7;10}{\milli\farad} und die Spannung von \SI{9}{V}. Auch diese Werte wurden so gewählt, dass die Messdaten eine gute Auswertung ermöglichen.
    \end{itemize}

    \Blindtext[3][3]

    \section*{Durchführung}
    
\end{document}

I also tried without adding the \setlength{\headheight} (suggested by the warnings), but leaving the \vspace*{} at the beginning of the document per Werner's answer in the linked question. That solution, however, has the problem that the header of the odd pages is too high and ends up in the text. I can't keep adding \vspace*{}s because I don't know in advance where a page ends (or at least it's not elegant).

Another thing I tried is a solution with \usepackage[pagestyles]{titlesec}, but that didn't work either (I can paste my not-so-minimal example of that if you want me to).

So basically:

  • if I add the \setlength{\headheight}, I have the problem that the top margins don't all look the same and the bottom margin is not right.
  • if I use the \vspace*{}s, then the bottom margin is right, but I have to insert it manually and change it every time I add or remove something in the text (unless there is an automated way to do it that I don't know).

I'm ready to do away with the xltabulars in the headers, if there is another easy way to line up the lines of the header properly.

I am out of ideas to try and open to suggestions.

EDIT:

By the way, my question sounds exactly like this one, but that one is closed and has no answer.

2 Answers 2

4

You can adjust the page layout using the shipout/after hook.

\documentclass{article}

\usepackage{showframe}

\makeatletter
  \newdimen\evenoddheadheightdiff
  \def\addtoheadheight#1{%
    \@tempdima=#1\relax
    \global\advance\headheight\@tempdima
    \global\advance\textheight-\@tempdima
  }
  \AddToHook{shipout/after}{%
    \ifodd\thepage
      \addtoheadheight{\evenoddheadheightdiff}%
    \else
      \addtoheadheight{-\evenoddheadheightdiff}%
    \fi
  }
\makeatother

\evenoddheadheightdiff=10ex\relax

\begin{document}

\strut\clearpage
\strut\clearpage
\strut\clearpage
\strut\clearpage
\strut\clearpage
\strut\clearpage

\end{document}

Different head heights

Then you just have to plug in the desired headers using fancyhdr or scrlayer-scrpage or the like. For example:

\documentclass[twoside]{article}

\usepackage{fancyhdr}

\fancypagestyle{first}{
  \fancyhead[C]{\huge Something tall for the first page}
}
\fancypagestyle{default}{
    \fancyhead[OC]{\huge Something very tall}
    \fancyhead[ER]{Something not so tall}
}
\pagestyle{default}

\makeatletter
  \newdimen\evenoddheadheightdiff
  \def\addtoheadheight#1{%
    \@tempdima=#1\relax
    \global\advance\headheight\@tempdima
    \global\advance\textheight-\@tempdima
  }
  \AddToHook{shipout/after}{%
    \ifodd\thepage
      \addtoheadheight{\evenoddheadheightdiff}%
    \else
      \addtoheadheight{-\evenoddheadheightdiff}%
    \fi
  }
\makeatother

\evenoddheadheightdiff=-10ex\relax
\addtoheadheight{-\evenoddheadheightdiff}

\begin{document}

\thispagestyle{first}
\strut\clearpage
\strut\clearpage
\strut\clearpage
\strut\clearpage
\strut\clearpage
\strut\clearpage

\end{document}

An example with headers


You can easily extend this to have a different \headheight on the first page (or any other page, for that matter). I would recommend changing the interface if you do this a lot, depending on your application. This answer was just meant to show you the way. Here's one way to do it with the given interface.

\documentclass[twoside]{article}

\usepackage{fancyhdr}
\usepackage{showframe}

\fancypagestyle{first}{
  \fancyhead[C]{\huge Something tall for the first page}
}
\fancypagestyle{default}{
    \fancyhead[OC]{\huge Something very tall}
    \fancyhead[ER]{Something not so tall}
}
\pagestyle{default}

\makeatletter
  \newdimen\evenoddheadheightdiff
  \def\addtoheadheight#1{%
    \@tempdima=#1\relax
    \global\advance\headheight\@tempdima
    \global\advance\textheight-\@tempdima
  }
  \AddToHook{shipout/after}{%
    \ifodd\thepage
      \addtoheadheight{\evenoddheadheightdiff}%
    \else
      \addtoheadheight{-\evenoddheadheightdiff}%
    \fi
  }
\makeatother

\evenoddheadheightdiff=-10ex\relax

\newdimen\evenfirstpageheightdiff
\evenfirstpageheightdiff=-30ex\relax

\addtoheadheight{-\evenfirstpageheightdiff}
\AddToHookNext{shipout/after}{%
  \addtoheadheight{\evenfirstpageheightdiff}%
  \addtoheadheight{-\evenoddheadheightdiff}
}

\begin{document}

\thispagestyle{first}
\strut\clearpage
\strut\clearpage
\strut\clearpage
\strut\clearpage
\strut\clearpage
\strut\clearpage

\end{document}

separate height for first page

4
  • OK, so this seems to be a very tricky question indeed. This solution works fine as long as the header is only one line. As soon as I have four (I only tried it in the odd pages so far), then the spacing is no longer correct and the header ends up overlapping the text. Maybe I can play with the value of \evenoddheadheightdiff, but I'm scared to try what happens if I have a tabular there...
    – Andyc
    Jan 10, 2022 at 23:38
  • 1
    @Andyc well obviously you have to set \evenoddheadheightdiff to a value that matches the actual heights of your headers. I just picked (-)10ex at random for my MWE. If the content of your header is taller than \headheight, it will overflow (and you will get a warning telling you about it). You'll have to calculate/measure/guess the correct value for your headers. Use showframe to see the bounding box of your headers when picking the value.
    – schtandard
    Jan 11, 2022 at 10:37
  • Yes, I figured that, and I got it right in the meantime. Thank you for pointing it out. I think it would be nice though, if there were two \headheightdiffs: one for the first page and one for the subsequent pages. This just in case the first header is much taller than the one for the other odd pages, which would end up putting a lot of white space before the odd page header starting from page 3 and it wouldn't look nice.
    – Andyc
    Jan 11, 2022 at 19:00
  • @Andyc see my edit.
    – schtandard
    Jan 11, 2022 at 22:22
1

For the first page the simplest solution is usually to have an empty header and then put the "header" as normal text just at the beginning of the document. This saves the hassle of having a different \headheight.

Changing \headheight mid-document is tricky business sowieso (anyway), and trying to have the \textheight being changed accordingly is even trickier. So my solution doesn't do that but uses a fixed \headheight. On even pages where a tall header is placed, I first put the header in a \raisebox to trick fancyhdr into believing that it isn't tall, so that it will not complain, and to position it properly. And moreover on even pages I add some additional vertical space on the top of the text (i.e. just under the header) with \vspace (or a zero-width \rule). I do this with \afterpage, which is repeated on each page, and just checks if the page number is even to put that space there).

\documentclass[twoside]{article}

\usepackage{lipsum}
\usepackage{fancyhdr}
\usepackage{afterpage}
\usepackage{graphicx}

\fancypagestyle{firstpage}{%
  \fancyhead{}
}

\pagestyle{fancy}
\setlength{\headheight}{16pt}
\fancyhead[LE]{\raisebox{-10pt}[0pt][0pt]{\parbox[t]{\headwidth}{\Huge EVEN PAGE HEADER\\SECOND LINE}}}
\fancyhead[LO]{ODD PAGE HEADER}
\fancyfoot[C]{\thepage}
\renewcommand{\headrulewidth}{0pt}

\newcommand{\nextpage}{%
  \ifodd\value{page}\else\vspace*{30pt}\fi
  % Other possibility:
  % \ifodd\value{page}\else\rule{0pt}{30pt}\fi
  \afterpage{\nextpage}
}

\begin{document}

\thispagestyle{firstpage}
\afterpage{\nextpage}
\includegraphics[height=100pt]{example-image}
\vspace{20pt}

\lipsum[1-4]
\begin{figure}
  \centering

  \includegraphics{example-image-a}
\vspace{5cm}
  \caption{This is the figure}
\end{figure}

\lipsum[1-20]

\end{document}

enter image description here enter image description here

2
  • I tried this solution, nice indeed, but I get what seems to be an extra page at the end of the document with only the header and page number, but no text.
    – Andyc
    Jan 10, 2022 at 23:18
  • That is probably the extra \vspace that the last \afterpage puts there if the last page is an odd page. Sorry about that. That would need a test in the \afterpage to see if we are at the last page. In my example I didn't have that because my last page was an even page. I'll try to think of a clean solution for this. Jan 10, 2022 at 23:26

You must log in to answer this question.

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