%
% OverUnderBraces by Donald Arseneau (asnd@reg.triumf.ca)
% 
% From: asnd@erich.triumf.ca (Donald Arseneau)
% Newsgroups: comp.text.tex
% Subject: Re: Partially Overlapped Over- and Underbracing in Expressions
% Date: 4 Apr 1993 16:22 PST
% response to wayne@nimios.eng.mcmaster.ca (Wayne Dam)
%
% \overunderbraces{upper_brace_specifiers}{main_formula}{lower_brace_specifiers}
% (Definition should be where @ is a "letter," as in a style file.)
%
\newcommand\overunderbraces[3]{{% or \def\overunderbraces #1#2#3
 \baselineskip\z@skip \lineskip4pt \lineskiplimit4pt
 \displaystyle % deliberately generate error if not in math mode!
 \setbox\z@\vbox{\ialign{${}##{}$\hfil&&${}##{}$\hfil\crcr
   \global\let\br\br@label #1\crcr
   \global\let\br\br@down #1\crcr
   #2\crcr % stop after the main line
 }}% finished partial alignment and box.
 \dimen@-\ht\z@ %   Measure height -- it is the height we want for the whole
 \setbox\z@\vbox{\ialign{${}##{}$\hfil&&${}##{}$\hfil\crcr % do whole alignment
   \global\let\br\br@label #1\crcr
   \global\let\br\br@down #1\crcr
   #2\crcr
   \global\let\br\br@up #3\crcr
   \global\let\br\br@label #3\crcr
 }}% finished whole alignment and box.
 \advance\dimen@\ht\z@
 \lower\dimen@\hbox{\box\z@}% move alignment to get the desired height
}}
 
\def\br@up#1#2{\multispan{#1}\upbracefill}
\def\br@down#1#2{\multispan{#1}\downbracefill}
\def\br@label#1#2{\multispan{#1}\hidewidth $#2$\hidewidth}
%
\endinput
%
\documentstyle[ovudbraces]{article}
\setlength{\textheight}{24cm}
\setlength{\textwidth}{16cm}
\begin{document}
\title{{\sc OverUnderBraces}}
\author{Donald Arseneau(asnd@reg.triumf.ca)}
\date{April 4, 1993}
\maketitle
 
We want to interleave \verb+\overbrace+ with \verb+\underbrace+, as in:
\begin{verbatim}
               x               y
         ,-----^-----,   ,-----^-----,
 a + b + c + d + e + f + g + h + i + j + k + l + m = \pi r^2
                 `-----v-----'
                       z
\end{verbatim}
The solution is quite straightforward, but requires some care.
First, (on paper, or imagination) take the main line of the
formula and divide it into segments according to the braces.
There are seven segments:
\begin{verbatim}
               x               y
         ,-----^-----,   ,-----^-----,
 a + b + c + d + e + f + g + h + i + j + k + l + m = \pi r^2
 11111111222222223333344455555666666667777777777...
                 `-----v-----'
                       z
