0

I want all of the PDFs in my directory /pdfs to be included with one command, so that I only have to put new pdfs in the directory and not having to worry about actually implementing them each time I compile. Like so:

\includepdf[pages=-]{pdfs/some.pdf}
\includepdf[pages=-]{pdfs/random.pdf}
\includepdf[pages=-]{pdfs/pdf.pdf}
\includepdf[pages=-]{pdfs/files.pdf}
...

But rather just have for instance

\includepdf[pages=-]{pdfs/*} 

at the end of my document.

What I currently do is run the command pdfjam * -o all_files.pdf in the directory, delete the new files, and have \includepdf[pages=-]{pdfs/all_files.pdf} in my document. But this is a somewhat cumbersome and mindless exercise I feel there should be an easier way.

Any ideas?

E. l4d3
  • 697
  • 1
    https://tex.stackexchange.com/questions/7653/how-to-iterate-through-the-name-of-files-in-a-folder may be useful. – Willie Wong Sep 23 '19 at 17:49

1 Answers1

4

You could write a Makefile that dumps all the file names into an auxiliary macro (space delimited, make sure that no file name contains a space) that is then parsed by LaTeX.

The Makefile, change my_latex_file.tex to the actual file name of your .tex-file (important: the indents in Makefiles have to be a tabulator, not spaces)

PDFS = $(wildcard pdfs/*.pdf)
TEX = pdflatex

main: my_latex_file.tex $(PDFS)
    $(TEX) -jobname="$(<:%.tex=%)" "\def\mypdffiles{$(PDFS)}\input{$<}"

And a small LaTeX file that includes every PDF contained in the macro \mypdffiles (if it is defined):

\documentclass[]{article}

\usepackage{pdfpages}
\usepackage{xparse}
\ExplSyntaxOn
\seq_new:N \l_elade_pdf_files_seq
\msg_new:nnn { elade } { missing-macro }
  { The~macro~#1 doesn't~exist. }
\NewDocumentCommand \IncludePDFs { O{} }
  {
    \cs_if_exist:NTF \mypdffiles
      {
        \seq_set_split:NnV \l_elade_pdf_files_seq { ~ } \mypdffiles
        \seq_map_inline:Nn \l_elade_pdf_files_seq
          {
            \includepdf [ { #1 } ] { ##1 }
          }
      }
      { \msg_error:nnn { elade } { missing-macro } { \mypdffiles } }
  }
\ExplSyntaxOff

\begin{document}
\IncludePDFs[pages=-]
\end{document}

Then compile using make instead of pdflatex (or whichever version). You might have to extent the Makefile a bit to cover everything or set it up to call arara if you should prefer this.

Skillmon
  • 60,462