You can test with the macro \IfFileExist{filename}{then-do-this}{else-do-this}
whether a file filename
exists or not and respond accordingly. If the file filename
exists, the then-do-this
branch is executed, otherwise the else-do-this
branch. Replace then-do-this
and else-do-this
with your own code.
In your new given MWE I would delete thr part to copy the pdf
file and change the part to build the thumbnail slide to (supposing you have 4 thumbnails to include):
\IfFileExists{\jobname-copy.pdf}{%\begin{frame} \foreach \i in {1,...,4} { \fbox{\includegraphics[width=0.2\linewidth, page=\i] {\jobname-copy.pdf}} }\end{frame}}{\typeout{No Thumbnails included}}
Now you only need to do (you can put this into a batch file):
pdflatex myslides.texpdflatex myslides.texcp myslides.pdf myslides-copy.pdfpdflatex myslides.tex
The thumbnail were only included if the file myslides-copy.pdf
exists. If not file myslides.tex
compiles without included thumbnails.
Update 2:
The reason for the error message you got
Error: PDF file is damaged - attempting to reconstruct xref table...
is simple: you want to copy the file myslides.pdf
you have already opened to compile it. That's not possible. So you have to first compile the presentation to get the frames and to close this file myslides.pdf
. Then copy the file myslides.pdf
to myslides-copy.pdf
to include the thumbnails from myslides-copy.pdf
into a new compiled file myslides.pdf
.