# Math mode inside underbrace text

Problem:

Trying to activate math mode inside underbrace text but I receive errors telling me that I am not allowed. I have managed to solve this with $ and $ but the equation shifts to the center of the page despite using \noindent.

Minimal Working Example (MWE):

\documentclass[a4paper,11pt]{book}
\usepackage{amsmath}
\usepackage{amssymb}

\begin{document}

\noindent $(a \pm b)^2 = (a \pm b)(a \pm b) = a \cdot a \pm a \cdot b \pm \underbrace{b \cdot a}_\text{a \cdot b} + b \cdot b = a^2 \pm 2ab + b^2$

\end{document}


Desired output:

Get math mode to work inside underbrace text while keeping the overall text aligned to the left.

• Although the accepted answer is a simpler solution,  is display mode, and you can instead use  for inline. – Davislor Jul 5 '19 at 20:24

In theory you could use $...$ to have math mode inside \text{...}

\documentclass[a4paper,11pt]{book}
\usepackage{amsmath}
\usepackage{amssymb}

\begin{document}

\noindent $(a \pm b)^2 = (a \pm b)(a \pm b) = a \cdot a \pm a \cdot b \pm \underbrace{b \cdot a}_\text{$a \cdot b$} + b \cdot b = a^2 \pm 2ab + b^2$

\end{document}


But why do you use \text{...} for math? Just remove it:

\documentclass[a4paper,11pt]{book}
\usepackage{amsmath}
\usepackage{amssymb}

\begin{document}

\noindent $(a \pm b)^2 = (a \pm b)(a \pm b) = a \cdot a \pm a \cdot b \pm \underbrace{b \cdot a}_{a \cdot b} + b \cdot b = a^2 \pm 2ab + b^2$

\end{document}

• This is embarrassing - I certainly do not know why I had it in my mind that I had to use \text to get underbrace to work. – kexxcream Jul 5 '19 at 17:04