[Tex/LaTex] Drop the current page

page-breaking

I need to use a command of a 3rd-party package, which inserts multiple pages of content into my document. Is there a way to tell LaTeX to ignore the last of these pages? E.g. the command inserts 3 pages, but I only want the first two.

Equivalently, can I tell LaTeX to simply drop the current page, i.e. to go back to the last hard page break (\newpage) and forget all the content that has been added since then?

Obviously I can easily remove the page from the resulting PDF using external postprocessing, but I'd rather do it directly in the document if that's possible.

Note that I cannot modify the 3rd-party package and that not using the command from that package is not an acceptable solution.

Best Answer

If you know the number of pages will be included with the 3rd-party package/command, then you can issue a number of nested commands using atbegshi to discard a specific page. The following MWE provides \removepage{<num>} that will remove a page <num> from the current page:

enter image description here

\documentclass{article}
\usepackage{atbegshi,multido,graphicx}% http://ctan.org/pkg/{atbegshi,multido,graphicx}
\makeatletter
\newcommand{\removepage}[1]{%
  \def\@drop@this@page{\AtBeginShipoutDiscard}%
  \multido{\iA=1+1}{#1}{%
    \expandafter\gdef\expandafter\@drop@this@page\expandafter%
      {\expandafter\AtBeginShipoutNext\expandafter{\@drop@this@page}}%
    }%
  \@drop@this@page%
}
\newcommand{\magiccommand}{% Inserts 9 pages
  \multido{\iA=1+1}{9}{\clearpage\centering\scalebox{40}{\iA}}}

\makeatother
\begin{document}
\removepage{3}%
\magiccommand% 3rd-party command for including a bunch of pages
\end{document}

Note that using this method will discard the page completely (including the header/footer), which could contain an incremented page number. One should correct for this if needed, perhaps as part of \removepage.

If \magiccommand only inserts text that is formatted together with your current document (rather than including stand-alone full pages), then you can just issue \AtBeginShipoutNext{\AtBeginShipoutDiscard} after the command, which should remove the final page.

Related Question