[Tex/LaTex] Keeping header “on top” in landscape mode

fancyhdrheader-footerlandscape

When using pdflscape and fancyhdr, the headers also get rotated. Whilst it's a normal behaviour in most cases (especially when the document has to be printed), I am dealing with a report which will never be printed, so this default behaviour is more annoying.

Here is a MWE of a document dealing with portrait and landscape pages:

\documentclass[a4paper, 10pt]{article}
\usepackage{graphicx}
\usepackage{pdflscape}
\usepackage{fancyhdr}
\usepackage{lipsum}
\usepackage[left=2cm, right=2cm, bottom=2cm, top=2cm]{geometry}
\setlength{\headheight}{32pt}

\lhead{\includegraphics[scale=.5]{tt}\\Foo}
\rhead{\includegraphics[scale=.5]{tt}\\\thepage}
\cfoot{}

\fancypagestyle{customlandscape}{%
\fancyhf{}
\newgeometry{left=1cm, right=1cm, bottom=1cm, top=1cm}
% ??
}

\pagestyle{fancy}
\begin{document}

\lipsum[1-10]
\newpage

\pagestyle{customlandscape}
\begin{landscape}
    Here is the landscaped content, the header of this page should look like the rest...
\end{landscape}


\newpage
\pagestyle{plain}
\lipsum[1-10]
\end{document}

Note : you will have to provide 2 images called tt for it to compile, sorry for that. So, if we look a bit closer, the header of the portrait pages look like this :

portrait page

How can I manage for my landscape pages to look like this ? :
landscape page

Currently, by compiling my MWE, you'll notice that the header is on the right on the landscape page.

PS : There is another problem in this MWE, after switching back to portrait, it seems I'm not able to restore the default pagestyle (with the images in the header).

Best Answer

Without lscap package you change page format to landscape by

\newpage

\paperwidth=\pdfpageheight
\paperheight=\pdfpagewidth
\pdfpageheight=\paperheight
\pdfpagewidth=\paperwidth

here the problem was to adapt the headwidth length

\documentclass[a4paper,10pt]{article}
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage{lipsum}
\usepackage[left=2cm, right=2cm, bottom=2cm, top=2cm]{geometry}
\setlength{\headheight}{32pt}

\lhead{tt\\Foo}
\rhead{vv\\\thepage}
\cfoot{}

\pagestyle{fancy}
\begin{document}

\lipsum[1-10]

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

%landsape
\pdfpageheight=\paperwidth
\pdfpagewidth=\paperheight
\paperwidth=\pdfpagewidth
\paperheight=\pdfpageheight

%text width and height
\begingroup 
\vsize=.8\pdfpageheight % do what you like
\hsize=.8\pdfpagewidth  % do what you like
\textwidth=\hsize
\textheight=\vsize
% you can specify the margins

\lipsum[1]

\endgroup
\newpage

\paperwidth=\pdfpageheight
\paperheight=\pdfpagewidth
\pdfpageheight=\paperheight
\pdfpagewidth=\paperwidth


\pagestyle{plain}
\lipsum[1-10]
\end{document}

edit for more pagestyle{fancy}

\documentclass[a4paper,10pt]{article}
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage{lipsum}
\usepackage[left=2cm, right=2cm, bottom=2cm, top=2cm]{geometry}
\setlength{\headheight}{32pt}

\lhead{tt\\Foo}
\rhead{vv\\\thepage}
\cfoot{}

\pagestyle{fancy}
\begin{document}

\lipsum[1-10]

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

%landscape
\pdfpageheight=\paperwidth
\pdfpagewidth=\paperheight
\paperwidth=\pdfpagewidth
\paperheight=\pdfpageheight

%text width and height
\begingroup 
\vsize=.8\pdfpageheight % do what you like
\hsize=.8\pdfpagewidth  % do what you like
\textwidth=\hsize
\textheight=\vsize
% you can specify the margins

\lipsum[1-10]

\endgroup
\newpage
\addtolength\headwidth{-1\mtL}
\paperwidth=\pdfpageheight
\paperheight=\pdfpagewidth
\pdfpageheight=\paperheight
\pdfpagewidth=\paperwidth


\lipsum[1-20]

\pagestyle{plain}
\lipsum[1-10]
\end{document}