[Tex/LaTex] fancyhdr in landscape mode: how to align header and footer correctly

fancyhdrgeometryheader-footerlandscape

My document has the following 2 features:

  1. Some page(s) are displayed in landscape mode and multi-columns, whereas the rest are in portrait
  2. Each page has a "fancy" header/footer

I found a neat solution to address 1 in a related post here, which involves changing the dimensions of the page to make it landscape. However, the problem is that given the geometry of my page, the header/footer on the landscape page looks off (i.e. footer not centered and running off the page, header not in the very upper top right). And no matter how much I played with \setlength or \vsize, \hsize, my landscape page still looks like this (see below).

How can I make sure my multicolumns are centered, my footer is centered at the bottom, and my header is in the very upper right hand corner?

enter image description here

\documentclass{article}
\usepackage{fancyhdr}
\usepackage{lipsum}
\usepackage{multicol}
\usepackage{geometry}
 \geometry{
 left=0.5in, right=0.5in, top=0.6in, bottom=1.25in
 }
\begin{document}

\pagestyle{fancy}
\fancyfoot[C]{my footer}
\fancyhead[R]{my header\\ \\ Page \thepage}


\lipsum[1]
\newpage

\newlength{\mtL}
\setlength{\mtL}{.8\paperheight}% the next hsize
\addtolength\mtL{-\headwidth}
\newpage
\addtolength\headwidth{\mtL}

\paperwidth=\pdfpageheight
\paperheight=\pdfpagewidth
\pdfpageheight=\paperheight
\pdfpagewidth=\paperwidth
\headwidth=\textheight
\begingroup 
\vsize=.8\pdfpageheight % do what you like
\hsize=.8\pdfpagewidth  % do what you like
\textwidth=\hsize
\textheight=\vsize
\begin{multicols}{2}
\lipsum[1-10]
\end{multicols}
\endgroup



\newpage
\paperwidth=\pdfpageheight
\paperheight=\pdfpagewidth
\pdfpageheight=\paperheight
\pdfpagewidth=\paperwidth
\headwidth=\textwidth

\lipsum[3]
\end{document}

Best Answer

Here is a suggestion using a combination of packages typearea and geometry:

\documentclass{article}
\usepackage{fancyhdr}
\usepackage{lipsum}
\usepackage{multicol}

\usepackage[usegeometry]{typearea}% before geometry!
\usepackage{geometry}
\geometry{
  left=0.5in, right=0.5in, top=0.6in, bottom=1.25in,headheight=23pt,includehead
}
\newcommand*{\useportrait}{%
  \clearpage
  \KOMAoptions{paper=portrait,DIV=current}%switch to portrait
  \newgeometry{% geometry settings for portrait
    left=0.5in, right=0.5in, top=0.6in, bottom=1.25in,headheight=23pt,includehead
  }%
  \fancyhfoffset{0pt}% <- recalculate head and foot width for fancyhdr
}
\newcommand*{\uselandscape}{%
  \clearpage
  \KOMAoptions{paper=landscape,DIV=current}%switch to landscape
  \newgeometry{% geometry settings for landscap
    left=0.5in, right=0.5in, top=0.6in, bottom=1.25in,headheight=23pt,includehead
  }%
  \fancyhfoffset{0pt}% recalculate head and foot width for fancyhdr
}

\pagestyle{fancy}
\fancyfoot[C]{my footer}
\fancyhead[R]{my header\\ Page \thepage}

\begin{document}
\lipsum[1]

\uselandscape
\begin{multicols}{2}
\lipsum[1-10]
\end{multicols}

\useportrait
\lipsum[3]
\end{document}

Result:

enter image description here

Related Question