5

I am reading the presentation here where the background image changes in each slide. I am thinking how you can do it in Beamer. Pseudocode

\documentclass{beamer}
\usetheme{Berkeley}    
% TODO random background generation here

\begin{document}
\usepackage{lipsum}
\begin{frame}
\frametitle{Normal}
% lorem
\end{frame}
\end{document}

If there is a test collection of background images in TeXLive, that would be great to use them. Otherwise, the dummy test image is fine.

OS: Debian 9.1 Stretch
TeXLive: 2017

9

The following uses pgf/tikz to generate a random number out of 1, 2 and 3. In the mwe package (which is included with xcolor) there are the image files example-image-a, example-image-b, and example-image-c, so we choose them with \@alph.

\documentclass{beamer}
\usetheme{Berkeley}    

\usepackage{pgf}% or \usepackage{tikz}
\usepackage{lipsum}

\makeatletter
\newcommand*{\randimg}{%
  \pgfmathrandom{3}%
  \includegraphics[width=\paperwidth,height=\paperheight]%
    {example-image-\@alph{\pgfmathresult}}}
\makeatother

\setbeamertemplate{background}{%
  \randimg%
}

\begin{document}
\begin{frame}
\frametitle{Normal}
\lipsum
\end{frame}
\begin{frame}
  Foo
\end{frame}
\begin{frame}
  Bar
\end{frame}
\begin{frame}
  Baz
\end{frame}
\end{document}

The following uses a TeX count called \mycount to cycle through the images and doesn't need pgf or tikz anymore (just the definition of \randimg and the initialization of \mycount, the rest remains untouched):

\newcount\mycount
\newcommand*{\randimg}{%
  \global\advance\mycount by 1\relax%
  \ifnum\mycount>3\relax%
    \global\mycount=1\relax%
  \fi%
  \includegraphics[width=\paperwidth,height=\paperheight]%
    {example-image-\@alph{\mycount}}}
  • My output: C, B, B and A in the background image. How can you use the background image collection mentioned by you here? – Léo Léopold Hertz 준영 Sep 8 '17 at 14:15
  • 1
    @LéoLéopoldHertz준영 your question is titled as "How to randomly assign background picture in Beamer?". My answer does exactly this. If you want to cycle through images, you might increment a counter on each frame and revert it to 1 if it is greater than 3. And instead of \@alph{\pgfmathresult} you use \alph{mycounter}. – Skillmon Sep 8 '17 at 18:05
  • 1
    @LéoLéopoldHertz준영 is this answer enough? Looking at your other questions I recognized, that you have several questions where you did not accept an answer though several answer are given and good (including bounty given by you). – Skillmon Sep 10 '17 at 14:01
  • Yes, your answer is the right one in dynamics, but I need to understand if it can be extended to any collection of pictures? What do you think? I think it is a basic list problem that needs to be connected here. Any test images which we can use to test this? – Léo Léopold Hertz 준영 Oct 5 '17 at 9:11
  • Well, either you give your images consecutive names (containing either numbers or letters, however you want) and call them this way, or you find out how to read folder contents. Your initial question however is completely answered and you should accept the answer as it is very unlikely that you'll get another, better answer. I will not add anything to my answer beyond that point. You might ask a new better formulated question which really fits your needs and doesn't ask something other than what you want. – Skillmon Oct 5 '17 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.