# Looping through several background images in beamer

I have an image directory with several images:

p1.jpg
p2.jpg
p3.jpg
p4.jpg


I also have the following environment that sets the background image to a frame:

\newenvironment{imageframe}[1]
{% Set background image
\usebackgroundtemplate{%
\begin{tikzpicture}[remember picture,overlay]%
\node[inner sep=0] at (current page.center) {\includegraphics[width=\paperwidth,height=\paperheight]{#1}};%
\end{tikzpicture}%
}%
\begin{frame}%
}
{
\end{frame}%
}


It is used like this:

\begin{imageframe}{p1.jpg}
\frametitle{Nothing was the same.}
Always felt like my vision been bigger than the bigger picture.
\end{imageframe}


I would like to create variable (or command) which holds the list of valid background images (p1.jpg, ..., p4.jpg), and then not specify the image path explicitly when the environment is called. Instead, the first image (p1.jpg) should be selected the first time imageframe is invoked, the second image (p2.jpg) should be called the second time, and so on. Overflow should loop back to p1.jpg.

You can generate a few macros and a name list and loop through via a mod operation.

\documentclass{beamer}
\usepackage{tikz,mwe}% For dummy images

\newcounter{backgroundimagecounter}
\setcounter{backgroundimagecounter}{0}
\newenvironment{autoimageframe}
{% Set background image
\usebackgroundtemplate{%
\begin{tikzpicture}[remember picture,overlay]%
\pgfmathsetmacro\currentbackgroundimage{\backgroundimagenamelist[int(Mod(\value{backgroundimagecounter},\numberofbackgroundimages))]}%
\node[inner sep=0] at (current page.center) {\includegraphics[width=\paperwidth,height=\paperheight]{\currentbackgroundimage}};%
\end{tikzpicture}%
\stepcounter{backgroundimagecounter}%
}%
\begin{frame}%
}
{
\end{frame}%
}
\def\backgroundimagenamelist{{
"example-image",
"example-image-a",
"example-image-b",
"example-image-c"}}
\def\numberofbackgroundimages{4}

\begin{document}
\foreach\x in{1,...,9}{
\begin{autoimageframe}
\frametitle{Nothing was the same.}
Always felt like my vision been bigger than the bigger picture.
\end{autoimageframe}
}

\end{document}


Without counters and without the need of telling the number of images in advance: just define the list of images in the shown way and you're done.

\documentclass{beamer}
\usepackage{tikz}

\newenvironment{autoimageframe}
{% Set background image
\usebackgroundtemplate{%
\begin{tikzpicture}[remember picture,overlay]
\node[inner sep=0] at (current page.center) {%
\includegraphics[width=\paperwidth,height=\paperheight]{\currentbgimage}%
};
\end{tikzpicture}%
\expandafter\swapbgimage\bgimagelist
}%
\begin{frame}}
{\end{frame}}
\newcommand\currentbgimage{\expandafter\usebgimage\bgimagelist}
\newcommand{\usebgimage}{}
\def\usebgimage#1#2\bgimagelist{#1}
\newcommand{\swapbgimage}{}
\def\swapbgimage#1#2\bgimagelist{%
\gdef\bgimagelist{#2{#1}\bgimagelist}%
}

% Define here the list of images
% Each image is in a braced group
% REMEMBER to have \bgimagelist at the end
\newcommand\bgimagelist{
{example-image}
{example-image-a}
{example-image-b}
{example-image-c}
\bgimagelist
}

\begin{document}

% Thanks to Percusse for the code here
\foreach\x in{1,...,9}{
\begin{autoimageframe}
\frametitle{Nothing was the same.}
Always felt like my vision been bigger than the bigger picture.
\end{autoimageframe}
}

\end{document}


The \currentbgimage macro uses the first item in the list; then \swapbgimage puts the first item at the end.