5

This question led to a new package:
underoverlap

I've been using the \overunderbrace macro provided in this answer. It works fine for simple examples, but since the original definition uses \vcenter, vertical alignment is not correct when the axis of the 'content' is not aligned to its vertical center. For example, to get proper alignment in the following formula, I had to do some manual tweaking with \raisebox. Not ideal.

  enter image description here

I'm trying to perfect the macro; perhaps write a small package. (There are several extensions possible, e.g., \underoverbrace, custom vertical spacing, custom brace-command (\underoverline), etc.)

Question: Rather than 'naively' using \vcenter, is there a way to raise/lower the whole structure the exact amount needed to align its axis with the axis of the 'content' (which is a row in a \halign)?

Several possible solutions come to mind:

  • The simplest (for me) would be to add two optional arguments to the macro for the content before and after the bracing. I could then put them in the 'content row'. But I'd rather not have to.
  • If I have to, I can probably measure the segments and calculate the correct amount to raise/lower.

I'm just hoping there's a simpler solution I'm not yet aware of.


Edit: Here's a simple MWE of the misalignment, based on the example of the original answer:

\documentclass{article}

\makeatletter
\def\overunderbrace#1#2#3{%
    \begingroup
    \let\overunderbrace@sup\empty
    \let\overunderbrace@sub\empty
    \@ifnextchar^%
        {\@overunderbracesup{#1}{#2}{#3}}%
        {\@ifnextchar_%
            {\@overunderbracesub{#1}{#2}{#3}}%
            {\@overunderbrace{#1}{#2}{#3}}%
        }%
}

\def\@overunderbracesup#1#2#3^#4{%
    \def\overunderbrace@sup{#4}%
    \@ifnextchar_%
        {\@overunderbracesub{#1}{#2}{#3}}%
        {\@overunderbrace{#1}{#2}{#3}}%
}

\def\@overunderbracesub#1#2#3_#4{%
    \def\overunderbrace@sub{#4}%
    \@ifnextchar^%
        {\@overunderbracesup{#1}{#2}{#3}}%
        {\@overunderbrace{#1}{#2}{#3}}%
}

\def\@overunderbrace#1#2#3{%
\mathop {\vcenter {\m@th \ialign {##&##&##\crcr
    \noalign {\kern 3\p@}%
    \span\omit\hfil\hbox{\vbox to 0pt{\vss\hbox{\vbox{\hbox{$\m@th\scriptstyle\overunderbrace@sup$}\vspace{0pt}}}}}\hfil
    &%
    \crcr \noalign {\kern 5\p@\nointerlineskip}%
    \span\omit\downbracefill&%
    \crcr \noalign {\kern 3\p@\nointerlineskip}%
        $\hfil \displaystyle {#1}\hfil $&%
        $\hfil \displaystyle {#2}\hfil $&%
        $\hfil \displaystyle {#3}\hfil $%
    \crcr \noalign {\kern 3\p@\nointerlineskip}%
      & \span\omit  \upbracefill
    \crcr \noalign {\kern 5\p@\nointerlineskip}%
    &\span\omit\hfil\hbox{\vbox to 0pt{\hbox{\vbox{\vspace{0pt}\hbox{$\m@th\scriptstyle\overunderbrace@sub$}}}\vss}}\hfil
    \crcr \noalign {\kern 3\p@ }%
}}}%
    \endgroup
}
\makeatother


\begin{document}
    $$
        a+b+
        \overunderbrace
            {c+\mathord{\mathop{\Sigma}_d} + \mathord{}}
            {e+f+g}
            {\mathord{}+h+i}^{x}_{y}
        = e^2
    $$
\end{document}

enter image description here

4
  • Care to give a small MWE for the misaligned text?
    – percusse
    Jan 29, 2013 at 0:22
  • Here you go. It's based on the original example. Note the \Sigma subscript sticking out, which is the cause of the misalignment.
    – mhelvens
    Jan 29, 2013 at 0:34
  • holy cow that is some sophisticated TeX
    – thang
    Jan 29, 2013 at 0:38
  • It's not so bad if you understand the basics. (It never is.)
    – mhelvens
    Jan 29, 2013 at 0:40

2 Answers 2

4

You don't want to centre it: just take off the underbrace so the baseline of the construct is the baseline of the base, then rebuild it:

enter image description here

\documentclass{article}

\makeatletter
\def\overunderbrace#1#2#3{%
    \begingroup
    \let\overunderbrace@sup\empty
    \let\overunderbrace@sub\empty
    \@ifnextchar^%
        {\@overunderbracesup{#1}{#2}{#3}}%
        {\@ifnextchar_%
            {\@overunderbracesub{#1}{#2}{#3}}%
            {\@overunderbrace{#1}{#2}{#3}}%
        }%
}

\def\@overunderbracesup#1#2#3^#4{%
    \def\overunderbrace@sup{#4}%
    \@ifnextchar_%
        {\@overunderbracesub{#1}{#2}{#3}}%
        {\@overunderbrace{#1}{#2}{#3}}%
}

\def\@overunderbracesub#1#2#3_#4{%
    \def\overunderbrace@sub{#4}%
    \@ifnextchar^%
        {\@overunderbracesup{#1}{#2}{#3}}%
        {\@overunderbrace{#1}{#2}{#3}}%
}

\def\@overunderbrace#1#2#3{%
\mathop {\setbox\z@\vbox{\m@th \ialign {##&##&##\crcr
    \noalign {\kern 3\p@}%
    \span\omit\hfil\hbox{\vbox to 0pt{\vss\hbox{\vbox{\hbox{$\m@th\scriptstyle\overunderbrace@sup$}\vspace{0pt}}}}}\hfil
    &%
    \crcr \noalign {\kern 5\p@\nointerlineskip}%
    \span\omit\downbracefill&%
    \crcr \noalign {\kern 3\p@\nointerlineskip}%
        $\hfil \displaystyle {#1}\hfil $&%
        $\hfil \displaystyle {#2}\hfil $&%
        $\hfil \displaystyle {#3}\hfil $%
    \crcr \noalign {\kern 3\p@\nointerlineskip}%
      & \span\omit  \upbracefill
    \crcr \noalign {\kern 5\p@\nointerlineskip}%
    &\span\omit\hfil\hbox{\vbox to 0pt{\hbox{\vbox{\vspace{0pt}\hbox{$\m@th\scriptstyle\overunderbrace@sub$}}}\vss}}\hfil
    \crcr %\noalign {\kern 3\p@ }%
}%
\global\setbox\@ne\lastbox
\unkern
\global\setbox\thr@@\lastbox
\unkern
}%
\vtop{\box\z@\kern3\p@\nointerlineskip\box\thr@@\kern5\p@\nointerlineskip\box\@ne\kern3\p@}%
}%
    \endgroup
}
\makeatother


\begin{document}
    $$
        a+b+
        \overunderbrace
            {c+\mathord{\mathop{\Sigma}_d} + \mathord{}}
            {e+f+g}
            {\mathord{}+h+i}^{x}_{y}
        = e^2
    $$
\end{document}
1
  • Very cool. :-) Thanks! I knew about \vtop, but I didn't know about 'unboxing' yet.
    – mhelvens
    Jan 29, 2013 at 10:56
6

Here's my take, based on Herbert's answer to the question you cited. For simplicity I defined an \overunderbrace with five arguments (the last two being the things to be put over / under the braces), but it could also be done as in your sample code.

output

\documentclass{article}
\usepackage{mathtools}
\newcommand*\overunderbrace[5]{%
  \mathrlap{\overbrace{\phantom{#1#2}}^{\mathclap{#4}}}%
  #1\mathord{\underbrace{#2#3}_{\mathclap{#5}}}%
  }
\begin{document}
\[
  a+b+\overunderbrace{c+\mathord{\mathop{\Sigma}_d}+}{e+f+g}{+h+i}{x}{y}+k+l=e^2
\]
\end{document}
4
  • Hm... That's quite elegant. Short code, and the underbrace isn't hindered by the \Sigma_d. Don't know how I missed Herbert's answer on my original pass through that question.
    – mhelvens
    Jan 29, 2013 at 10:50
  • This solution is probably better for the \underoverbrace problem, but I gotta give the green tick to David, who more directly answered my question.
    – mhelvens
    Jan 29, 2013 at 10:58
  • @mhelvens: The choice which answer to give to green tick is completely up to you :-) Jan 29, 2013 at 13:33
  • I know. :-p I just wanted to reiterate that I prefer your solution, at least for the specific problem I used as an example.
    – mhelvens
    Jan 29, 2013 at 13:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .