# How to add a date into the name field of includepdf?

I'm using Sweave to automate the creation of a report, and I want to include a PDF into my document, but the PDF and its name are updated daily (because they are produced using Sweave).

For example today, my PDF's name is report_2016-01-04.pdf; tomorrow it'll be report_2016-01-05.pdf.

(Obviously) This isn't working for me...

\documentclass[10pt]{report}

\usepackage[yyyymmdd]{datetime}
\renewcommand{\dateseparator}{-}

\begin{document}

\includepdf[pages={1,2,3}]{report_\today.pdf}

\end{document}

• Hape you tried typing the \today to the log via \tyoeout to to confirm that is is not adding extra things that might confuse a file inclusion – daleif Jan 4 '16 at 20:21

Here is an option that should work:

\documentclass{article}

\usepackage{datetime2}
\usepackage{pdfpages}

\newcommand{\theyear}{\number\year}
\newcommand{\themonth}{\ifnum\month<10 0\fi\number\month}
\newcommand{\theday}{\ifnum\day<10 0\fi\number\day}
\begin{document}

\includepdf[pages={1,2,3}]{report_\theyear-\themonth-\theday.pdf}

\end{document}


Since the format will always be YYYY-MM-DD, we include them in the filename verbatim as \theyear-\themonth-\theday, where each have been defined to respect that format (including a two-digit month and day; see How to convert a one digit number to a two digit number).

Note that datetime is obsolete, and replaced by datetime2.