LaTeX forum ⇒ Page LayoutConditional Variation of Head Height Topic is solved

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

Conditional Variation of Head Height  Topic is solved

Postby pjmiller_57 » Tue Feb 05, 2013 4:24 pm

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.
  1. \ifthenelse{\thepage=1}
  2. {\setlength\headheight{72pt}}
  3. {\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.
  1. \documentclass{article}
  2.  
  3. %% Margins %%
  4.  
  5. \setlength{\textwidth}{6.5in}
  6. \setlength{\oddsidemargin}{0in}
  7.  
  8. %%%% Packages %%%%
  9.  
  10. \usepackage[usenames,dvipsnames]{color}
  11. \usepackage[lastpage,user]{zref}
  12. \usepackage{fancyhdr}
  13. \usepackage{ifthen}
  14. \usepackage{parskip}
  15. \usepackage{enumitem}
  16. \usepackage{amssymb}
  17. \usepackage{soul}
  18. \usepackage[
  19. pdftex,
  20. pdfstartpage=1,
  21. pdfpagemode=UseOutlines,
  22. bookmarks,
  23. bookmarksopen,
  24. pdfstartview=Fit,
  25. pdfview=Fit,
  26. colorlinks,
  27. linktocpage,
  28. linkcolor=blue,
  29. citecolor=blue,
  30. pagebackref=true]
  31. {hyperref}
  32.  
  33. %%%% Headers/Footers %%%%
  34.  
  35. \setlength\headheight{72pt}
  36. \renewcommand\headrule{{\color{BrickRed} \hrule height 1pt width\headwidth}}
  37. \pagestyle{fancy}
  38. \definecolor{light-gray}{gray}{0.65}
  39.  
  40. \lhead{\ifthenelse{\thepage = 1}
  41. {\textcolor{light-gray}{My Name} \\ [4ex] \vspace{36pt} \textcolor{light-gray}{Telephone Number}}
  42. {\textcolor{light-gray}{My Name}}}
  43.  
  44. \rhead{\ifthenelse{\thepage = 1}
  45. {\textcolor{light-gray}{Page \thepage \ of \zpageref{LastPage}} \\ [4ex] \vspace{36pt}
  46. \textcolor{light-gray}{Email Address}}
  47. {\textcolor{light-gray}{Page \thepage \ of \zpageref{LastPage}}}}
  48.  
  49. \chead{\ifthenelse{\thepage = 1}
  50. {{\large\bfseries MY NAME, TITLE} \\
  51. Address Line 2 \\
  52. Address Line 3 \\
  53. Address Line 4 \\ [1ex]}
  54. {}}
  55.  
  56. \cfoot{}
  57.  
  58. %%%% Bullet Points %%%%
  59.  
  60. \renewcommand\labelitemi{$\square$}
  61.  
  62. %%%% Main Document %%%%
  63.  
  64. \begin{document}
  65.  
  66. Some text
  67.  
  68. \newpage
  69.  
  70. More text
  71.  
  72. \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

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

Postby cgnieder » Tue Feb 05, 2013 10:02 pm

The conditional
  1. \ifthenelse{\thepage=1}
  2. {\setlength\headheight{72pt}}
  3. {\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
  1. \ifthenelse{\value{page}=1}
  2. {\setlength\headheight{72pt}}
  3. {\setlength\headheight{0pt}}

You actually don't need ifthen for this test, though. It is easy to define one yourself:
  1. % make @ a letter:
  2. \makeatletter
  3. \newcommand\ifpageone{%
  4. \ifnum\value{page}=1
  5. % use only the first of two following arguments,
  6. % \expandafter is necessary to let \@firstoftwo
  7. % only act after \fi has been expanded:
  8. \expandafter\@firstoftwo
  9. \else
  10. % use only the second of two following arguments
  11. \expandafter\@secondoftwo
  12. \fi}
  13. \makeatother


You can use this to set a new head height for example by setting it in the empty footer you're using:
  1. \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:
  1. Package Fancyhdr Warning: \headheight is too small (72.0pt):
  2. Make it at least 76.76665pt.


Regards.
site moderator & package author

pjmiller_57
Posts: 20
Joined: Tue Nov 08, 2011 4:37 pm

Postby pjmiller_57 » Wed Feb 06, 2013 4:44 pm

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:
  1. ! Undefined control sequence.
  2. \f@ncyocf ->\iffirstpage
  3. {\global \setlength \headheight {78pt}}\strut
  4. l.87 \newpage
  5.  
  6. [1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}]
  7. ! Undefined control sequence.
  8. \f@ncyocf ->\iffirstpage
  9. {\global \setlength \headheight {78pt}}\strut
  10. 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.
  1. \begin{document}
  2.  
  3. Some text
  4.  
  5. \newpage
  6. \setlength\headheight{14pt}
  7.  
  8. More text
  9.  
  10. \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: 1993
Joined: Sat Apr 16, 2011 7:27 pm

Postby cgnieder » Thu Feb 07, 2013 12:25 am

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:

  1. % make @ a letter:
  2. \makeatletter
  3. \newcommand\ifpageone{%
  4. \ifnum\value{page}=1
  5. % use only the first of two following arguments,
  6. % \expandafter is necessary to let \@firstoftwo
  7. % only act after \fi has been expanded:
  8. \expandafter\@firstoftwo
  9. \else
  10. % use only the second of two following arguments
  11. \expandafter\@secondoftwo
  12. \fi}
  13. % make @ other again:
  14. \makeatother


and then later:
  1. \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

Postby pjmiller_57 » Thu Feb 07, 2013 3:38 pm

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


Return to “Page Layout”

Who is online

Users browsing this forum: No registered users and 6 guests