# turn pdfximage/pdflastximagepages into a function and use in tikz : ERROR: Missing number, treated as zero.

I feel a bit stupid not to be able to solve this problem, but I can't find a way to turn the following command, usefull to get the number of pages in a pdf : \pdfximage{mypdf.tex} \the\pdflastximagepages into an actual command so that I can just write \pdfximage{images.pdf} in a \foreach loop. And if someone can explain me why the first two examples works but not the last one, I'd be really interested.

Thanks!

MWE:

\documentclass{beamer}
\usepackage{tikz}

\newcommand{\nbpagepdf}[1]{\pdfximage{#1}\the\pdflastximagepages}

\begin{document}
\begin{frame}[fragile]
%% Ok:
The number of pages is: \nbpagepdf{images.pdf} !
%% Ok:
\pdfximage{images.pdf}
\foreach \ov in {1,...,\the\pdflastximagepages}{
\includegraphics<\ov>[page=\ov]{images.pdf}
}
%% Not ok:
\foreach \ov in {1,...,\nbpagepdf{images.pdf}}{
\includegraphics<\ov>[page=\ov]{images.pdf}
}
\end{frame}
\end{document}


Error:

ERROR: Missing number, treated as zero.

--- TeX said ---
\pdfximage
l.10   }

• \nbpagepdf is not a number, it only outputs at the end a number. you can't use it in a context where tex needs a real number. – Ulrike Fischer Aug 13 '18 at 17:57
• @Ulrike Fischer : so what can I do to embed everything into a single function ? – tobiasBora Aug 13 '18 at 20:59
• Nothing (at least not with pdflatex, with lualatex it would be perhaps possible to write a lua function). It won't work like you want it to work. – Ulrike Fischer Aug 13 '18 at 21:04

Define a suitable macro that first does \pdfximage.

\documentclass{beamer}
\usepackage{tikz}

\newcommand{\includepdfpages}[1]{%
\pdfximage{#1}%
\foreach \ov in {1,...,\the\pdflastximagepages}{%
\includegraphics<\ov>[page=\ov]{#1}%
}%
}

\begin{document}
\begin{frame}
\includepdfpages{tobiasimages.pdf}
\end{frame}
\end{document}


The file has ten images and this is the result:

• Thank you, even if it's not exactly what I was asking for, it should do the job most of the time. But I still don't understand what make it fondamentally different from others macros? For example I would expect that using an \edef would solve the issue, but it does not work... If you have some good explanation for that it would be really nice. – tobiasBora Aug 14 '18 at 6:34