# Include pdf pages from the current document into itself [duplicate]

I'd like to include some pages from the pdf file produced by a tex document into the document itself. Is it possible to do it? The use case is that I would like to produce an 'outline of the talk' slide at the end of a beamer presentation, like this one

Minimal non-working example:

\documentclass{beamer}
\usepackage{graphicx}
\begin{document}
\begin{frame}
\begin{block}{Frame}
First frame
\end{block}
\end{frame}
\begin{frame}
\includegraphics[page=1, width=0.5\textwidth]{\jobname.pdf}
\end{frame}
\end{document}


I have tried pdfpages and \includegraphics from graphicx, but they both fail with the same error

pdfTeX error: pdflatex (file XXX.pdf): xpdf: reading PDF image failed


I am fine if the included version is not the one currently being compiled, but the result of a previous compilation of the same file. If I rename the pdf file and then include the copy it works (that's how I produced the image above); it would be OK to have a solution that simply automates this. I'd like to avoid turning on unrestricted \write18 just for this, though.

## marked as duplicate by Federico Poloni, user36296 beamer StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Feb 9 '17 at 16:58

• you will need to move the file, so use shell-escape (or simpler really) just include the moving of the file into your build command for the document. – David Carlisle Feb 9 '17 at 16:09
• Maybe helpful: tex.stackexchange.com/questions/69991/… – user36296 Feb 9 '17 at 16:36
• @samcarter Thanks, that sounds like the exact same question. I voted to close my own question as a duplicate (I didn't know it was even possible...) – Federico Poloni Feb 9 '17 at 16:55

When compiling a .tex iput file, usually the name of the resulting .pdf output file is the same as the name of the .tex iput file.

Usually an existing .pdf output file that came into being during a previous (La)TeX run gets destroyed at the start of the current (La)TeX run. Therefore it usually is not available for including/importing pages during the current (La)TeX run.

That destruction can be worked around:

E.g., you can write some shell-script or batch-script for renaming the .pdf file in question before starting compilation.

With many TeX platforms it also is possible when calling (La)TeX from the command-line to pass some option for changing/setting the name of all those files where the name is derived/deriveable from the expansion of the \jobname-primitive. This also affects the name of the .pdf output file.

Setting the Name of the Output File

You can change the name of all output files by using the option --job-name=name.
This switch actually sets the name of the TeX job and has an effect of the
output file names, because these names are derived from the job name.
Look at the following example:

C:\> latex -job-name=foo sample2e
This is TeX, Version 3.14159 (MiKTeX 2.2)
(D:\texmf\tex\latex\base\sample2e.tex
LaTeX2e <2001/06/01>
Babel <v3.7h> and hyphenation patterns for english, german, ngerman, loaded.
(D:\texmf\tex\latex\base\article.cls
Document Class: article 2001/04/21 v1.4e Standard LaTeX document class
(D:\texmf\tex\latex\base\size10.clo))
No file foo.aux.
(D:\texmf\tex\latex\base\omscmr.fd) [1] [2] [3] (foo.aux) )
Output written on foo.dvi (3 pages, 7256 bytes).
Transcript written on foo.log.
C:\>

Note the altered output file names: foo.aux, foo.dvi and foo.log.


For producing your presentation you can probably write a script for creating two .pdf output files from the same set of .tex input files, using different job-names.

E.g., in the process of producing the first .pdf output file, use a job-name which containes the phrase "draft".

E.g., in the process of producing the second .pdf output file, use a job-name which containes the phrase "final".

The "draft" .pdf output file and the "final" .pdf output file should be very similar.

But:

When producing the first .pdf file, the "draft" .pdf output file, you can have TeX insert some dummy pages in those places where you actually wish to have included copies of some of the document's own pages.

When producing the second .pdf file, the "final" .pdf output file, you can apply graphics/graphicx-package's \includegraphics or pdfpages-package's \includepdf in those places where you actually wish to have included copies of some of the document's own pages for importing/including pages from the "draft" .pdf output file.

This way same page-breaking and page-numbering of the "draft" .pdf output file and the "final" .pdf output file is ensured.

As the "draft" .pdf output file and the "final" .pdf output file are based on the same set of .tex input files, you need to implement a mechanism for finding out whether it is the "draft" .pdf output file or the "final" .pdf output file that currently gets produced.

It the "draft" .pdf output file is in production, have TeX insert some dummy pages.

It the "final" .pdf output file is in production, have TeX import the pages from the "draft" .pdf output file.

Such a mechanism could be based on the kernel-macro \ifFileExists, which can be used for checking whether the "draft" .pdf output file already exists, and, if so, having TeX include pages from the "draft" .pdf output file, and, if not so, having TeX include/produce some "dummy pages".

It could also be based on checking whether the expansion of the \jobname-primitive yields the name of the "draft" .pdf output file or the name of the "final" .pdf output file.

Be aware that currently in any case importing via graphics/graphicx-package's \includegraphics or via pdfpages-package's \includepdf implies that hyperlinks contained within original pages are disabled within copied pages.

Also be aware that copied pages actually stem from a (La)TeX run where the expansion of \jobname was different. Therefore pages that are to be copied not containing textual phrases derived from the expansion of the \jobname-primitive might be a good idea.