Wednesday 10 November 2010

Using pdfpages to rotate odd pages only

Today I scanned a document to PDF. It was a large document, and I scanned it in stages. No worries about putting the pieces together—that's what the LaTeX package pdfpages is for. What did cause a bit of a problem was that the odd and even pages were oppositely orientated. When I scanned the pages I had to turn the book around, meaning that all the odd pages were upside down.

To rectify this problem I had to delve into the dark world of LaTeX programming. It was an adventure, but thankfully it wasn't too difficult. What I came up with was the following tex file:
\documentclass[a4paper,twoside]{memoir}

\newcounter{number@}

\usepackage{pdfpages}

\begin{document}

\setcounter{number@}{0}

\loop\ifnum \value{number@} < 6 %CHANGE for each document
\stepcounter{number@}
   \ifodd \value{number@}
      \includepdf[pages=\arabic{number@}, angle=180]{document}
   \else
      \includepdf[pages=\arabic{number@}]{document}
\fi
\repeat

\end{document}

To illustrate, I've made an example PDF file to test it on. This test file is an open access paper from Zootaxa, the original of which is available here.

Do remember to change the number that \value{number@} is being compared to. This number is the total pages in "document.pdf", and I haven't yet figured out how to automatically retrieve it. Doing so would've consumed more time than I can afford just now.

Particularly helpful in this adventure was the Tralics site that contains documentation on all TeX commands, and this site on counters.

2 comments:

matieh said...

this code places the number of pages in \lastpagecount:

\documentclass[]{minimal}
\usepackage{pdfpages}
\newcommand{\retrievepagecount}[1]{\pdfximage{#1}\edef\lastpagecount{\the\pdflastximagepages}}
\begin{document}
\retrievepagecount{pdffile.pdf}
\lastpagecount
\end{document}

Samuel Brown said...

Thank you matieh! That code makes things much more straightforward.