20

In my document, I want pages from a specific point onward to appear in reverse page order, i.e. the last page should come first, then the penultimate page, etc. How can I do it?

(I don't want to reverse content - only the order in which pages appear)

Gadi A
  • 971
  • You are talking about the full page content, not just changing the numbering of the pages (as the used {page-numbering} tag would suggest)? Changing the order of the pages is not possible in TeX itself. You would need to post-process the output PDF (or PS) file. However, this will most likely remove or break all hyperlinks. – Martin Scharrer Jun 27 '11 at 13:20
  • I want to re-order the pages themselves in the PDF. If it is not possible I can accomplish this quite easily with pdftk, but I hopes for something more elegant... – Gadi A Jun 27 '11 at 13:22
  • I doubt it is possible within pdfTeX (I wish I am wrong), but you can use pdftk and using it you can obtain desired effect. Go with cat operation. EDIT: Ah, too late and more importantly needlessly. :) – przemoc Jun 27 '11 at 13:24
  • @GadiA: No, AFAIK reordering pages is not supported by TeX. For this it would need to buffer a lot of pages before writing them out. – Martin Scharrer Jun 27 '11 at 13:24
  • @przemoc, @GadiA: Does pdftk support/retains the hyperlinks? There is e.g. also the pdfjam script (based on the pdfpages LaTeX package) which will remove all hyperlinks. – Martin Scharrer Jun 27 '11 at 13:26
  • 1
    @Martin: Actually I've never checked it till now. I reversed PDF and links in TOC and bib references still work. – przemoc Jun 27 '11 at 13:33
  • @MartinScharrer: Reversing the page tree is trivial in PDF; see my answer on how pdfTeX. – Martin Schröder Dec 23 '11 at 21:21
  • @MartinSchröder: Ok, I wasn't thinking about reversing the page order using PDF features. That way it would be much easier. – Martin Scharrer Dec 24 '11 at 11:39
  • @GadiA: Just thought I'd mention I've used the solution to your question to complete (well, sort of) a document class for Technion Theses which properly sets the Hebrew content at the end so that you don't have to flip any pages after printing, just bring the thing to the binder. See: my own thesis, just submitted. – einpoklum Jan 23 '12 at 13:06
  • Glad to see I had a long term effect... (I used pdftk at the end). – Gadi A Jan 23 '12 at 13:48

2 Answers2

18

This reverses the order of the pages which are output while \RPtrue is set, and keeps pages in the normal order when \RPfalse is active. References and hyperlinks should work. I use Heiko Oberdiek's atbegshi package to hook into the \shipout primitive (which is responsible for sending pages to the dvi/pdf file): instead of sending the pages out, we store them in \RPbox. Namely, the contents of \RPbox have the form

  • Skip of 1 pt
  • First page to be reversed.
  • Kern of size <first page number> sp
  • Second page to be reversed.
  • Kern of size <second page number> sp
  • Etc.

Then in \RPfalse we use \lastkern and \lastbox to retrieve the contents of box registers and ship pages out one by one, with the correct page number in place (this makes sure that references are right). The assignments to \c@page are done locally so that the old value is restored once all pages have been output.

Also, I put a \clearpage in the macros \RPtrue and \RPfalse because I thought that would make more sense, but things shoud work without.

\documentclass{article}
\usepackage{atbegshi}
\newif\ifRP
\newbox\RPbox
\setbox\RPbox\vbox{\vskip1pt}
\makeatletter
\AtBeginShipout{%
  \ifRP
    \AtBeginShipoutDiscard%
    \global\setbox\RPbox\vbox{\unvbox\RPbox
      \box\AtBeginShipoutBox\kern\c@page sp}%
  \fi
}%
\renewcommand{\RPtrue}{%
  \clearpage
  \ifRP\RPfalse\fi
  \global\let\ifRP\iftrue
}%
\renewcommand{\RPfalse}{%
  \clearpage
  \global\let\ifRP\iffalse
  \setbox\RPbox\vbox{\unvbox\RPbox
    \def\protect{\noexpand\protect\noexpand}%
    \@whilesw\ifdim0pt=\lastskip\fi
      {\c@page\lastkern\unkern\shipout\lastbox}%
  }%
}%
\makeatother

% Test document.
\usepackage{lipsum}
\usepackage{hyperref}
\begin{document}

  \tableofcontents

  \section{Lipsum 1--50}
  \lipsum[1-50]

  \RPtrue

    \section{Lipsum 51--100}
    \lipsum[51-100]

  \RPfalse

  \section{Lipsum 101--150}
  \lipsum[101-150]

\end{document}
2

The probably never to be released trunk of pdftex (i.e. 1.50) adds the commands \pdfpagedivert and \pdfpageundivert which work like diversions in m4. With it you can change the order of pages in the resulting pdf any way you like.

It would be a nice feature for LuaTeX and could probably easily be added, if someone requested it.