[Tex/LaTex] How to use beamer colors in beamerarticle

beamerbeamerarticlecolor

I would like to use beamer defined colors into beamerarticle's version of my document. This way I can change beamer theme (or color theme) and colors change according it.

Here you have a MWE. A frame with a tikz figure using beamer colors fg and bg.

\documentclass[ignorenonframetext]{beamer}

%\documentclass{article}
%\usepackage{beamerarticle}

\usepackage{tikz}
\usecolortheme{beaver}

\begin{document}
This is some text outside any frame.

\begin{frame}{Frame}
\begin{alertblock}{Block}
\usebeamercolor[fg]{example text}
\begin{tikzpicture}
\filldraw[color=fg, fill=bg!50!white] (0,0) rectangle (5,3);
\draw[line width=5mm] (1,1)--(2,2);
\end{tikzpicture}
\end{alertblock}
\end{frame}
\end{document}

As soon as I try article with beamerarticle package, pdflatex complains with

! Package xcolor Error: Undefined color `fg'.

See the xcolor package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.16 ..., fill=bg!50!white] (0,0) rectangle (5,3);

The example contains a tikz figure but my question want to be more general, something like declaring sections (or any other text) with a beamer color and keeping this color in article mode.

Best Answer

Having just wanted to do this in answer to another question, I can tell you that this is Not Intended To Be Done! A lot of the theming and colour commands are simply set to "do nothing" when the article version of beamer is used. However, using a bit of trickery we can reload the commands and so make the colours available. This should be used with Extreme Caution as it might break something that I haven't tested (which is about everything).

Here's a picture based on a slightly modified version of your example:

beamer colours in article mode

And here's the code:

\documentclass{standalone}
%\url{http://tex.stackexchange.com/q/22768/86}
\PassOptionsToPackage{svgnames}{xcolor}
\usepackage{beamerarticle}
\usepackage{tikz}

\makeatletter

\def\beamer@currentmode{beamer}
\def\beamer@colorhook{}
\newskip\beamer@lastskip
\let\beamercolorbox=\relax
\let\endbeamercolorbox=\relax
\let\usetheme=\relax
\let\usecolortheme=\relax
\let\usefonttheme=\relax
\let\useoutertheme=\relax
\let\useinnertheme=\relax

\input{beamerbasethemes.sty}
\input{beamerbasecolor.sty}
\usecolortheme{default}
\usecolortheme{beaver}
\makeatother

\setbeamercolor{example text}{bg=Maroon}

\begin{document}

This is some text outside any frame.

\begin{frame}{Frame}
\begin{alertblock}{Block}
\usebeamercolor{example text}%
\begin{tikzpicture}
\filldraw[color=fg, fill=bg!50!white] (0,0) rectangle (5,3);
\draw[line width=5mm] (1,1)--(2,2);
\end{tikzpicture}
\end{alertblock}
\end{frame}
\end{document}

The main part is the stuff between \makeatletter and \makeatother. We need to load in the beamer colour management. Unfortunately, the file containing the commands checks the mode and skips most stuff in article mode. So we need to temporarily change to beamer mode to persuade it that we really do want to define this stuff. But as the files have already been read in (via the \usepackage{beamerarticle}) we can't \usepackage them again but have to \input them (this produces a couple of warnings in the log, but ignore them). We load in the themes set and the color set. There are a couple of things that are defined in beamer.cls that these depend on so we have to set them up: \beamer@colorhook and \beamer@lastskip. Also, some things are defined by \newcommand and were defined (to the wrong things) on the first read. So we need to set them all to \relax to avoid errors. After that, it's plain sailing!