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.

  • 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 7:58
  • 2
    @Malipivo: There should be place at TeX.SE for 'unwanted' solutions or solutions to questions no one asked before ;-) – user31729 Apr 14 '14 at 8:11
  • 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 9:15
  • 1
    OCG (PDF Layers) + JavaScript. – AlexG Apr 14 '14 at 10:07
  • 2
    randomly assigning colours during compilation has now its own question tex.stackexchange.com/q/171481/36296 – samcarter_is_at_topanswers.xyz Apr 14 '14 at 18:36

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


\usepackage{animate} %defines \@anim@newocg

%JavaScript action to be executed on page open event
\pdfpageattr{ /AA <<
  /O <<
    /S/JavaScript/JS (
      var l = bgOCGs.length;
      randomIndex = Math.floor(Math.random()*l)\%l;


  \newocg{bg}{\i}%  %create new ocg
  \immediate\pdfxform attr{/OC \lastOCGobj}\bgbox%
  \expandafter\xdef\csname cbox\i\endcsname{\the\pdflastxform}%
\setbeamertemplate{background canvas}{%
      \makebox[0pt][l]{\expandafter\pdfrefxform\csname cbox\i\endcsname}%

\begin{frame}{Frame 1}
\begin{frame}{Frame 2}
\begin{frame}{Frame 3}
\begin{frame}{Frame 4}
|improve this answer|||||

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. :-)

|improve this answer|||||

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.