EDIT: sorry, I thought about it. As others pointed out, the correct fix is to declare the box ouside the definition of \repeatstuff.
This can be fixed by replacing \let\mybox\relax by \global\let\mybox\relax. Assignments in TeX can be either "local", i.e., restricted to the current group (for instance the current environment in LaTeX, or within an equation, etc.), or "global", i.e., applied even outside the current group, everywhere.
When an assignment has been made locally within a group, the old value present before the group started is restored when the group ends. In your case, \newsavebox does a global assignment, because boxes are a scarce resource, that shouldn't be wasted, but \let\mybox\relax is local. Thus, the first \newsavebox defines \mybox globally, then it is locally let to \relax, which allows the second definition. When the equation ends, it is back to the former (globally assigned) value, namely, a box register. The third \newsavebox thus encounters an already defined command, and complains. Once more, \let\mybox\relax does its job locally, allowing the next \newsavebox, etc.
The following assignments are local:
\def, \edef
\let, \futurelet
\chardef, \boxdef, \dimendef, \countdef, \toksdef
\setbox
\count123=... or \mycount=..., where \mycount is defined using \countdef (or \newcount, which is a wrapper around that, or \newcounter in LaTeX)
- other similar assignments with
\toks, or \dimen, etc.
- etc.
All of those assignments can be made global by prefixing them with \global. As a convenience, TeX also provides \gdef and \xdef for \global\def and \global\edef, respectively.
Explaining why \newsavebox defines \mybox globally requires some discussion of how registers are allocated in plain TeX and in LaTeX...
\repeatstuffyou call\newsavebox, hence the error. Move it outside the\newcommand. – yannisl May 13 '11 at 16:20\myboxprevent the error? – Ian Thompson May 13 '11 at 16:28