# The argument of foo has an extra }.

I'm trying to write a command that does, effectively, an \fbox in something like an align environment. The following code works:

\usepackage{etextools}
\makeatletter
\newcommand*{\alignedfbox}[1]{\@aligedfbox{#1}{}#1&\@endalignedfbox}
\def\@aligedfbox#1#2#3&#4\@endalignedfbox{%
\ifempty{#4}{%
\vrule
\edef\temp{\expandafter\unexpanded\expandafter{\@@aligedfbox{#2#3}#1&\@@endalignedfbox}}%
\temp
}{%
\edef\temp{\expandafter\unexpanded\expandafter{\@aligedfbox{#1}{#2#3}#4\@endalignedfbox}}%
\temp
}%
}
\def\@@aligedfbox#1#2&#3\@@endalignedfbox{%
\underline{\overline{{}#2\vphantom{#1}}}
\ifempty{#3}{%
\vrule
}{%
&
\edef\temp{\expandafter\unexpanded\expandafter{\@@aligedfbox{#1}#3\@@endalignedfbox}}%
\temp
}%
}


and then

\begin{align*}
\alignedfbox{1 & = 2}
\end{align*}


However, the \edef\temp{\expandafter\unexpanded\expandafter{...}}\temp sequence is very ugly, and I'm not sure why I need it. (But if I replace it with ..., it fails with the error mentioned in the title of this post, that my arguments to \@alignedfbox/\@@alignedfbox have an extra }. Surprisingly, this error does not occur when I call \@aligedfbox{#1}{}#1&\@endalignedfbox} from \alignedfbox.) I think it has something to do that align and friends gather everything inside the environment into a macro before execution, and something to do with \protect/\protected and how TeX's behavior in expansion-only contexts is different from its behavior in other contexts, but I'm not sure.

What's a replacement for \edef\temp{\expandafter\unexpanded\expandafter{...}}\temp, and what's an explanation for why ... doesn't work by itself, in this instance?

-
I discovered that I can change the \edef\temp{\expandafter\unexpanded\expandafter{...}}\temp in \@@aligedfbox to \expandafter\relax.... I have no idea why this is the case, nor why I can't do the same in \@aligedfbox. – Jason Gross Apr 6 '11 at 9:41
Have you tried the \Aboxed command from the mathtools package, it does something similar, but less complicated – daleif Apr 6 '11 at 10:13
The \Aboxed command from mathtools works. Thanks! But it doesn't answer my request for an explanation of why I'm getting those errors. – Jason Gross Apr 6 '11 at 11:15
sorry, no time to investigate – daleif Apr 6 '11 at 13:00

## 2 Answers

As already suggested, you shouldn't reenvent the wheel ...

\documentclass{article}
\usepackage{mathtools}
\begin{document}

\begin{align*}
\Aboxed{1 &= 2 }\\
1 &\ne 2
\end{align*}

\end{document}


-
Actually there is apparently an error in it (I wrote it ;-(, try \Aboxed{\int_4 &= 2 } – daleif Apr 6 '11 at 10:41
Always the same old story ... \protect\int... – Herbert Apr 6 '11 at 10:44
not really, I used $\displaystyle#1{}$, that {} is irrelevant – daleif Apr 6 '11 at 10:49
I do not understand anything what you are talking about – Herbert Apr 6 '11 at 10:51
Likewise, what should \protect\int help? See my answer above, there the internal of \Aboxed is fixed – daleif Apr 6 '11 at 10:52

This seems better (will be fixed in the next mathtools)

\documentclass{article}
\usepackage{mathtools}
\makeatletter
\def\@Aboxed#1&#2\ENDDNE{%
\settowidth\@tempdima{$\displaystyle#1$}% <-
\addtolength\@tempdima{\fboxsep}%
\addtolength\@tempdima{\fboxrule}%
\global\@tempdima=\@tempdima
\kern\@tempdima
&
\kern-\@tempdima
\boxed{#1#2}
}
\makeatother
\begin{document}

\begin{align*}
\Aboxed{1 &= 2 }\\
1 &\ne 2
\end{align*}
\end{document}

-