Compiling only a page range or page selection

In the line of David Carlisle's answer, but using pure latex tools, I'm suprised no one mentioned pdfpages package.

You can compile your full document and then extract the required pages using another latex "driving" document whichs uses pdfpages. For example:

\documentclass{article} % Not really important
\usepackage{pdfpages}
\begin{document}
\includepdf[pages={12-15,23,45-49}]{main.pdf}
\end{document}

atbegshi provides the capability to discard a ready-to-be-shipped-out page using \AtBeginShipoutDiscard. Together with etoolbox's list-processing capability, you can easily discard/retain pages conditionally:

\documentclass{article}
\usepackage{lipsum,atbegshi,etoolbox}% http://ctan.org/pkg/{lipsum,atbegshi,etoolbox}
\makeatletter
\newcommand{\discardpages}[1]{% \discardpages{<csv list>}
  \xdef\discard@pages{#1}% Store pages to discard
  \AtBeginShipout{% At shipout, decide whether to discard page/not
    \renewcommand*{\do}[1]{% How to handle each page entry in csv list
      \ifnum\value{page}=##1\relax%
        \AtBeginShipoutDiscard% Discard page/not
        \gdef\do####1{}% Do nothing further
      \fi%
    }%
    \expandafter\docsvlist\expandafter{\discard@pages}% Process list of pages to discard
  }%
}
\newif\ifkeeppage
\newcommand{\keeppages}[1]{% \keeppages{<csv list>}
  \xdef\keep@pages{#1}% Store pages to keep
  \AtBeginShipout{% At shipout, decide whether to discard page/not
    \keeppagefalse%
    \renewcommand*{\do}[1]{% How to handle each page entry in csv list
      \ifnum\value{page}=##1\relax%
        \keeppagetrue% Page should be kept
        \gdef\do####1{}% Do nothing further
      \fi%
    }%
    \expandafter\docsvlist\expandafter{\keep@pages}% Process list of pages to keep
    \ifkeeppage\else\AtBeginShipoutDiscard\fi% Discard page/not
  }%
}
\makeatother
\discardpages{1,3,5,7,9,29,35,40}% Discard these pages.
%\keeppages{2,4,6}% Keep these pages.
\begin{document}
\lipsum[1-50]\lipsum[1-50]\lipsum[1-50]\lipsum[1-50]\lipsum[1-50]
\end{document} 

The above 47-page document is scaled down to 39 after having removed the 8 pages 1, 3, 5, 7, 9, 29, 35, 40 (as supplied by \discardpages{1,3,5,7,9,29,35,40}). To discard zero pages, you could use \discardpages{} (or make no call to \discardpages). Using \keeppages{2,4,6} instead leaves you with a 3-page document consisting of only pages 2, 4 and 6. It should not be that difficult to extend this to include more complicated page ranges.

The above has not been tested with hyperlinks from hyperref.


Also the following packages should be mentioned:

  • selectp: [for plain TeX and LaTeX 2.09]

Select pages to be out­put

De­fines a com­mand \out­pu­tonly, whose ar­gu­ment is a list of pages to be out­put. With the com­mand present (be­fore \be­gin{doc­u­ment}), only those pages are out­put.

  • selectpage:

Select pages to be out­put from a doc­u­ment

Re­quires a file of page num­bers, which spec­i­fies the num­bers of pages to print (in gen­er­ated or­der).

  • pagesel: [for LaTeX 2]

Select pages of a doc­u­ment for out­put

Selects sin­gle pages, ranges of pages, odd pages or even pages for out­put. The pack­age is part of the oberdiek bun­dle.

(http://www.ctan.org/topic/page-control).

Tags:

Compiling