2

I have different document classes with separate page geometry for some pages. Often the first page has a bigger header or footer.

The geometry package allows to store different geometries with \savegeometry and load them with \loadgeometry. This works well if the position of the geometry change is known in advance and defined through \newpage.

If the position of the geometry change in form of \newpage is not specified explicitly but should be after a certain page this is not straight forward like in the following example:

\documentclass{scrartcl}
\usepackage{blindtext}
%
% Select approach
%
% 1: Separate page geometry for first page
% 2: Same page geometry for all pages but adjusted header, \vspace at document begin and \enlargethispage for first page
% 3: No \loadgeometry, \afterpage or other geometry modifications
\newcounter{approach}
\setcounter{approach}{1}
%
% Select test case
%
% 1: Two sections and text [passed with approach 1 and 2]
% 2: One section and text [failed with approach 1 but passed with approach 2]
% 3: One section and itemize (split over two pages) surrounded by text [failed with approach 1 but passed with approach 2]
% And probably many other test cases that will fail with approach 1...
% Approach results per definition in a not desired result
\newcounter{testcase}
\setcounter{testcase}{1}
%
% Page geometry
%
\usepackage[showframe]{geometry}
%
\newgeometry{margin=20mm, headheight=\baselineskip, footskip=2\baselineskip, includehead, includefoot}
\savegeometry{standard}
%
\ifnum\value{approach}=1
    \newgeometry{margin=20mm, headheight=4\baselineskip, footskip=4\baselineskip, includehead, includefoot}
    \savegeometry{title}
\fi
%
\loadgeometry{standard}
\setlength{\footheight}{\baselineskip}
%
% Page styles
%
\usepackage{scrlayer-scrpage}
%
\newcommand*{\headerstandard}{Document (Approach \arabic{approach}, Test case \arabic{testcase}) \hfill Date}
\newcommand*{\headertitle}{\headerstandard\\ Item\\ Item\\ Item}
%
\setkomafont{pagenumber}{}
\newcommand*{\footerstandard}{Id \hfill Page \pagemark}
\newcommand*{\footertitle}{Info\\ Info\\ \footerstandard}
%
\defpagestyle{standard}{%
    {}{}{\headerstandard}%
}{%
    {}{}{\smash{\parbox[b]{\textwidth}{\footerstandard}}}%
}
\ifnum\ifnum\value{approach}=1 1\else\ifnum\value{approach}=3 1\else 0\fi\fi=1% \value{approach}=1 or \value{approach}=3
    \defpagestyle{title}{%
        {}{}{\headertitle}%
    }{%
        {}{}{\smash{\parbox[b]{\textwidth}{\footertitle}}}%
    }
\fi
\ifnum\value{approach}=2
    \defpagestyle{title}{%
        {}{}{\smash{\parbox[t]{\textwidth}{\headertitle}}}%
    }{%
        {}{}{\smash{\parbox[b]{\textwidth}{\footertitle}}}%
    }
\fi
\pagestyle{standard}
%
% Title page
%
\ifnum\value{approach}=1
    \usepackage{afterpage}
    %
    \renewcommand*{\maketitle}{%
        \thispagestyle{title}%
        \loadgeometry{title}%
        % ...
        \afterpage{\aftergroup\restoregeometry}%
    }
\fi
\ifnum\value{approach}=2
    \renewcommand*{\maketitle}{%
        \thispagestyle{title}%
        \vspace*{3\baselineskip}% Hack to simulate bigger head height
        \vspace{-\parskip}%
        \vspace{-\baselineskip}%
        \vspace{-1.535\baselineskip}% Hack two remove vertical space inserted if followed by section
        % ...
        \enlargethispage{-3\baselineskip}% Hack has not exact the same result as in approach 1
    }
\fi
\ifnum\value{approach}=3
    \renewcommand*{\maketitle}{%
        \thispagestyle{title}%
        % ...
    }
\fi
%
\AtBeginDocument{\maketitle}
%
% Document with test cases
%
\begin{document}
\ifnum\value{testcase}=1
    \section{Section}%
    \blindtext[6]%
    \section{Section}%
    \blindtext[3]%
\fi
\ifnum\value{testcase}=2
    \section{Section}%
    \blindtext[6]%
\fi
\ifnum\value{testcase}=3
    \section{Section}%
    \blindtext[5]%
    \blinditemize[7]%
    \blindtext[5]%
\fi
\end{document}

I found and tried one approach (called approach 1 in the example) using \afterpage from the afterpage package:

In this approach I used a separate page geometry for the first page but it fails in the last two of the three provided test cases. See the the not changed header height and in test case 3 also the overfull \vbox at the bottom on page two:

  1. 2. 3.

As an alternative I tried a second approach (called approach 2 in the example) with following references:

