6

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)?
Suzanne Soy
  • 3,043
  • Are you by any chance trying to do something similar to this http://tex.stackexchange.com/questions/47998/repeat-scaled-version-of-slide-savebox-and-usebox-in-multi-slide-frame ? – percusse Sep 08 '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 08 '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). – Suzanne Soy Sep 08 '12 at 17:49
  • @egreg why does it work with \documentclass{article} then? Just curious. – Suzanne Soy Sep 08 '12 at 17:50
  • @GeorgesDupéron What do you mean? Of course \documentclass\bgroup article\egroup raises an error. – egreg Sep 08 '12 at 17:53
  • @egreg I mean that when I use \documentclass{article}, the \newenvironment{foo}{\savebox{\mybox}\bgroup}{\egroup} works, but if I use \documentclass{beamer} it fails when used (see my two MWE). If the problem is that \savebox doesn't accept content enclosed in \bgroup and egroup like you said, shouldn't it fail also in an article? – Suzanne Soy Sep 08 '12 at 17:59
  • 3
    @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 08 '12 at 18:07
  • 1
    @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 08 '12 at 19:11

1 Answers1

7

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}
Heiko Oberdiek
  • 271,626