我有一个包含多幅图像的图像目录:
p1.jpg
p2.jpg
p3.jpg
p4.jpg
我还有以下将背景图像设置为框架的环境:
\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}%
}
使用方式如下:
\begin{imageframe}{p1.jpg}
\frametitle{Nothing was the same.}
Always felt like my vision been bigger than the bigger picture.
\end{imageframe}
我想创建变量(或命令)来保存有效背景图像列表(p1.jpg,...,p4.jpg),然后在调用环境时不明确指定图像路径。相反,第一次调用 imageframe 时应选择第一幅图像(p1.jpg),第二次调用第二幅图像(p2.jpg),依此类推。溢出应循环回到 p1.jpg。
答案1
无需计数器,也无需提前告知图像数量:只需按照显示的方式定义图像列表即可。
\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}
宏\currentbgimage
使用列表中的第一个项目;然后\swapbgimage
将第一个项目放在末尾。
答案2
您可以生成一些宏和一个名称列表并通过mod
操作循环执行。
\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}