[Tex/LaTex] How to fix the math-spacing when a lot of \phantom, \mathrlap, etc. are used

horizontal alignmentmath-modespacing

This question led to a new package:
underoverlap

Introduction

Based on this question and then this question, I'm trying to perfect the \overunderbrace macro. Right now, I'm trying to get the spacing right.

Minimal Working Example

\documentclass[varwidth=20cm,margin=3mm]{standalone}

\usepackage{mathtools}
\usepackage{xcolor}

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

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

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

\def\@overunderbrace#1#2#3{%
    \mathrlap{\overbrace{\textcolor{red}{#1#2}}^{\mathclap{\overunderbrace@sup}}}%
    #1%
    \mathrlap{\underbrace{\textcolor{green}{#2#3}}_{\mathclap{\overunderbrace@sub}}}%
    #2%
    #3%
    \endgroup%
}
\makeatother

\newcommand{\config}[2]{\left\langle\,#1, #2\,\right\rangle}

\begin{document}
    $$
        \config{F\vphantom{F'}}{p}
        \longrightarrow
        \config{F'}{p}
        \longrightarrow
        \config{F'}{p'}
        \longrightarrow
        \cdots
    $$
    $$
        \overbrace
            {\config{F\vphantom{F'}}{p}\longrightarrow\config{F'}{p}}^{test}
        \longrightarrow
        \config{F'}{p'}
        \longrightarrow
        \cdots
    $$
    $$
        \overunderbrace
            {\config{F\vphantom{F'}}{p}\longrightarrow}
            {\config{F'}{p}}
            {\longrightarrow\config{F'}{p'}}
            ^{\text{environmental}}
            _{\text{local}}
        \longrightarrow
        \cdots
    $$
\end{document}

enter image description here

As you can see, the braces are horizontally misaligned to the content.

Note

The \@overunderbrace code here may seem (unnecessarily) more complicated than the code from the answer I based it on, but that's because I'm trying to set up a repeating pattern, so we can get more than just two overlapping braces. The way to do that seems to be to:

  • set [brace 1] with [phantom+rlap arguments 1+2]
  • set [argument 1]
  • set [brace 2 with [phantom+rlap arguments 2+3]
  • set [argument 2]
  • etc…

Diagnosis

To diagnose the problem, I replaced \phantom with \textcolor in the following code:

\def\@overunderbrace#1#2#3{%
    \mathrlap{\overbrace{\textcolor{red}{#1#2}}^{\mathclap{\overunderbrace@sup}}}%
    #1%
    \mathrlap{\underbrace{\textcolor{green}{#2#3}}_{\mathclap{\overunderbrace@sub}}}%
    #2%
    #3%
    \endgroup%
}

enter image description here

The black output is identical to the previous image. The red (green) content is the placement that the over(under)brace is based on.

It occurred to me that TeX determines spacing between math 'atoms' based on their respective types (ord, bin, rel, etc.). And I'm messing with this process by reordering the segments, \phantoming and \mathrlaping them.

Attempted Solution

So I augmented the formal parameters, making sure they always encounter the same 'environment' (to the best of my ability):

\def\@overunderbrace#1#2#3{%
    \@@overunderbrace%
        {\mathord{}#1\vphantom{#2}}%
        {\vphantom{\mathord{}#1}#2\vphantom{#3\mathord{}}}%
        {\vphantom{#2}#3\mathord{}}%
}

\def\@@overunderbrace#1#2#3{%
    ... the old \@overunderbrace ...
}

enter image description here

For comparison, I added the same formula without any braces, and then with only the overbrace (from the TeX command). As you see, no color is visible anymore (except for the rulers I added manually), so the printed content is aligned with its phantom images.

This solution is usable, but as you can see by the comparison, the spacing is still not right. Possibly because it's now using some 'inter-atom' glue twice in a row.

This is the point where I give up and ask for help.

Question: How can I get perfect spacing in a setup like this?

Best Answer

Nice colour debugging (+1 for that:-)

You can measure the extra space that would have been between #1 and #2 normally and re-insert it:

enter image description here

\def\@overunderbrace#1#2#3{%
    \setbox\z@\hbox{${#1#2}$}\setbox\tw@\hbox{${#1}{#2}$}\dimen@\dimexpr\wd\z@-\wd\tw@
    \mathrlap{\overbrace{\textcolor{red}{{#1#2}}}^{\mathclap{\overunderbrace@sup}}}%
    {#1}%
    \kern\dimen@
    \mathrlap{\underbrace{\textcolor{green}{{#2#3}}}_{\mathclap{\overunderbrace@sub}}}%
    {#2%
     #3}%
    \endgroup%
}
Related Question