# Insert page of current document as figure

I'm trying to insert a page (that was already typeset, i.e. page < currentpage) of the current document as a figure. Obviously, one could use something like

\includegraphics[page=42]{MasterAsFigure.pdf}


However, this is not a "nice" solution because you have to remember to adapt the pagenumber manually and it requires two compilation runs.

Is there a better method to "label" a page (meaning, a position in the document which is then resolved to the page that part of the document was typeset on) and include it as a figure in a later part of the document?

• I think you will in any case need two runs (and to copy the file between runs) the new pdf is already being written by the time you get to this point so it is not in a state that can be included. page=\thepage will probably work if the image is being included at a point where the number is known, or you could use \label and a modified \pageref – David Carlisle Oct 26 '14 at 11:18
• @DavidCarlisle That makes sense. I'd assume that the (modified) \pageref must be stripped of all "referencing" logic (i.e. the hyperref stuff etc.). Is it possible to tell LaTeX to create such a copy before opening the pdf for writing? Or, alternatively, to create the copy after compilation has finished. – elemakil Oct 26 '14 at 11:41
• If you allow pdflatex --shell-escape you can run any system commands from within latex so copy or cp or whatever moves files on your system, or you could get the command sequence set up by your editor or arara or make or whatever to do it – David Carlisle Oct 26 '14 at 11:49

You can't use the current .pdf file during the run, because it is only finalized with the internal references at the end of the job.

Thus you have to make a copy of the previously generated one, say filename-copy.pdf with some script. Then this will work:

\documentclass{article}
\usepackage{graphicx,refcount}

\usepackage{kantlipsum} % to provide content

\begin{document}

\kant

\newpage

\kant[1-2]

\newpage

\kant

\newpage

\begin{center}
\ifnum\getpagerefnumber{toreprint}=0
\fbox{\rule{0pt}{3cm}\itshape\Large Not yet available}
\else
\includegraphics[page=\getpagerefnumber{toreprint},width=.3\textwidth]{\jobname-copy}
\fi
\end{center}

\newpage

\kant

\end{document}


The \ifnum test is used for the case when the reference has not yet been saved in the .aux file (first run after the label has been added, or any time the .aux file is removed by hand).

• Is there a platform-independent approach (which does not require using arara or a makefile, hence I'd assume it must rely on -shell-scape) to create the copy after compilation from within the document? – elemakil Oct 27 '14 at 9:58
• @elemakil I'll do some tries. A platform independent approach might be using Lua, which is available as texlua as soon as one has a recent TeX distribution. – egreg Oct 27 '14 at 10:21