| 1 |
\documentclass[11pt]{article} |
| 2 |
\usepackage{oubraces} |
| 3 |
\overfullrule=5pt |
| 4 |
\title{\textsf{oubraces.sty}\\[5pt] |
| 5 |
\large{Interleave \textbackslash overbrace with \textbackslash underbrace}} |
| 6 |
\author{Donald Arseneau} |
| 7 |
\date{2011 (1993)} |
| 8 |
|
| 9 |
\makeatletter |
| 10 |
|
| 11 |
% LaTeX article explaining \overunderbraces |
| 12 |
|
| 13 |
% hacked version with extra labels for this article: |
| 14 |
% \Xoverunderbraces{upper_brace}{main_formula}{lower_brace}{special_labels} |
| 15 |
% |
| 16 |
\newdimen \Xheight |
| 17 |
|
| 18 |
\def\Xoverunderbraces #1#2#3#4{{% #4 = special labels |
| 19 |
\baselineskip\z@skip \lineskip4pt \lineskiplimit4pt |
| 20 |
\displaystyle % generate error if not in math mode! |
| 21 |
% first, do the top half of the alignment in a save-box to measure the height |
| 22 |
\setbox\z@\vbox{\ialign{&\hfil${}##{}$\hfil\cr |
| 23 |
\global\let\br\br@label #1\cr % upper labels |
| 24 |
\global\let\br\br@down #1\cr % upper braces |
| 25 |
#2\cr % main line of the formula |
| 26 |
}}% finished partial alignment and \vbox. |
| 27 |
\dimen@-\ht\z@ % Measure height -- it is the height we want for the whole |
| 28 |
% second, do the whole alignment without the rules to find its size |
| 29 |
\Xheight\z@ |
| 30 |
\setbox\z@\vbox{\ialign{&\hfil${}##{}$\hfil\cr |
| 31 |
\global\let\br\br@label #1\cr % upper labels |
| 32 |
\global\let\br\br@down #1\cr % upper braces |
| 33 |
#2\cr % main line of the formula |
| 34 |
\global\let\br\br@up #3\cr % lower braces |
| 35 |
\global\let\br\br@label #3\cr % lower labels |
| 36 |
\noalign{\kern4pt}% Extra space |
| 37 |
#4\cr % special labels |
| 38 |
}}% finished whole alignment and \vbox. |
| 39 |
\Xheight\ht\z@ \global\let\XV\relax |
| 40 |
% finally, do the whole alignment yet again in its final format |
| 41 |
\setbox\z@\vbox{\ialign{\XV\hfil${}##{}$\hfil\XV&&\hfil${}##{}$\hfil\XV\cr |
| 42 |
\global\let\br\br@label #1\cr % upper labels |
| 43 |
\global\let\br\br@down #1\cr % upper braces |
| 44 |
#2\cr % main line of the formula |
| 45 |
\global\let\br\br@up #3\cr % lower braces |
| 46 |
\global\let\br\br@label #3\cr % lower labels |
| 47 |
\noalign{\kern4pt \global\let\XV\XVR}% Extra space |
| 48 |
#4\cr % special labels |
| 49 |
}}% finished whole alignment and \vbox. |
| 50 |
\advance\dimen@\ht\z@ |
| 51 |
\lower\dimen@\hbox{\box\z@}% move alignment to get the desired height |
| 52 |
}} |
| 53 |
|
| 54 |
\def\XVR{\kern-.05pt\vbox to\z@{\vss\hrule width.1pt height\Xheight}\kern-.05pt} |
| 55 |
|
| 56 |
\begin{document} |
| 57 |
|
| 58 |
\maketitle |
| 59 |
|
| 60 |
\parskip=4pt |
| 61 |
\linespread{1.05}\selectfont |
| 62 |
|
| 63 |
\noindent Sometimes one would like to interleave braces labelling |
| 64 |
parts of an equation, like in |
| 65 |
$$ |
| 66 |
\overunderbraces{&\br{2}{x}& &\br{2}{y}}% |
| 67 |
{a + b +&c + d +&e + f&+&g + h&+ i + j&+ k + l + m}% |
| 68 |
{& &\br{3}{z}} |
| 69 |
= \pi r^2 |
| 70 |
$$ |
| 71 |
which is something that straightforward \verb"\overbrace" and |
| 72 |
\verb"\underbrace" cannot do. |
| 73 |
|
| 74 |
There is a trick, which I heard from Etienne Riga, that works well |
| 75 |
for simple cases without requiring any packages or new commands, and |
| 76 |
it involves \emph{overprinting} parts of the equation: |
| 77 |
put an overlapping \verb"\overbrace" (or \verb"\underbrace") |
| 78 |
into \verb"\rlap{$...$}", and insert it into the equation. |
| 79 |
This does not easily handle the multiple-overlap of the example |
| 80 |
above (the overlapping gets misaligned) but can do |
| 81 |
$$ |
| 82 |
\rlap{$\overbrace{A+B}^x$}A+\underbrace{B+C}_z |
| 83 |
$$ |
| 84 |
with \verb"\rlap{$\overbrace{A+B}^x$}A+\underbrace{B+C}_z". |
| 85 |
|
| 86 |
The oubraces package provides an alternative solution (for both plain |
| 87 |
\TeX\ and \LaTeX) based on visual formatting into columns, like an array |
| 88 |
or table. The formula at top is then produced by |
| 89 |
\begin{verbatim} |
| 90 |
\overunderbraces{&\br{2}{x}& &\br{2}{y}}% |
| 91 |
{a + b +&c + d +&e + f&+&g + h&+ i + j&+ k + l + m}% |
| 92 |
{& &\br{3}{z}} |
| 93 |
= \pi r^2 |
| 94 |
\end{verbatim} |
| 95 |
|
| 96 |
First, (on paper or imagination) write the formula |
| 97 |
and divide it into segments at the tip of each brace. |
| 98 |
In our example there are seven segments: |
| 99 |
$$ |
| 100 |
\Xoverunderbraces{&\br{2}{x}& &\br{2}{y}}% |
| 101 |
{a + b +&c + d +&e + f&+&g + h&+ i + j&+ k + l + m}% |
| 102 |
{& &\br{3}{z}}% |
| 103 |
{1&2&3&4&5&6&7} |
| 104 |
= \pi r^2 |
| 105 |
$$ |
| 106 |
although segments 1 and 7 could be omitted. |
| 107 |
We will type the main line of the formula with \verb"&" inserted |
| 108 |
between each segment: |
| 109 |
\begin{verbatim} |
| 110 |
a + b +&c + d +&e + f&+&g + h&+ i + j&+ k + l + m |
| 111 |
\end{verbatim} |
| 112 |
Each segment then becomes a column in an alignment, |
| 113 |
and each brace may span multiple columns. The idea is best |
| 114 |
illustrated by a \LaTeX{} array: |
| 115 |
\begin{verbatim} |
| 116 |
\begin{array}[c]{ccccccc} |
| 117 |
& \multicolumn{2}{c}{x}& & \multicolumn{2}{c}{y}\\ |
| 118 |
& \multicolumn{2}{c}{\downbracefill}& |
| 119 |
& \multicolumn{2}{c}{\downbracefill}\\ |
| 120 |
a + b +&c + d +&e + f&+&g + h&+ i + j&+ k + l + m\\ |
| 121 |
& & \multicolumn{3}{c}{\upbracefill}\\ |
| 122 |
& & \multicolumn{3}{c}{z} |
| 123 |
\end{array} = \pi r^2 |
| 124 |
\end{verbatim} |
| 125 |
This doesn't work well at all, having bad braces with bad vertical and |
| 126 |
horizontal spacing. We should instead use \TeX's primitive |
| 127 |
\verb"\halign" command, and measure the size of boxes to get the |
| 128 |
correct vertical positioning of the formula's main line. Rather |
| 129 |
than type the very messy \TeX\ commands directly with each use, |
| 130 |
it is better to make a new command: |
| 131 |
$$ |
| 132 |
\hbox to\hsize{\indent\verb"\overunderbraces{"{\sl |
| 133 |
upper\_braces\/}\verb"}{"{\sl main\_formula\/}\verb"}{"{\sl |
| 134 |
lower\_braces\/}\verb"}"\hfil} |
| 135 |
$$ |
| 136 |
We already know how to write the main line of the formula |
| 137 |
(by segmenting the formula and inserting \verb"&" between |
| 138 |
segments), and we will also use \verb"&" between braces. |
| 139 |
Each labelled brace will be specified in the format: |
| 140 |
$$ |
| 141 |
\hbox to\hsize{\indent\verb"\br{"$n$\verb"}{"\mbox{\sl |
| 142 |
label\/}\verb"}"\hfil} |
| 143 |
$$ |
| 144 |
where $n$ is the number of segments spanned by the brace. |
| 145 |
Each top brace of our example spans two segments (with an |
| 146 |
empty segment between them) and the bottom |
| 147 |
brace spans three segments, so they are specified by: |
| 148 |
|
| 149 |
\medskip |
| 150 |
\indent \hbox to8em{top braces:\hfil}\verb" &\br{2}{x}& &\br{2}{y}"\\ |
| 151 |
\indent \hbox to8em{bottom braces:\hfil}\verb" & &\br{3}{z}" |
| 152 |
|
| 153 |
\medskip |
| 154 |
|
| 155 |
Having determined all three parameters to use with |
| 156 |
\verb"\over"\-\verb"under"\-\verb"braces", the complete command is |
| 157 |
\begin{verbatim} |
| 158 |
\overunderbraces{&\br{2}{x}& &\br{2}{y}}% |
| 159 |
{a + b +&c + d +&e + f&+&g + h&+ i + j&+ k + l + m}% |
| 160 |
{& &\br{3}{z}} |
| 161 |
= \pi r^2 |
| 162 |
\end{verbatim} |
| 163 |
which produces the equation at the start of this article. |
| 164 |
|
| 165 |
\end{document} |
| 166 |
|
| 167 |
Here is the definition of \verb"\overunderbraces". It may look |
| 168 |
long, but it has some repetition in order to measure the height of |
| 169 |
the top half of the formula. There are also the auxiliary macros |
| 170 |
\verb"\brAup", \verb"\brAdown", and \verb"\brAlabel" that allow |
| 171 |
the same \verb"\br" command to produce up-braces, down-braces, and |
| 172 |
centered labels at the appropriate places. |
| 173 |
|
| 174 |
\begin{verbatim} |
| 175 |
% \overunderbraces{upper_braces}{main_formula}{lower_braces} |
| 176 |
% |
| 177 |
\def\overunderbraces #1#2#3{{% |
| 178 |
\baselineskip=0pt \lineskip=4pt \lineskiplimit=4pt |
| 179 |
\displaystyle % generate error if not in math mode! |
| 180 |
% first, do the top half of the alignment in a save-box |
| 181 |
\setbox0=\vbox{\ialign{&\hfil${}##{}$\hfil\cr |
| 182 |
\global\let\br\brAlabel #1\cr % upper labels |
| 183 |
\global\let\br\brAdown #1\cr % upper braces |
| 184 |
#2\cr % main line of the formula |
| 185 |
}}% finished partial alignment and \vbox. |
| 186 |
\dimen0=-\ht0 % Measure height of partial alignment -- |
| 187 |
% ... it is the height we want for the whole. |
| 188 |
% Now do the whole alignment (notice the repetition from above) |
| 189 |
\setbox0=\vbox{\ialign{&\hfil${}##{}$\hfil\cr |
| 190 |
\global\let\br\brAlabel #1\cr % upper labels |
| 191 |
\global\let\br\brAdown #1\cr % upper braces |
| 192 |
#2\cr % main line of the formula |
| 193 |
\global\let\br\brAup #3\cr % lower braces |
| 194 |
\global\let\br\brAlabel #3\cr % lower labels |
| 195 |
}}% finished whole alignment and \vbox. |
| 196 |
\advance\dimen0 by\ht0 % calc. the necessary lowering |
| 197 |
\lower\dimen0\hbox{\box0}% shift to get the desired height |
| 198 |
}} |
| 199 |
|
| 200 |
% Three aliases for \br. |
| 201 |
% #1=number of spanned columns, #2=label |
| 202 |
\def\brAup#1#2{\multispan{#1}\upbracefill} |
| 203 |
\def\brAdown#1#2{\multispan{#1}\downbracefill} |
| 204 |
\def\brAlabel#1#2{\multispan{#1}\hidewidth $#2$\hidewidth} |
| 205 |
\end{verbatim} |
| 206 |
|
| 207 |
If you check the definitions in oubraces.sty, you will |
| 208 |
find some streamlining with `internal' commands such as \verb"\dimen@" |
| 209 |
used for \verb"\dimen0". |
| 210 |
|
| 211 |
\end{document} |