# Aligning underbrace for long and short terms

I have an equation in which I want to use underbrace , but the braces appear in different heights since one term is longer than the other. Here is an example:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{physics}
\begin{document}
\begin{align}
\underbrace{-\frac{1}{M}\vec{p}{\partial\vec{p}}\frac{\partial\mathcal{P}}{\partial\vec{x}}}_{\frac{\partial\mathcal{H}} {\partial\vec{p}}}+\underbrace{\mathcal{U}'}_{\frac{\partial\mathcal{H}}{\partial\vec{x}}}\mathcal{P}
\end{align}
\end{document}


I tried using vphantom, but then my equation got distorted noticeably. How can I fix this ? Thank you in advance.

• Use \vphantom{\frac{\partial\mathcal{P}}{\partial\vec x}} inside the second underbrace. – Sigur Feb 22 '19 at 18:29

I see no problem if I add a \vphantom for the bigger object in the first term.

I added a few items:

1. braces around the \underbrace constructs, to get good spacing when operators are involved;

2. a couple of thin spaces after \vec{...} constructs in order to avoid clashes;

3. removed the call to physics, which you're not using and, in my opinion, should not be used because of several weaknesses.

\documentclass[12pt]{article}
\usepackage{amsmath}

\begin{document}

\begin{align}
{\underbrace{
-\frac{1}{M}\vec{p}\,\partial\vec{p}\,\frac{\partial\mathcal{P}}{\partial\vec{x}}
}_{\frac{\partial\mathcal{H}}{\partial\vec{p}}}}
+
{\underbrace{
\vphantom{\frac{\partial\mathcal{P}}{\partial\vec{x}}}
\mathcal{U}'
}_{\frac{\partial\mathcal{H}}{\partial\vec{x}}}}
\end{align}

\end{document}


• In the second term, you mean? – Sigur Feb 22 '19 at 18:35
• I tried to keep the example as simple as possible, but I have more terms following the second term. Adding those, there appears a large gap between the terms. – user175924 Feb 22 '19 at 18:40

As you can see,

\vphantom{\frac{\partial\mathcal{P}}{\partial\vec x}}


should work.

MWE

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{physics}
\begin{document}
\begin{align}
\underbrace{-\frac{1}{M}\vec{p}{\partial\vec{p}}\frac{\partial\mathcal{P}}{\partial\vec{x}}}_{\frac{\partial\mathcal{H}} {\partial\vec{p}}}+\underbrace{\vphantom{\frac{\partial\mathcal{P}}{\partial\vec x}}\mathcal{U}'}_{\frac{\partial\mathcal{H}}{\partial\vec{x}}}
\end{align}
\end{document}


• I tried to keep the example as short as possible, but actually the second term is multiplied by another term. Due to the brace, there appears a gap between the U' and the term it's multiplied. I guess I need to keep the second brace shorter, or find find a replacement for underbrace. – user175924 Feb 22 '19 at 18:48
• @user175924, so edit your post and insert the full code. – Sigur Feb 22 '19 at 18:50