0

As suggested here, I'm using \includepdf to insert several multi-page PDF files of musical scores into a LaTeX document:

\newcommand{\insertScore}[3] {
  \addcontentsline{toc}{section}{#2}
  \includepdf[scale=0.8,pages=1,pagecommand=\section*{#2}]{#1/score.pdf}
  \includepdf[scale=0.8,pages=2-,pagecommand={}]{#1/score.pdf}

  … more stuff following
}

However, while most of these PDFs consist of two or more pages, some have only one single page, which makes the second \includepdf fail. Is there any way to work around this issue without knowing in advance, how many pages the included PDF will consist of? The LaTeX file is generated automatically in this project, therefore using different commands for different PDFs isn't really an option ...

1
  • 1
    I posted an answer, but generally it is much better to provide an MWE (starting with \documentclass and ending with \end{document}) than code fragments like this. This allows people to reproduce your problem and test their solutions. It also often helps you in understanding your own problem better.
    – schtandard
    Jun 22, 2022 at 6:42

1 Answer 1

1

Just test the number of pages in the PDF first (cf. this question).

\newcommand{\insertScore}[3] {%
  \pdfximage{#1/score.pdf}%
  \addcontentsline{toc}{section}{#2}%
  \includepdf[scale=0.8,pages=1,pagecommand=\section*{#2}]{#1/score.pdf}%
  \ifnum\pdflastximagepages>1\relax
    \includepdf[scale=0.8,pages=2-,pagecommand={}]{#1/score.pdf}%
  \fi
  %
  % … more stuff following
}

You must log in to answer this question.

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