4

I have multiple PDF files(that have only one page) that I need to include into a document. I succeeded in making Latex include the PDFs that are present in the directory with this:

\usepackage{pdfpages}

\usepackage{bookmark}

\includepdf[pages={1},fitpaper=true]{1.pdf} 

\includepdf[pages={1},fitpaper=true]{2.pdf} 

\includepdf[pages={1},fitpaper=true]{3.pdf} 

\includepdf[pages={1},fitpaper=true]{4.pdf} 

\includepdf[pages={1},fitpaper=true]{5.pdf} 

etc...

So if only files 1.pdf and 2.pdf are present in the directory , files 3.pdf, 4.pdf etc... will be skipped and the document will continue on to the next command. Then if I have more files to include , LaTeX will automatically detect them and insert them into the final document. Which is what I want. The problem is that I have to include a blank page between each of the inserted PDFs. I tried :

\includepdf[pages={1,{}},fitpaper=true]{1.pdf} 

and this:

\includepdf[pages={1},fitpaper=true]{1.pdf} 
\newpage
 \thispagestyle{empty}
\mbox{}

But if I do this , it will insert a blankpage after each PDF that isn't in the directory.Thus resulting in a \AM@currentdoc... page followed by a blank page in the created PDF document. So to be clear,I want him to only insert a blank page if the PDF file is found. Is this possible?

5
  • 1
    Welcome to TeX.SX! Does tex.stackexchange.com/questions/39982/… help?
    – egreg
    Commented Feb 11, 2015 at 17:05
  • You can check for \ifoddpage. Commented Feb 11, 2015 at 17:07
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Commented Feb 12, 2015 at 0:49
  • Please complete your code to provide a minimal working example (MWE). If you say \includepdf[]{something.pdf} and something.pdf does not exist, TeX stops with an error. It doesn't just move on to the next file. So you cannot have told us what you are really doing.
    – cfr
    Commented Feb 12, 2015 at 4:01
  • It does move on if you put a space in between both commands. Commented Feb 12, 2015 at 20:47

1 Answer 1

2

You could use something like this which does not produce errors when a file does not exist. On my machine, I get a four page document (files 1 and 4 exist; files 2 and 3 do not).

\documentclass[twoside]{article}
\usepackage{pdfpages}
\includepdfset{pages=1, fitpaper=true}
\usepackage{bookmark}

\newcommand*\ifpdfexists[1]{%
  \IfFileExists{#1.pdf}{\includepdf{#1}\cleardoublepage}{\relax}}

\begin{document}

  \ifpdfexists{body}
  \ifpdfexists{1}
  \ifpdfexists{2}
  \ifpdfexists{mypages}

\end{document}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .