# Why a calc operation in width of beamercolorbox doesn't work?

Trying to answer How to add the footnote in the right down corner with the background color(only page)&change time background color I wanted to calculate beamercolorbox width as wd=\pagewidth-\beamer@sidebarwidth but if failed, even with \usepackage{calc}. Do you know why?

Next you have an example to test it. First solution (with calc) doesn't work, second and third (with \addtolength and pgf-calc library) work.

\documentclass[14pt]{beamer}
\usetheme{Berkeley}
\usepackage{calc}

\begin{document}
\begin{frame}
% wd=5cm-1cm does not work
\begin{beamercolorbox}[wd=5cm-1cm]{sidebar}
First box
\end{beamercolorbox}

\newlength{\mylength}
\setlength{\mylength}{5cm}
\begin{beamercolorbox}[wd=\mylength]{sidebar}
First box
\end{beamercolorbox}

\pgfmathsetmacro{\lengthb}{5-1}
\begin{beamercolorbox}[wd=\lengthb cm]{sidebar}
First box
\end{beamercolorbox}
\end{frame}

\end{document}


-
beamer is pre-pgfkeys era. I didn't check it but probably the wd= is directly stored in a dimen and there is no possibility to catch the calculation. You might try \dimexpr...\relax if luck is on our side it should work :) – percusse Oct 23 '13 at 8:54
@percusse That's an answer right there. – Jubobs Oct 23 '13 at 9:02
@percusse So, this is the normal behaviour. Isn't it? It's is not possible to use calc to set beamercolor boxes size. – Ignasi Oct 24 '13 at 8:01
The calc package is nice but not really bullet-proof. I would stick to \dimexpr construct in this case. @percusse I think you should make it an answer. – yo' Oct 24 '13 at 14:51
@percusse: sir, you should make an answ... oh wait. :) – Paulo Cereda Oct 24 '13 at 16:05

The PGF-like keys being filtered whether the argument is a dimen or a computation or else type of branching came later chronologically. Thus beamer, in a sense, predates the pgfkeys way of working and we should not expect much of the flexibility from beamer options as they are quite esoteric which might be, arguably, the reason why pgfkeys are developed in the first place.

In this case the key gets the argument literally and tries to give it to a dimen but the 5cm causes TeX to stop scanning and you have the dangling -1cm printed. A possible solution is to make sure that the dimen computation survives via e-TeX extension \dimexpr...\relax (manual section 3.5).

\begin{beamercolorbox}[wd=\dimpexr5cm-1cm\relax]{sidebar}
First box
\end{beamercolorbox}

-

The reasons wd=5cm-1cm fails are the following:

Internally 5cm-1cm is directly stored in a macro \beamer@colbox@wd, this is then later used to set the \hsize to:

\hsize=\beamer@colbox@wd


At no point can either PGFmath or calc interact. For the latter, there needs to be a \setlength call, so

\usepackage{etoolbox}
\expandafter\patchcmd\csname\string\beamercolorbox\endcsname
{\hsize=\beamer@colbox@wd}
{\setlength\hsize{\beamer@colbox@wd}}
{}{}


would do.

For this to work with PGFmath we could substitute to

\expandafter\patchcmd\csname\string\beamercolorbox\endcsname
{\hsize=\beamer@colbox@wd}
{\pgfmathsetlength\hsize{\beamer@colbox@wd}}
{}{}


replacing LaTeX’s (and calc’s) \setlength with \pgfmathsetlength which would make it possible to use any PGFmath option in the wd key.

Both of these solutions will fail for rounded boxes (rounded=true) because the \beamer@colbox@wd macro is used again which we could solve by also fixing another occurrences of either \beamer@colbox@wd or \bmb@width.

It might be easier to simply redefine the wd key so that it does immediately evaluate the value:

\define@key{beamercolbox}{wd}{\pgfmathsetlengthmacro\beamer@colbox@wd{#1}}


or, for calc, something like

\define@key{beamercolbox}{wd}{\setlength\pgfutil@tempdima{#1}% any "temp" is okay
\edef\beamer@colbox@wd{\the\pgfutil@tempdima}}


Obviously this needs to be done for other keys, too, if you want to use them; in the beamercolbox namespace there are also ht, dp, leftskip and rightskip. If you use those calculations often, it might be worth it (and it also might be better to ask the developers); otherwise use the already discussed \dimexpr solution.

-
Uf! I just wanted to be sure that i wasn't doing anything wrong. In any case I'll try to understand your answer and apply in future themes. Thank you. – Ignasi Oct 24 '13 at 20:38
@Ignasi You weren’t doing anything wrong. The keys from the beamercolorbox environment simply aren’t setup for dimension calculation. My answer shows how one would correct that (at best in the source files directly), for a one-time problem, use \dimexpr. :) – Qrrbrbirlbel Oct 25 '13 at 19:10