How can I align equations to the left and to the right?

This is my first topic here in StackExchange. After a lot of research, I couldn't figure out how to align equations to the left and to the right using any environment. For example, I'd like to align this set of three equations:

where all equalities are to be aligned, and also the first two equations, but I want to align them to the right. I mean, I'd like to "shift" the blank space created on the right (after the derivative) to the left of the second line's expression (before the minus sign). I certainly could align them to the right using split environment, but it doesn't allow me to align both the equations and the equal signs.

My code using alignat is written as follows:

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}
\begin{document}

\begin{alignat}{3}
(\hat{n}\cdot \nabla \times \hat{n})
&= - \cos^2{(\theta(z))} \cdot \frac{d \theta}{dz} -
\sin^2{(\theta(z))}         \cdot \frac{d \theta}{dz} &&= \\
&= - \underbrace{\left[\cos^2{(\theta(z))} + \sin^2{(\theta(z))}
\right]}_{=1} \cdot \frac{d \theta}{dz} &&= \\
&= -\frac{d \theta}{dz}
\end{alignat}

\end{document}


While as using split:

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}
\begin{document}

$$\begin{split} (\hat{n}\cdot \nabla \times \hat{n}) = - \cos^2{(\theta(z))} \cdot \frac{d \theta}{dz} - \sin^2{(\theta(z))} \cdot \frac{d \theta}{dz}& = \\ = - \underbrace{\left[\cos^2{(\theta(z))} + \sin^2{(\theta(z))} \right]}_{=1} \cdot \frac{d \theta}{dz}& = \\ = -\frac{d \theta}{dz} \end{split}$$

\end{document}


Where the two first derivatives are aligned to the right, but not the equal signs.

My goal is to do something like this:

• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. May 9, 2016 at 3:08

It was unclear whether the OP wanted a single or 3 equation numbers, based on the two examples provided. If only a single equation number is needed, then a tabstackengine solution is as follows.

Key was using a \mathrclap on the 3rd row, to overcome the specified right alignment of the 2nd column.

\documentclass{article}
\usepackage{tabstackengine}
\stackMath
\setstacktabulargap{0pt}
\setstackgap{L}{2.5\baselineskip}
\makeatletter
\renewcommand\TAB@delim[1]{\displaystyle#1}
\makeatother
\usepackage{mathtools}
\begin{document}
$$\tabularCenterstack{rr}{ (\hat{n}\cdot \nabla \times \hat{n}) =& - \cos^2{(\theta(z))} \cdot \frac{d \theta}{dz} - \sin^2{(\theta(z))} \cdot \frac{d \theta}{dz} = \\ =& - \protect\underbrace{\left[\cos^2{(\theta(z))} + \sin^2{(\theta(z))} \right]}_{=1} \cdot \frac{d \theta}{dz} = \\ =\mathrlap{ -\frac{d \theta}{dz}}& }$$
\end{document}


Because all columns are right-aligned, the \tabularCenterstack{rr}{...} could be replaced with a \tabbedCenterstack[r]{...}.