Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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)

share|improve this question
    
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 [pdf](La)TeX (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
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

2 Answers 2

up vote 14 down vote accepted

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}
share|improve this answer
    
Very cool. This reverses all the pages in the document. How would this work to reverse some subset of the pages from a particular point in the document (as in the OP's question)? –  Alan Munn Aug 8 '11 at 0:48
    
@Alan Munn: \o/ I also got hyperlinks to work. –  Bruno Le Floch Aug 8 '11 at 10:10

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.