[Tex/LaTex] Landscape and page numbers with pdfpages

landscapepage-numberingpdfpages

I am using pdfpages to insert PDFs into a master file. Some of the PDFs are portrait and some are landscape. I am using pagecommand=\thispagestyle{plain} so that the page numbers are in sequence throughout the final document. However, on PDFs included as landscape, the page numbers appear on the left side of the page (i.e. as they would if the page were not landscape). How can I get them to appear on the bottom of the page?

MWE where one.pdf is included in portrait and two.pdf is included in landscape:

\documentclass[12pt,english]{article}
\usepackage{pdfpages}
\includepdfset{pagecommand=\thispagestyle{plain}}
\usepackage{geometry}

\begin{document}

This is the text in the master file.

\newpage{}

\includepdf[pages=-,landscape=false]{one}

\newpage{}

\includepdf[pages=-,landscape=true]{two}

\end{document}

Best Answer

Using fancyhdr you can define your own page style and move the page number/counter into the appropriate position.

\usepackage{pdfpages}% http://ctan.org/pkg/pdfpages
\usepackage{fancyhdr}% http://ctan.org/pkg/fancyhdr
\fancypagestyle{mylandscape}{%
  \fancyhf{}% Clear header/footer
  \fancyfoot{% Footer
    \makebox[\textwidth][r]{% Right
      \rlap{\hspace{\footskip}% Push out of margin by \footskip
        \smash{% Remove vertical height
          \raisebox{\dimexpr.5\baselineskip+\footskip+.5\textheight}{% Raise vertically
            \rotatebox{90}{\thepage}}}}}}% Rotate counter-clockwise
  \renewcommand{\headrulewidth}{0pt}% No header rule
  \renewcommand{\footrulewidth}{0pt}% No footer rule
}

You would then use pagecommand=\thispagestyle{mylandscape}. Example output with one.pdf (1 page portrait) and two.pdf (1 page landscape):

enter image description here

two.tex:

\documentclass{article}
\usepackage{pdflscape,lipsum}% http://ctan.org/pkg/lipsum
\pagestyle{empty}
\begin{document}
\begin{landscape}
\lipsum[1-5]
\end{landscape}
\end{document}

one.tex:

\documentclass{article}
\usepackage{lipsum,pdfpages,fancyhdr}% http://ctan.org/pkg/{lipsum,pdfpages,fancyhdr}
\fancypagestyle{mylandscape}{%
  \fancyhf{}% Clear header/footer
  \fancyfoot{% Footer
    \makebox[\textwidth][r]{% Right
      \rlap{\hspace{\footskip}% Push out of margin by \footskip
        \smash{% Remove vertical height
          \raisebox{\dimexpr.5\baselineskip+\footskip+.5\textheight}{% Raise vertically
            \rotatebox{90}{\thepage}}}}}}% Rotate counter-clockwise
  \renewcommand{\headrulewidth}{0pt}% No header rule
  \renewcommand{\footrulewidth}{0pt}% No footer rule
}
\begin{document}
\lipsum[1-5]
\includepdf[noautoscale,landscape,pagecommand=\thispagestyle{mylandscape}]{two.pdf}
\end{document}