# Savebox with \bgroup/\egroup doesn't work in beamer (for environment)

This works:

\documentclass{article}
\newsavebox{\mybox}
\begin{document}
\savebox{\mybox}\bgroup This is a test.\egroup\usebox{\mybox}
\end{document}


But this fails (Missing } inserted. at the beginning of the environment and Extra }, or forgotten \endgroup. at the end):

\documentclass{beamer}
\newsavebox{\mybox}
\begin{document}
\begin{frame}
\savebox{\mybox}\bgroup This is a test.\egroup\usebox{\mybox}
\end{frame}
\end{document}


Note I want the \bgroup and \egroup, and not { and }, because I'm trying to create a savebox environment:

\documentclass{article}
\usepackage{graphicx}
\newsavebox{\mybox}
\newenvironment{foo}{%
\savebox{\mybox}\bgroup%
}{%
\egroup\fbox{\scalebox{0.1}{\usebox{\mybox}}}%
}
\begin{document}
\begin{foo}
This is a test.
\end{foo}
\end{document}

1. Why does it fail with beamer?
2. How can I make it work (note life's a whole lot easier for me if the \begin{foo}/\end{foo} can be within a frame environment, but currently it still fails wherever I move the foo environment, having removed the usebox of course)?
-
Are you by any chance trying to do something similar to this tex.stackexchange.com/questions/47998/… ? –  percusse Sep 8 '12 at 16:41
None of the commands \mbox, \makebox, \fbox, \framebox, \sbox and \savebox accepts the contents enclosed in \bgroup and \egroup. They are ultimately based on \hbox, but they gather their (mandatory) argument as normal macros. –  egreg Sep 8 '12 at 17:48
@percusse Yes, I'm trying to do something similar to that, but automated (basically, I re-define the frame environment, and \input{\jobname}, to get a scaled version of some/all slides on a (few) slide(s)). See my question (we have agreed that the best was to copy the pdf and \includegraphics it, but I'm trying to do this without copying the pdf, just as a proof of concept since it's much more likely to break). –  Georges Dupéron Sep 8 '12 at 17:49
@GeorgesDupéron It doesn't actually work: try to use the box! If you load the color package, the accident 'working' (i.e. lack of error) is ended: you are seeing grouping in action! –  Joseph Wright Sep 8 '12 at 18:07
@GeorgesDupéron The only one that "works" is \mbox, but \mbox\bgroup a\egroup becomes \leavevmode\hbox{\bgroup}a\egroup, so it's only a coincidence that it makes a box. –  egreg Sep 8 '12 at 19:11

There is environment lrbox:

\documentclass{beamer}

\newsavebox{\mybox}
\newenvironment{foo}{%
\begin{lrbox}{\mybox}%
}{%
\end{lrbox}%
\fbox{\scalebox{.1}{\usebox{\mybox}}}%
}

\begin{document}
\begin{frame}

\begin{lrbox}{\mybox}This is a test.\end{lrbox}%
\usebox{\mybox}

\begin{foo}This is a test.\end{foo}

\end{frame}
\end{document}

-