# Aligning under and overbraces

I have a "simple" representation given by the following MWE

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation*}
\underbrace{\ldots%
\overbrace{\text{ turning }}^{p(w_{t-2}|w_t)}%
\overbrace{\text{   into  }}^{p(w_{t-1}|w_t)}}_{\text{output context words - m-word window}}%
\underbrace{\text{ banking }}_{\text{position }t}%
\underbrace{\overbrace{\text{ crises }}^{p(w_{t+1}|w_t)}%
\overbrace{\text{   as   }}^{p(w_{t+2}|w_t)}%
\ldots}_{\text{output context words - m-word window}}
\end{equation*}
\end{document}


The third \underbrace seems not aligned with other part of content and I can't see a reason for that.

Also is there any way add a new line in the text output context words - m-word window? inserting a \n where the - lies?

The third underbrace is typeset in a higher position because the text over it has no letters with a descender (like "g" in the first two). I insert a \vphantom{gh} with an h in order to align also the \overbraces. For a two-line description under the underbrace you can use the \substack macro (also from amsmath):

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation*}
\underbrace{\ldots%
\overbrace{\text{ turning }}^{p(w_{t-2}|w_t)}%
\overbrace{\text{   into  }}^{p(w_{t-1}|w_t)}}_{\substack{\text{output context words}\\\text{m-word window}}}%
\underbrace{\text{ banking }}_{\text{position }t}%
\underbrace{\overbrace{\text{ crises }}^{p(w_{t+1}|w_t)}%
\overbrace{\text{   as\vphantom{gh}   }}^{p(w_{t+2}|w_t)}%
\ldots}_{\substack{\text{output context words}\\\text{m-word window}}}
\end{equation*}
\end{document}


• Adding to the all users my sincere congratulations. – Sebastiano Dec 16 '19 at 12:49

“turning into” and “banking” both have a descender, the « g », i.e. a character the has some part below the baseline. However, “crises as” doesn't; this explains the behavior you saw.

You can use the \strut command in text to impose a minimum height and depth at the point where you use it. It is an invisible box with zero width such that, when two struts are put on top of each other, their reference points are exactly one standard baseline apart.1 Here is a suggestion to fix the problem in your example (the first two struts are not necessary here, but a macro could systematically put them: they can only help, not hurt in this context).

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation*}
\underbrace{\ldots%
\overbrace{\text{ \strut turning }}^{p(w_{t-2}|w_t)}%
\overbrace{\text{   into  }}^{p(w_{t-1}|w_t)}}_{\text{output context words - m-word window}}%
\underbrace{\text{ \strut banking }}_{\text{position }t}%
\underbrace{\overbrace{\text{ \strut crises }}^{p(w_{t+1}|w_t)}%
\overbrace{\text{   as   }}^{p(w_{t+2}|w_t)}%
\ldots}_{\text{output context words - m-word window}}
\end{equation*}
\end{document}


If you also add struts to “into” and “as”, this will additionally put the top braces at the same height as the other top braces:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation*}
\underbrace{\ldots%
\overbrace{\text{ \strut turning }}^{p(w_{t-2}|w_t)}%
\overbrace{\text{ \strut into  }}^{p(w_{t-1}|w_t)}}_{\text{output context words - m-word window}}%
\underbrace{\text{ \strut banking }}_{\text{position }t}%
\underbrace{\overbrace{\text{ \strut crises }}^{p(w_{t+1}|w_t)}%
\overbrace{\text{ \strut as   }}^{p(w_{t+2}|w_t)}%
\ldots}_{\text{output context words - m-word window}}
\end{equation*}
\end{document}


Footnote

1. \strutbox is defined in LaTeX as an \hbox containing a \vrule (TeX vertical rule) with zero width, 0.7\baselineskip height and 0.3\baselineskip depth. In math mode, the \strut macro simply copies the \strutbox to the math list under construction. In other modes, it uses \unhcopy to (1) switch to horizontal mode if not already in this mode, and (2) copy the contents of the \hbox (i.e., the \vrule) to the current horizontal list.