Thereby I kept the same page geometry for all pages but adjusted the header, added \vspace at page begin and \enlargethispage for the first page. This approach passes all test cases but comes with some problems in the code realization. I have to add one positive \vspace to respect the bigger head height and one negative \vspace to remove the vertical space that is added from the following section. Also the \enlargethispage results in a different text height as in approach 1:

  1. 2. 3.

Finally my questions are:

  1. Is it possible to make approach 1 with \afterpage working reliable?
  2. Is it in addition possible to compute the header and footer height automatically? In the example I defined them manually for one special case although it should work automatically for other cases.

Thank you very much for your help.

Update:

According to the comment from @David Carlisle I have added a third appraoch (called approach 3 in the updated example) coming from approach 1 with no \loadgeometry, \afterpage or other geometry modifications. But this approach results per definition in a not desired result. Thereby the text body height is constant and the header on the first page with a bigger height is not aligned at the top with the header on the second page. The footer also with a bigger height on the first page is aligned at the bottom with the footer on the second page but has a collision with the text body:

I also have to say that approach 2 comes also with no geometry changes but with some \vspace hacks.

  • 1
    it is not possible to change the horizontal width without forcing a page break so commands such as \newgeometry are usually restricted to a forced page break, you say the position of the change of size isn't known in advance but you seem to only use a different size in \maketitle on the first page? – David Carlisle Apr 1 '18 at 20:08
  • You are right. The position of the change of size (in the example after the first page) is known, but a \newpage is not specified explicitly. So I used \afterpage. I changed the explanation in the question. – Michael Apr 2 '18 at 7:49
  • If this is about customizing the title exclusively, the answer is different. – Johannes_B Apr 2 '18 at 8:26
  • 1
    @Michael but that is doing things backwards, you don't need to do anything at the page break, set up the main document page style then use \thispagestyle for the title-page, no need for afterpage or newgeometries etc, – David Carlisle Apr 2 '18 at 8:51
  • @David Carlisle I have updated the question according to your comment. – Michael Apr 2 '18 at 10:58
1

Here is a solution.

\documentclass{scrartcl}
\usepackage{mwe}
\newcounter{testcase}
\setcounter{testcase}{3}

\usepackage[showframe]{geometry}

\usepackage{etoolbox}

\makeatletter 
\def\Gmt@restore{}
\apptocmd\Gm@savelength{%
  \g@addto@macro\Gmt@restore{\global\expandafter\noexpand\expandafter\csname
  #1\endcsname\expandafter=\expandafter\the\csname #1\endcsname\relax}}{}{}
\patchcmd{\@outputpage}{\global \@colht \textheight}{\Gmt@restore@pkg \global \@colht \textheight}{}{} 
\makeatother

\newgeometry{margin=20mm, headheight=\baselineskip, footskip=2\baselineskip, includehead, includefoot}
\savegeometry{standard}

\makeatletter 
\edef\Gmt@restore@pkg{\Gmt@restore
 \global\expandafter\let\expandafter\noexpand\expandafter\Gmt@restore@pkg\noexpand\empty}%
\makeatother

\newgeometry{margin=20mm, headheight=4\baselineskip, footskip=4\baselineskip, includehead, includefoot}
\savegeometry{title}

\loadgeometry{standard}

\setlength{\footheight}{\baselineskip}
\usepackage{scrlayer-scrpage}
\newcommand*{\headerstandard}{Document (Approach 1, Test case 2) \hfill Date}
\newcommand*{\headertitle}{\headerstandard\\ Item\\ Item\\ Item}
\setkomafont{pagenumber}{}
\newcommand*{\footerstandard}{Id \hfill Page \pagemark}
\newcommand*{\footertitle}{Info\\ Info\\ \footerstandard}
\defpagestyle{standard}{%
    {}{}{\headerstandard}%
}{%
    {}{}{\smash{\parbox[b]{\textwidth}{\footerstandard}}}%
}
\defpagestyle{title}{%
        {}{}{\headertitle}%
    }{%
        {}{}{\smash{\parbox[b]{\textwidth}{\footertitle}}}%
    }
\pagestyle{standard}

\renewcommand*{\maketitle}{%
    \thispagestyle{title}%
    \loadgeometry{title}%%
}
\AtBeginDocument{\maketitle}
\begin{document}
\ifnum\value{testcase}=1
    \section{Section}%
    \blindtext[6]%
    \section{Section}%
    \blindtext[3]%
\fi
\ifnum\value{testcase}=2
    \section{Section}%
    \blindtext[6]%
\fi
\ifnum\value{testcase}=3
    \section{Section}%
    \blindtext[5]%
    \blinditemize[7]%
    \blindtext[5]%
\fi  
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.