Page LayoutConditional Variation of Head Height

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
pjmiller_57
Posts: 20
Joined: Tue Nov 08, 2011 4:37 pm

Conditional Variation of Head Height

Post by pjmiller_57 »

Hello All,

Am trying to use \ifthenelse from the ifthen package to vary the size of \headheight within a document. Would like this to be larger for the first page and then smaller (possibly at its defaults size) for each subsequent page.

I had thought I could accomplish this using code like the following.

Code: Select all

\ifthenelse{\thepage=1}
  {\setlength\headheight{72pt}}
  {\setlength\headheight{0pt}}
Unfortunately, that's not working for some reason. Below is some code that, for the most part, replicates what I'm trying to do. Can anyone help me understand why the snippet of code above is not working, as well as what the code should look like? In the code below, there are some \iftenelse statements that do work, but I found I wasn't able to use the same approach to producing variable head heights.

Code: Select all

\documentclass{article}

%% Margins %%

\setlength{\textwidth}{6.5in}
\setlength{\oddsidemargin}{0in}

%%%% Packages %%%%

\usepackage[usenames,dvipsnames]{color} 
\usepackage[lastpage,user]{zref}
\usepackage{fancyhdr}
\usepackage{ifthen}
\usepackage{parskip}
\usepackage{enumitem}
\usepackage{amssymb}
\usepackage{soul}
\usepackage[
pdftex,
pdfstartpage=1,
pdfpagemode=UseOutlines,
bookmarks,
bookmarksopen,
pdfstartview=Fit,
pdfview=Fit,
colorlinks,
linktocpage,
linkcolor=blue,
citecolor=blue,
pagebackref=true]
{hyperref}

%%%% Headers/Footers %%%%

\setlength\headheight{72pt}
\renewcommand\headrule{{\color{BrickRed} \hrule height 1pt width\headwidth}}
\pagestyle{fancy}
\definecolor{light-gray}{gray}{0.65}

\lhead{\ifthenelse{\thepage = 1}
                  {\textcolor{light-gray}{My Name} \\ [4ex] \vspace{36pt} \textcolor{light-gray}{Telephone Number}}
                  {\textcolor{light-gray}{My Name}}}

\rhead{\ifthenelse{\thepage = 1}
                  {\textcolor{light-gray}{Page \thepage \ of \zpageref{LastPage}} \\ [4ex] \vspace{36pt}
                   \textcolor{light-gray}{Email Address}}
                  {\textcolor{light-gray}{Page \thepage \ of \zpageref{LastPage}}}}

\chead{\ifthenelse{\thepage = 1}
                  {{\large\bfseries MY NAME, TITLE} \\
                   Address Line 2 \\
                   Address Line 3 \\
                   Address Line 4 \\ [1ex]}
                  {}}

\cfoot{}

%%%% Bullet Points %%%%

\renewcommand\labelitemi{$\square$}

%%%% Main Document %%%%

\begin{document}

Some text

\newpage

More text

\end{document}
I'd be interested in knowing if those \ifthenelse statements are well-specified, or if they are ugly but still functional.


Thanks,
Paul

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Conditional Variation of Head Height

Post by cgnieder »

The conditional

Code: Select all

\ifthenelse{\thepage=1}
      {\setlength\headheight{72pt}}
      {\setlength\headheight{0pt}}
will be executed at the place you call it in the document and set the head height once depending on the current definition of \thepage. This leads me to a possible problem with your conditional: \thepage might be “i” when you use \pagenumbering{roman} or even something like -- page~1 --. A safer way would be

Code: Select all

\ifthenelse{\value{page}=1}
      {\setlength\headheight{72pt}}
      {\setlength\headheight{0pt}}
You actually don't need ifthen for this test, though. It is easy to define one yourself:

Code: Select all

