Percorrendo várias imagens de fundo no beamer

Percorrendo várias imagens de fundo no beamer

Eu tenho um diretório de imagens com várias imagens:

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

Também tenho o seguinte ambiente que define a imagem de fundo como um quadro:

\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}%
}

É usado assim:

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

Gostaria de criar uma variável (ou comando) que contenha a lista de imagens de fundo válidas (p1.jpg, ..., p4.jpg) e não especificar explicitamente o caminho da imagem quando o ambiente for chamado. Em vez disso, a primeira imagem (p1.jpg) deve ser selecionada na primeira vez que o imageframe for invocado, a segunda imagem (p2.jpg) deve ser chamada na segunda vez e assim por diante. O estouro deve voltar para p1.jpg.

Responder1

Sem contadores e sem a necessidade de informar antecipadamente a quantidade de imagens: basta definir a lista de imagens da forma mostrada e pronto.

\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}

insira a descrição da imagem aqui

A \currentbgimagemacro utiliza o primeiro item da lista; em seguida, \swapbgimagecoloca o primeiro item no final.

Responder2

Você pode gerar algumas macros e uma lista de nomes e fazer um loop por meio de uma modoperação.

\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}

insira a descrição da imagem aqui

informação relacionada