# Fix additional space and aligning the whole equation

Please I need to aligning the whole equation and to erase additional space because of \underbrace. I used \mathclap, \makebox[] but it still looked very ugly. Thanks for helping!

$$\label{eq1} \begin{split} d^{connection}_{j} & = \sqrt{\smash[b]{{{\bigg(x_0 \pm \sqrt {\smash[b]{r^2-\underbrace{\strut y_0^2}_ {%} \textstyle \begin{array}{c} y_j^2 \end{array}} -y_j^2+2\underbrace{\strut y_0}_ {%} \textstyle \begin{array}{c} y_j \end{array}}y_j}} - \underbrace{\strut x_j}_ {%} \textstyle \begin{array}{c} x_j \pm d_j^{\text{while waiting 0}} \end{array}} \bigg)^2}}}} \end{split}$$

• Welcome to TeX.SE. What’s the purpose of the 1-row array environments? – Mico Oct 31 '17 at 6:53

You might want to think about writing your expression in a different way.

\documentclass{article}

\usepackage{mathtools}

\newcommand*\smashedunderbrace[2][]{\mathpalette\dosmashedunderbrace{{#2}{#1}}}
\newcommand*\dosmashedunderbrace[2]{\dosmashedunderbraceindeed{#1}#2}
\newcommand*\dosmashedunderbraceindeed[3]{%
\smash[b]{%
\ooalign{
$#1#2$\cr
\hidewidth$#1\underbrace{\phantom{#2}}_{#3}$\hidewidth\cr
}%
}%
}

\begin{document}

$$\label{eq1} d^{\text{connection}}_{j} = \sqrt{ \biggl( x_0 + \sqrt{r^2 - \smashedunderbrace[y_j^2]{y_0^2} - y_j^2 + 2 \smashedunderbrace[y_j]{y_0} y_j} - \smashedunderbrace[\qquad x_j \pm d_j^{\text{while waiting 0}}]{x_j} \;\biggr)^2 }$$

\end{document}


I suggest you get rid of the \sqrt terminology and, instead, use (...)^{1/2} notation. That way, none of the array wrappers and none of the \smash[b] directives are needed and thus can be omitted, greatly simplifying the code. Do use ^{} terms to ensure that various subscripts are all typeset at equal depths.

\documentclass{article}
\usepackage{mathtools} % for '\mathclap' directive; loads 'amsmath' automatically
\begin{document}
$$\label{eq1} d^{\,\text{connection}}_{j} = \bigl\{\bigl[x_0 \pm ( r^2-\underbrace{y_0^2}_{y_j^2} {}-y_j^2 +2\underbrace{y_0^{}}_{y_j^{\vphantom{2}}} y_j^{} )^{1/2} - \underbrace{x_j}_{\mathclap{x_j^{}\pm d_j^{\,\text{while waiting 0}}}} \,\bigr]^2 \bigr\}^{1/2}$$
\end{document}