\end{verbatim}
Insert \& between each segment (where the tips of the braces point):
\begin{verbatim}
               x               y
         ,-----^-----,   ,-----^-----,
 a + b +&c + d +&e + f&+&g + h&+ i + j&+ k + l + m
                 `-----v-----'
                       z
\end{verbatim}
Count how many segments are spanned by each brace.  We will use
\verb+\multispan {n}{\upbracefill}+ for each brace, on separate lines
of an alignment.
 
First, let's show a flawed LaTeX solution to illustrate the method:
\begin{verbatim}
$\begin{array}[c]{ccccccc}
  & \multicolumn{2}{c}{x}&              & \multicolumn{2}{c}{y}\\
  & \multispan{2}{\downbracefill}& & \multispan{2}{\downbracefill}\\
 a + b +&c + d +&e + f&+&g + h&+ i + j&+ k + l + m\\
  & & \multispan{3}{\upbracefill}\\
  & & \multicolumn{3}{c}{z}
\end{array}=\pi r^2 $
\end{verbatim}
 
$\begin{array}[c]{ccccccc}
  & \multicolumn{2}{c}{x}&              & \multicolumn{2}{c}{y}\\
  & \multispan{2}{\downbracefill}& & \multispan{2}{\downbracefill}\\
 a + b +&c + d +&e + f&+&g + h&+ i + j&+ k + l + m\\
  & & \multispan{3}{\upbracefill}\\
  & & \multicolumn{3}{c}{z}
\end{array}=\pi r^2 $
 
The spacing is all wrong using {array} though, and vertical centering
does not give the right baselines.  Instead, we should use the primitive
\verb+\halign+ in a way similar to
 the definitions of \verb+\overbrace+ and \verb+\underbrace+;
and use "save" boxes to get the vertical positioning right.  Rather than
type the very messy TeX directly, it is better to make a new command:
 
\begin{verbatim}
% 
% \overunderbraces{upper_brace_specifiers}{main_formula}{lower_brace_specifiers}
% (Definition should be where @ is a "letter," as in a style file.)
%
\newcommand\overunderbraces[3]{{% or \def\overunderbraces #1#2#3
 \baselineskip\z@skip \lineskip4pt \lineskiplimit4pt
 \displaystyle % deliberately generate error if not in math mode!
 \setbox\z@\vbox{\ialign{${}##{}$\hfil&&${}##{}$\hfil\crcr
   \global\let\br\br@label #1\crcr
   \global\let\br\br@down #1\crcr
   #2\crcr % stop after the main line
 }}% finished partial alignment and box.
 \dimen@-\ht\z@ %   Measure height -- it is the height we want for the whole
 \setbox\z@\vbox{\ialign{${}##{}$\hfil&&${}##{}$\hfil\crcr % do whole alignment
   \global\let\br\br@label #1\crcr
   \global\let\br\br@down #1\crcr
   #2\crcr
   \global\let\br\br@up #3\crcr
   \global\let\br\br@label #3\crcr
 }}% finished whole alignment and box.
 \advance\dimen@\ht\z@
 \lower\dimen@\hbox{\box\z@}% move alignment to get the desired height
}}
 
\def\br@up#1#2{\multispan{#1}\upbracefill}
\def\br@down#1#2{\multispan{#1}\downbracefill}
\def\br@label#1#2{\multispan{#1}\hidewidth $#2$\hidewidth}
\end{verbatim}
 
So remember, we wanted
\begin{verbatim}
               x               y
         ,-----^-----,   ,-----^-----,
 a + b + c + d + e + f + g + h + i + j + k + l + m = \pi r^2
                 `-----v-----'
                       z
\end{verbatim}
We already saw how to break the formula into segments with \&
( a + b +\&c + d +\&e + f\&+\&g + h\&+ i + j\&+ k + l + m ) and we
counted the segments under (over) each brace.  Each brace should 
be specified by typing
\begin{verbatim}
   \br{number_of_segments}{label},
\end{verbatim}
separated by  \&  to indicate which segment to start at; and 
where  \& \&  skips a segment that has no brace.  For our
example formula, the upper and lower brace specifiers are:
\begin{verbatim}
top row:  &\br{2}{x}& &\br{2}{y}
bot row:  & &\br{3}{z}
\end{verbatim}
Now put them all together:
\begin{verbatim}
\overunderbraces{&\br{2}{x}& &\br{2}{y}}%
   {a + b +&c + d +&e + f&+&g + h&+ i + j&+ k + l + m}%
   {& &\br{3}{z}}
= \pi r^2
\end{verbatim}
\begin{equation}
\overunderbraces{&\br{2}{x}& &\br{2}{y}}%
   {a + b +&c + d +&e + f&+&g + h&+ i + j&+ k + l + m}%
   {& &\br{3}{z}}
= \pi r^2
\end{equation}
Presto!  It is not wysiwyg, nor is it discriptive, but it's not 
terribly hard to use.
\end{document}
