Consider the minimal working example below that progressively superimposes (using the `overpic` package) images contained in a pdf file.

```
\documentclass{beamer}
\usepackage{overpic}

\begin{document}
\begin{frame}

\onslide<1->{\begin{overpic}[width=\textwidth,page=1]{MyPDF}
\onslide<2->{\begin{overpic}[width=\textwidth,page=2]{MyPDF}
\onslide<3->{\begin{overpic}[width=\textwidth,page=3]{MyPDF}
\end{overpic}}
\end{overpic}}
\end{overpic}}

\end{frame}
\end{document}

```

I would like to shorten this code using a macro that takes as arguments the number of pages and the name of the pdf file. Ideally, the reduced code should look like this

```
\documentclass{beamer}
\usepackage{overpic}

\begin{document}
\begin{frame}

\MyMacro{3}{MyPDF}

\end{frame}
\end{document}

```

I tried to realize this using two for loops (using the `pgffor` package) as follows

```
\newcommand{\MyMacro}[2]{
\foreach \n in {1,...,#1}{\onslide<\n->\{\begin{overpic}[width=\textwidth,page=\n]{#2}}
\foreach \n in {1,...,#1}{\end{overpic}\}}
}

```

However, this approach does not work. I think that the main issue is that the `\begin{overpic}` and the `\end{overpic}` parts are split into two separate loops. I cannot figure out a way to make it work. Any suggestions?