# How to fix the math-spacing when a lot of \phantom, \mathrlap, etc. are used?

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}


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%
}


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 ...
}


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?

-
Thanks! I'm going to read that. That's not what's causing the problem here, however. – mhelvens Jan 29 '13 at 18:36
If you use \newcommand{\config}[2]{{}\left\langle\,#1, #2\,\right\rangle{}}, it works. Somewhere on the way, your math-lapping gets in the way of TeX’s math-spacing algorithm. Meaning: The left and right angles do not see the appropriate math classes and assume something else (or nothing?). – Qrrbrbirlbel Jan 29 '13 at 18:54
– Qrrbrbirlbel Jan 29 '13 at 18:59
@Qrrbrbirlbel Nice observation on the {}. Building on that, I've actually made the following sensible change: \newcommand{\config}[2]{\mathord{\left\langle\,#1, #2\,\right\rangle}}, and it works. This doesn't fix the general problem, of course, but it is another diagnostic result. – mhelvens Jan 29 '13 at 19:04

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:

\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%
}

-
Looking good! But I'll have to put off testing it until tomorrow evening. (I can only look forward to 4.5 hours sleep as it is.) --- One question in the mean time, though: I was under the impression that \z@ was a 'dimen' register (i.e., not a 'box' register); moreover, one that must not be changed under any circumstances, or so the TeXbook tells me. So.. what's up with that? – mhelvens Jan 29 '13 at 23:40
It's not being changed it is just being used as an efficient 0 see tex.stackexchange.com/questions/55400/what-does-z-do/… – David Carlisle Jan 29 '13 at 23:42
Ah, yes of course. \setbox takes an index. But \z@... one wonders if such an 'optimization' makes a significant difference in this day and age. More to the point: is it worth obscuring the meaning of the code? – mhelvens Jan 30 '13 at 5:16
It was out of habit rather than optimisation, it also makes it much less likely to get expansion errors as if you use a literal 0 tex has to scan ahead to see if more digits follow. if you look at much latex (or amsmath) code you will see literally hundreds of uses if \z@ so I wasn't expecting it to be obscure to anyone who was happy to see code with setbox at all. – David Carlisle Jan 30 '13 at 9:32
I must be at just that point in the TeXbook where I know about \setbox but not about \z@. ;-) – mhelvens Jan 30 '13 at 20:17