Math & Science\underbrace at a strange place

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
bbasic
Posts: 7
Joined: Tue Jul 08, 2008 4:21 pm

\underbrace at a strange place

Post by bbasic »

Take a look at the following code:

Code: Select all

$$\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{2em}\hspace{25.5pt}&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.

Thank you all for the suggestions.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
Stefan Kottwitz
Site Admin
Posts: 10358
Joined: Mon Mar 10, 2008 9:44 pm

\underbrace at a strange place

Post by Stefan Kottwitz »

This code is very hard to read. That's why I can understand that nobody answered. It's recommendable to use more spaces on math mode code, so the code can be read by humans.

For braces in very complex expressions, such as this array, we could use TikZ for marking places and for drawing braces from one mark to another. Here's an example: Highlighting elements in matrices with TikZ.

Your example with such a \tikzmark, rewritten to be nicer to read and without all those \raisebox, \makebox and \hspace commands:

Code: Select all

\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}
Output:
braces.png
braces.png (7.52 KiB) Viewed 4671 times
TikZ is a graphics package for LaTeX with excellent capabilities, even if we use it here just for a small purpose. It builds on pgf. Have a look at the TikZ manual if you are interested.

Stefan
LaTeX.org admin
Post Reply