# Something like \includeonly but for \includepdf?

Let's suppose we have a document like the one below.

\documentclass{scrartcl}

\usepackage{pdfpages}

\begin{document}
\include{Text_A}
\include{Text_B}
\include{Text_C}
\include{Text_D}
\includepdf{Attachment_A}
\includepdf{Attachment_B}
\includepdf{Attachment_C}
\end{document}


I can use the \includeonly command to only compile certain parts. So if I only want Text_A and Text_C, I can add \includeonly{Text_A,Text_C} to the preamble. Now my problem is that this only works for parts which are included with \include.

I'm looking for a way to combine this with parts that are included with \includepdf. So if I only want Text_A, Attachment_A and Attachment_C, I have to comment out the lines one by one. This is rather unpleasant if there are a lot of files included via \includepdf and if the command contains multiple lines of options.

I could do some refactoring and put the \includepdf commands into single files so that I'm able to use \include exclusively in the main document. But I wonder if there might be a more elegant solution?

• The primary function of \includeonly is to keep track of page numbers and aux files. Of course, you could create a bunch of .tex files consisting of one \includepdf command. Aug 7 '19 at 13:36

You can borrow the \includeonly test used by \include.

This just includes the text and attachment for B

\documentclass{scrartcl}

\usepackage{pdfpages}
\makeatletter
\let\oldincludepdf\includepdf
\newcommand\zzincludepdf[2][]{%
\@tempswatrue
\if@partsw
\@tempswafalse
\edef\reserved@b{#2}%
\@for\reserved@a:=\@partlist\do
{\ifx\reserved@a\reserved@b\@tempswatrue\fi}%
\fi
\if@tempswa
\IfFileExists{#2.pdf}{\oldincludepdf[#1]{#2}}{\typeout{no file #2.pdf}}%
\fi}
\let\includepdf\zzincludepdf
\makeatother

\includeonly{Text_B,Attachment_B}
\begin{document}
\include{Text_A}
\include{Text_B}
\include{Text_C}
\include{Text_D}
\includepdf{Attachment_A}
\includepdf[pages=-]{Attachment_B}
\includepdf{Attachment_C}
\end{document}

• you seem to know something about the inner working ;-) Aug 7 '19 at 13:51
• Wow, thank you. You really have some deep knowledge! But this will only work as long as there are no options for \includepdf, right? When I add \includepdf[pages=-]{Attachment_B} or similar compilation fails :( Aug 7 '19 at 15:16
• @schmattes ohh details... give me a minute and I'll put the optional argument back Aug 7 '19 at 15:23
• @schmattes try now Aug 7 '19 at 15:27
• Works as advertised :) Again, thank you very much. Aug 7 '19 at 15:34