如何使方程式左右对齐?

如何使方程式左右对齐?

这是我在 StackExchange 上的第一个主题。经过大量研究,我还是搞不清楚如何使用任何环境将方程式向左和向右对齐。例如,我想对齐这组三个方程式:

使用 alignat 的示例

其中所有等式都要对齐,前两个等式也是如此,但我想将它们向右对齐。我的意思是,我想将右侧(导数之后)创建的空白“移位”到第二行表达式的左侧(减号之前)。我当然可以使用拆分环境将它们向右对齐,但它不允许我同时对齐等式和等号。

我使用alignat的代码写如下:

\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}

使用 split 时:

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

\begin{equation}
\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{equation}

\end{document}

其中两个一阶导数右对齐,但不对齐等号。

我的目标是做这样的事情:

目标

希望我已经表达清楚了。提前谢谢。

答案1

根据提供的两个示例,不清楚 OP 是想要单个方程数还是 3 个方程数。如果只需要单个方程数,则tabstackengine解决方案如下。

关键是使用\mathrclap第三行的 来克服第二列指定的右对齐。

\documentclass{article}
\usepackage{tabstackengine}
\stackMath
\setstacktabulargap{0pt}
\setstackgap{L}{2.5\baselineskip}
\makeatletter
\renewcommand\TAB@delim[1]{\displaystyle#1}
\makeatother
\usepackage{mathtools}
\begin{document}
\begin{equation}
\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{equation}
\end{document}

在此处输入图片描述

因为所有列都右对齐,所以\tabularCenterstack{rr}{...}可以用 替换\tabbedCenterstack[r]{...}

相关内容