8

I have used the fancyhdr package to customize the header of my pages. However, when I want a page to be landscape (by using the pdflscape package), it does produce a page in landscape orientation, but the header is on the right side of the page (see attached picture). I am using the article document class. Here is my code:

\pagestyle{fancy}
\fancyhf{}
\renewcommand{\sectionmark}[1]{\markboth{\thesection.\ #1}{}}
\lhead{\leftmark} %section 
\rhead{\thepage} %page number


\newpage
\begin{landscape}
\subsection{asdasd}
\end{landscape}

How can I move the header to the top of the page?

this is the result which make me sad

@grimler solved the problem with rotation of the header but now the pictures cannot be in the center and the margines of the landscape page is not symmetric

\documentclass[12pt]{article}               
\usepackage[left=2cm, right=2cm, bottom=2cm, top=3cm]{geometry}
\geometry{letterpaper} 

\begin{document}

\section{Nima}
\subsection{aaaaaaaaaaaaa}
\lipsum[1]
\begin{figure}[H]
  \centering
  \includegraphics[width=0.5\linewidth]{male.jpg}
  \caption{Example of a forward looking algorithm that identifies peaks \&     troughs.}
  \label{fig:old_zig_1}
\end{figure}


\newpage


\paperwidth=\pdfpageheight
\paperheight=\pdfpagewidth
\pdfpageheight=\paperheight
\pdfpagewidth=\paperwidth
\headwidth=\textheight



\begingroup 
\vsize=\textwidth
\hsize=\textheight


\begin{figure}[H]
\begin{center}
  \includegraphics[width=0.4\linewidth]{male.jpg}
\end{center}
  \caption{Example of a forward looking algorithm that identifies peaks \& troughs.}
  \label{fig:old_zig_1}

\end{figure}

\endgroup

enter image description here

enter image description here

4
  • 1
    Fancyhdr won't do it. You have to turn off the current headers/footers and create custom versions. See tex.stackexchange.com/questions/66769/… and tex.stackexchange.com/questions/226678/… Commented May 14, 2015 at 16:06
  • It seems like the figure environment doesn't recognize the new margins and adjusts the graphics to the old textwidth. If you remove the figure environment and just write \begin{center}\includegraphics[]{male.jpg}\end{center} then centering works fine. (But you don't get a caption and label). The reason for the asymmetric margins is your \usepackage[left=2cm, right=2cm, bottom=2cm, top=3cm]{geometry} line. We have tilted the page and this makes the right margin 1 cm longer than the other margins. To fix it, add \addtolength{\textheight}{1cm} before the \headwidth=\textheight line.
    – user49901
    Commented May 15, 2015 at 10:51
  • @Grimler Thank you so much , problems are all solved with the above comment.
    – lorraine
    Commented May 15, 2015 at 11:38
  • For those using the \addtolength{\textheight}{1cm} line, be sure to \addtolength{\textheight}{-1cm} afterwards, else you get a blank page when going back to portrait mode.
    – tb87
    Commented Oct 26, 2021 at 3:38

1 Answer 1

7

Same question as keeping-header-on-top-in-landscape-mode?

@touhami provided an excellent answer. Instead of using the landscape environment, change the geometry to landscape using:

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

and then set the headwidth to the textheight:

\headwidth=\textheight

Now set the textwidth & textheight locally within a group and write your landscape stuff:

\begingroup 
\vsize=\textwidth
\hsize=\textheight
%landscape stuff
\endgroup

and then change back to portrait using the same code (with the difference that the headwidth should be the textwidth).

Full MWE:

\documentclass{article}
\usepackage{fancyhdr}
\usepackage{lipsum}
\begin{document}

\pagestyle{fancy}
\fancyhf{}
\lipsum[1]
\newpage
\paperwidth=\pdfpageheight
\paperheight=\pdfpagewidth
\pdfpageheight=\paperheight
\pdfpagewidth=\paperwidth
\headwidth=\textheight
\begingroup 
\vsize=\textwidth
\hsize=\textheight
\lipsum[2]
\endgroup
\newpage
\paperwidth=\pdfpageheight
\paperheight=\pdfpagewidth
\pdfpageheight=\paperheight
\pdfpagewidth=\paperwidth
\headwidth=\textwidth

\lipsum[3]
\end{document}
6
  • thanks for your response, but it produces some problems with images. i cannot have the image in the center by using \centering.
    – lorraine
    Commented May 14, 2015 at 16:44
  • @Nimakhin You're right. Didn't work for me either. \begin{center} ... \end{center} Works however, same for you?
    – user49901
    Commented May 14, 2015 at 18:26
  • No, it did not move the picture to the center !
    – lorraine
    Commented May 15, 2015 at 7:54
  • Moreover, the margins in the landscape does not seem symmetric, how can i manage to have a symmetric margins in the landscape view?
    – lorraine
    Commented May 15, 2015 at 8:00
  • @Nimakhin Could you provide a new MWE with these problems?
    – user49901
    Commented May 15, 2015 at 8:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .