# \underbrace at a strange place, spanning array columns

Take a look at the following code:

$$\begin{array}{rr|r|c|l} vd_2:&\raisebox{0pt}[0pt]{\makebox[0pt][l]{\overbrace{\phantom{b_1\dots b_{n-l_{k-1}-2k}\hspace{2\arraycolsep}b_{n-l_{k-1}-2k+1}\dots b_{n-k-1}b_{n-k}\hspace{2\arraycolsep}c^k}}^v}}b_1\dots b_{n-l_{k-1}-2k}&b_{n-l_{k-1}-2k+1}\dots b_{n-k-1}b_{n-k}&c^k&d_2\\ \mathrm{PW}(w):&\dots w_r c^{r+1-k}&c^k\raisebox{0pt}[0pt][0pt]{\makebox[0pt][l]{\underbrace{\phantom{w_{k-1}\hspace{4.17em}\hspace{2\arraycolsep}c^k\hspace{2\arraycolsep}\widetilde{w_{k-1}}\dots}}_{w_r}}}w_{k-1}\hspace{4.17em}&c^k&\widetilde{w_{k-1}}\dots\dots \end{array}$$


I would like to draw the \underbrace in a more elegant way. The main problem is how to make sure that $c^kw_{k-1}$ is in the dead center of the column. In the proposed code, it has been done artifically: the empty space of length 4.17em is added to the right of $c^kw_{k-1}$, where the value 4.17em is obtained by trial-and-error. This works nicely in 12pt, but if the font size is changed, it slides off the center. Even if there were not a problem with changing the font size, it would seem logical that there is a nicer way to accomplish this effect, instead of guessing the value 4.17em (or whichever works for the particular font size) until a good enough pixel-precision is reached.

-
Welcome to TeX.SE. While code snippets are useful, it is better to compose a full MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. – Peter Grill Nov 19 '11 at 0:26
Sidenote: you shouldn't use $$..$$, see tex.stackexchange.com/questions/503/why-is-preferable-to – Torbjørn T. Nov 19 '11 at 0:29

This is fairly straight forward to do using the savepos module from the zref package. It allows you to place markers (or labels) in the text and retrieve their coordinates (x and y) in small point (or sp) units. The code is also much cleaner without the box adjustments you used.

Taking your code, I've defined the labels L to denote the left-most entry in your array (as a frame of reference), TL and TR to be the left and right \overbrace coordinate in the top row, and BL and BR to be the left and right \underbrace coordinate in the bottom row.

Then I construct an invisible table before typesetting the actual table. The invisible is overlapped by the actual table using \mathrlap (to produce a right overlap in math mode) from the mathtools package. Finally, spacing is obtained by \hspaces as needed. Vertical alignment is rectified using \strut, since there is otherwise no content to adjust the height/depth above/below the \overbrace/\underbrace. Using \mathstrut instead results in slightly weaker vertical alignment:

\documentclass{article}
\usepackage{mathtools}% http://ctan.org/pkg/mathtools
\usepackage[savepos]{zref}% http://ctan.org/pkg/zref
\begin{document}
$\mathrlap{\begin{array}{l} \hspace{\dimexpr\zposx{TL}sp-\zposx{L}sp\relax}\overbrace{\strut\hspace{\dimexpr\zposx{TR}sp-\zposx{TL}sp\relax}}^v \\ \hspace{\dimexpr\zposx{BL}sp-\zposx{L}sp\relax}\underbrace{\strut\hspace{\dimexpr\zposx{BR}sp-\zposx{BL}sp\relax}}_{w_r} \end{array}}% \begin{array}{rr|c|c|l} vd_2: & \zsavepos{TL}b_1\dots b_{n-l_{k-1}-2k} & b_{n-l_{k-1}-2k+1}\dots b_{n-k-1}b_{n-k} & c^k\zsavepos{TR} & d_2 \\ \zsavepos{L}\mathrm{PW}(w): & \dots w_r c^{r+1-k} & c^k\zsavepos{BL}w_{k-1} & c^k & \widetilde{w_{k-1}}\dots\zsavepos{BR}\dots \end{array}$

$\begin{array}{rr|r|c|l} vd_2:&\raisebox{0pt}[0pt]{\makebox[0pt][l]{\overbrace{\phantom{b_1\dots b_{n-l_{k-1}-2k}\hspace{2\arraycolsep}b_{n-l_{k-1}-2k+1}\dots b_{n-k-1}b_{n-k}\hspace{2\arraycolsep}c^k}}^v}} b_1\dots b_{n-l_{k-1}-2k}&b_{n-l_{k-1}-2k+1}\dots b_{n-k-1}b_{n-k}&c^k&d_2\\ \mathrm{PW}(w):&\dots w_r c^{r+1-k}&c^k\raisebox{0pt}[0pt][0pt]{\makebox[0pt][l]{\underbrace{\phantom{w_{k-1}\hspace{4.17em}\hspace{2\arraycolsep}c^k\hspace{2\arraycolsep}\widetilde{w_{k-1}}\dots}}_{w_r}}} w_{k-1}\hspace{4.17em}&c^k&\widetilde{w_{k-1}}\dots\dots \end{array}$
\end{document}​


Above is new zref version, while your original construction is given below. Since this uses zref and positioning labels, you have to compile (at least) twice for the referencing to be correct.

-
+1: Nice hack! :) – Count Zero Nov 19 '11 at 11:27
Thank you. It works exactly as I intented. The only problem I have with it is that it suggests including two (I believe rather cumbersome) packages, and I have the described problem with only one figure in the whole article. I see that mathtools can be avoided by drawing the \underbrace in the way I originally did, while determining its position using zref as you suggested. I have yet to think about whether to use this way, or to, as I later find out, replace \hspace{4.17em} with \hspace{2em}\hspace{25.5pt}—it turns out that this works quite well for other font sizes, too. – bbasic Nov 19 '11 at 16:13
@bbasic: mathtools provided \mathrlap, which is a very small part of the package. It would be possible to extract just that part, or use \rlap instead (but then you have to escape to math mode manually). My solution, however, is fundamentally driven by zref and therefore won't work without it. – Werner Nov 19 '11 at 16:20

if you need it only sometimes then use a workaround with \multicolumn:

\documentclass{article}
\begin{document}
$\begin{array}{rr|c|c|l} & \multicolumn{3}{c}{\overbrace{\rule{0.65\columnwidth}{0pt}}^v}\\ vd_2: & b_1\dots b_{n-l_{k-1}-2k} & b_{n-l_{k-1}-2k+1}\dots b_{n-k-1}b_{n-k} & c^k & d_2\\ \mathrm{PW}(w): & \dots w_r c^{r+1-k} & c^kw_{k-1} & c^k &\widetilde{w_{k-1}}\dots\dots\\[-0.5\normalbaselineskip] & \multicolumn{4}{r}{\underbrace{\rule{0.4\columnwidth}{0pt}}_{w_r}\quad} \end{array}$
\end{document}


-
Thank you for your suggestion. Unfortunately, it does not really fit my needs. I want everything to be placed very precisely, while your idea introduces too many rather arbitrary parameters (0.65\columnwidth, -0.5\normalbaselineskip, 0.4\columnwidth, and the \quad at the end is especially unsatisfying, since it makes to look like that the \underbrace encompasses four dots, not three as needed). – bbasic Nov 19 '11 at 16:23

The problem has been cross-posted to LaTeX-Community.org and solved. As Thorsten pointed me to the cross-post here, I'll add my TikZ underbrace solution here too, based on \tikzmark which has often been used here:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture,baseline=(#1.base)]
\node (#1) {\strut};}
\begin{document}
$\begin{array}{rr|c|c|l} vd_2: &\tikzmark{topleft}b_1\dots b_{n-l_{k-1}-2k} & b_{n-l_{k-1}-2k+1}\dots b_{n-k-1}b_{n-k} & c^k \tikzmark{topright} & d_2 \\ \mathrm{PW}(w): & \dots w_r c^{r+1-k} & \tikzmark{bottomleft} c^k w_{k-1} & c^k &\widetilde{w_{k-1}}\dots\dots\tikzmark{bottomright} \end{array}$
\tikz[overlay, remember picture, decoration={brace, amplitude=10pt}] {
\draw[decorate,thick] (topleft.north) -- (topright.north)
node [midway,above=10pt] {$v$};
\draw[decorate,thick] (bottomright.south) -- (bottomleft.south)
node [midway,below=10pt] {$w_r$};
}
\end{document}


-