7

Is it possible to change the background color of a Beamer presentation in the PDF in such way, that whenever I switch a page, the color is switched as well?

Note that I do not seek a solution to randomly switch during compilation (although that could be interesting as well), I look for a way to switch the color whenever I switch pages in the final PDF. So going from slide 1 to slide 2 and back should generate two different background colors for slide 1.

Uwe Ziegenhagen
  • 13,168
  • 5
  • 53
  • 93
  • 2
    It looks this is a task for JavaScript in TeX. It is pity that the question is not about random switch during compilation. I would like to try to solve such a task... :-) – Malipivo Apr 14 '14 at 07:58
  • @Malipivo: I guess, a lot of users here would anyway appreciate a solution to the random switching ;-) –  Apr 14 '14 at 08:07
  • I am polite and decent, I hope, I will wait for such a question. ;-) – Malipivo Apr 14 '14 at 08:09
  • 2
    @Malipivo: There should be place at TeX.SE for 'unwanted' solutions or solutions to questions no one asked before ;-) –  Apr 14 '14 at 08:11
  • @Christian H. I vote for it, a (sub)section (in the question) called something like >half-solutions< or >solutions to the questions no one will ever ask<. :-) – Malipivo Apr 14 '14 at 08:15
  • 1
    I saw two days ago a presentation with coloured backgrounds. Sadly while it looked quite good on the laptop it was quite unreadable on the wall. – Ulrike Fischer Apr 14 '14 at 09:15
  • 1
    OCG (PDF Layers) + JavaScript. – AlexG Apr 14 '14 at 10:07
  • @Malipivo: As I understand, it is quite acceptable to ask a question and also give the answer. This can be done to make this 'Question and answer'-page more complete, or to showoff an idea to solve a problem no one has thought of. – hpekristiansen Apr 14 '14 at 11:07
  • @Hans Oh yes, yes, we were just kidding. Uwe expressed his needs precisely. Hans Hagen is having impressive presentations with a lot of interesting ideas, generally speaking, see some for yourself at http://luatex.org/documentation.html. – Malipivo Apr 14 '14 at 11:15
  • @UlrikeFischer I guess I know whose presentation it was. This presentation made me come up with this questions... – Uwe Ziegenhagen Apr 14 '14 at 11:54
  • 2
    randomly assigning colours during compilation has now its own question http://tex.stackexchange.com/q/171481/36296 – samcarter_is_at_topanswers.xyz Apr 14 '14 at 18:36

2 Answers2

4

Example based on OCGs and JavaScript (pdflatex, AdobeReader required). Background colour is randomly chosen at display time.

\documentclass{beamer}

\usepackage{multido}
\usepackage{animate} %defines \@anim@newocg
\makeatletter
  \let\newocg\@anim@newocg
  \def\lastOCGobj{\@anim@curocg}
\makeatother

%JavaScript action to be executed on page open event
\pdfpageattr{ /AA <<
  /O <<
    /S/JavaScript/JS (
        if(typeof(bgOCGs)==='undefined'){
        bgOCGs=this.getOCGs();
        curOCG=bgOCGs[0];
        for(i=1;i<bgOCGs.length;i++)bgOCGs[i].state=false;
        curOCG.state=true;
      }
      var l = bgOCGs.length;
      randomIndex = Math.floor(Math.random()*l)\%l;
      curOCG.state=false;
      curOCG=bgOCGs[randomIndex];
      curOCG.state=true;
    )  
  >>
>>}

\begin{document}

\newsavebox\bgbox
\multido{\i=1+1}{100}{%
  \pgfmathsetmacro{\R}{random(0,10000)/10000}%
  \pgfmathsetmacro{\G}{random(0,10000)/10000}%
  \pgfmathsetmacro{\B}{random(0,10000)/10000}%
  \definecolor{bgcolor}{rgb}{\R,\G,\B}%
  \savebox\bgbox{\color{bgcolor!40}\rule{\paperwidth}{\paperheight}}%
  \newocg{bg}{\i}%  %create new ocg
  \immediate\pdfxform attr{/OC \lastOCGobj}\bgbox%
  \expandafter\xdef\csname cbox\i\endcsname{\the\pdflastxform}%
}  
\setbeamertemplate{background canvas}{%
  \makebox[\paperwidth][l]{%
    \rule{0pt}{\paperheight}%
    \multido{\i=1+1}{100}{%
      \makebox[0pt][l]{\expandafter\pdfrefxform\csname cbox\i\endcsname}%
    }%
  }%
}

\begin{frame}{Frame 1}
\end{frame}
\begin{frame}{Frame 2}
\end{frame}
\begin{frame}{Frame 3}
\end{frame}
\begin{frame}{Frame 4}
\end{frame}
\end{document}
AlexG
  • 54,894
3

Since you (essentially) want randomly coloured page backgrounds at displaytime of a PDF, this is only doable with JavaScript. As pointed out in the comments OCGs will help.

I wouldn't be surprised if this is already solved in ConTeXt. :-)