% make @ a letter:
\makeatletter
\newcommand\ifpageone{%
  \ifnum\value{page}=1
    % use only the first of two following arguments,
    % \expandafter is necessary to let \@firstoftwo
    % only act after \fi has been expanded:
    \expandafter\@firstoftwo
  \else
    % use only the second of two following arguments
    \expandafter\@secondoftwo
  \fi}
\makeatother
You can use this to set a new head height for example by setting it in the empty footer you're using:

Code: Select all

\cfoot{\iffirstpage{\global\setlength\headheight{13pt}}}
Please note that \global is necessary here since otherwise this would change the length only within the scope of the footer. All in all this feels a bit hacky but it works.

A few additional remarks to your code: I would set the page geometry with the package geometry rather than set things by hand. I also wouldn't call the driver pdftex as »hyperref« package option. It is quite able to detect the right driver itself. The head height 72pt is too less for the current setting. I get the following warning:

Code: Select all

Package Fancyhdr Warning: \headheight is too small (72.0pt): 
 Make it at least 76.76665pt.
Regards.
site moderator & package author
pjmiller_57
Posts: 20
Joined: Tue Nov 08, 2011 4:37 pm

Conditional Variation of Head Height

Post by pjmiller_57 »

Hi Clemens,

Thanks for your response. I tried the two pieces of code you supplied. Unfortunately, neither of them seemed to work for me.

The revised \ifthenelse statement seemed to set the header at page 1, much like you suggested my original \ifthenelse statement was doing.

The code that started with \makeatletter produced an error having to do with an undefined control sequence. Before testing this code, I commented out the \setlength\headheight{72pt} in the preamble as well as the empty \cfoot{}. Then immediately below the commented \cfoot{}, I pasted the \makeatletter code and the new line for \cfoot.

The error looked like this:

Code: Select all

! Undefined control sequence.
\f@ncyocf ->\iffirstpage 
                         {\global \setlength \headheight {78pt}}\strut 
l.87 \newpage
             
[1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}]
! Undefined control sequence.
\f@ncyocf ->\iffirstpage 
                         {\global \setlength \headheight {78pt}}\strut 
l.91 \end{document}
Found what looks like a simple solution to my problem. Because I'll likely to set page breaks in my document manually, I can just change the \headheight after the first \newpage like below.

Code: Select all

\begin{document}

Some text

\newpage
\setlength\headheight{14pt}

More text

\end{document}
Might not be the most elegant solution generally, but should work well for my specific case.

Have noted your comments about the pdftex option in the hyperref package and the need to increase the head height from 72pt. Also took a quick look at the geometry package. This looks very useful. Will plan to learn the details as soon as I have time.

Appreciate your help with this. If you can help me understand why I wasn't able to implement your proposed solution, that would be great. If not though, I think I can make do with the very simple solution I proposed above.


Thanks,
Paul
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Conditional Variation of Head Height

Post by cgnieder »

The error is my fault: when I played with the code first I called the macro \iffirstpage but later decided to call it \ifpageone. I simply forgot to change the code I posted. I should read:

Code: Select all

% make @ a letter:
\makeatletter
\newcommand\ifpageone{%
  \ifnum\value{page}=1
    % use only the first of two following arguments,
    % \expandafter is necessary to let \@firstoftwo
    % only act after \fi has been expanded:
    \expandafter\@firstoftwo
  \else
    % use only the second of two following arguments
    \expandafter\@secondoftwo
  \fi}
% make @ other again:
\makeatother
and then later:

Code: Select all

\cfoot{\ifpageone{\global\setlength\headheight{13pt}}}
If your manually setting the page break then setting a new head height manually is of course easier (and less hackish...)

Regards
site moderator & package author
pjmiller_57
Posts: 20
Joined: Tue Nov 08, 2011 4:37 pm

Re: Conditional Variation of Head Height

Post by pjmiller_57 »

Hi Clemens,

Thanks for your most recent response. The code works now. I'll leave this question open for another day or so in case anyone else has any other solutions they'd like to share.

Thanks again.

Paul
Post Reply