# Align multiple multline equation (or how to flushright steps in aligned math)

Most of readers will be tempted to flag this question as a duplicate, because I've already seen lot of debate about this (but none of them solved this issue). I'll first post my MWE for I have some mathematical definitions, and you can follow my steps by just compiling it on your pc.

\documentclass{article}

\usepackage[showframe]{geometry} %just to show the layout of the page
\usepackage{amsmath} % align, aligned, multline environments
\usepackage{mathtools}
\usepackage{amssymb}
\usepackage{verbatim}
\usepackage{xcolor}
%my custom commands for diff, deriv and pderiv
\makeatletter
\providecommand*{\diff}%
{\@ifnextchar^{\DIfF}{\DIfF^{}}}
\def\DIfF^#1{%
\mathop{\mathrm{\mathstrut d}}%
\nolimits^{#1}\gobblespace}
\def\gobblespace{%
\futurelet\diffarg\opspace}
\def\opspace{%
\let\DiffSpace\!%
\ifx\diffarg(%
\let\DiffSpace\relax
\else
\ifx\diffarg[%
\let\DiffSpace\relax
\else
\ifx\diffarg\{%
\let\DiffSpace\relax
\fi\fi\fi\DiffSpace}
\makeatother
\providecommand*{\deriv}[3][]{%
\frac{\diff^{#1}#2}{\diff #3^{#1}}
}
\providecommand*{\pderiv}[3][]{%
\frac{\partial^{#1}#2}{\partial #3^{#1}}
}

\begin{document}
This is the result with the \begin{verbatim} aligned \end{verbatim}
environment
\begin{aligned} \bar{M}_{ab}^{(x)} &= \int_{\mathbb{R}^2}\diff{y}\diff{z}\hat\varepsilon_x e^{-{i}\left(k_yy+k_zz\right)}\left\{\psi_a^{\star}(\vec{r})\left[e^{-{i}(k_xx)}\hat\varepsilon_x\psi_b(\vec{r})\right]_{-\infty}^{\infty} +\right.\\ &\phantom{\mathrel{=}}- \left.\int_{\mathbb{R}}\diff{x}e^{-{i}(k_xx)}\hat\varepsilon_x\psi_b(\vec{r})\pderiv{\left(\psi^{\star}_a(\vec{r})\right)}x +\psi^{\star}_a(\vec{r})e^{-{i}(k_xx)}\hat\varepsilon_x\psi_b(\vec{r})\right\}\, \end{aligned}

\textcolor{blue}{This is the result with} \begin{verbatim} multline \end{verbatim}
environment
\begin{multline}
\bar{M}_{ab}^{(x)} = \int_{\mathbb{R}^2}\diff{y}\diff{z}\hat\varepsilon_x e^{-{i}\left(k_yy+k_zz\right)}\left\{\psi_a^{\star}(\vec{r})\left[e^{-{i}(k_xx)}\hat\varepsilon_x\psi_b(\vec{r})\right]_{-\infty}^{\infty} +\right.\\
- \left.\int_{\mathbb{R}}\diff{x}e^{-{i}(k_xx)}\hat\varepsilon_x\psi_b(\vec{r})\pderiv{\left(\psi^{\star}_a(\vec{r})\right)}x +\psi^{\star}_a(\vec{r})e^{-{i}(k_xx)}\hat\varepsilon_x\psi_b(\vec{r})\right\}\,
\end{multline}
\textcolor{blue}{the only one that does what I want, but does not work in align(ed) environment.}\\[10pt]
\par This is the result with \begin{verbatim} aligned + multlined + equation \end{verbatim}
environment
\begin{aligned} \bar{M}_{ab}^{(x)} &\!\begin{multlined}[t] = \int_{\mathbb{R}^2}\diff{y}\diff{z}\hat\varepsilon_x e^{-{i}\left(k_yy+k_zz\right)}\left\{\psi_a^{\star}(\vec{r})\left[e^{-{i}(k_xx)}\hat\varepsilon_x\psi_b(\vec{r})\right]_{-\infty}^{\infty} +\right.\\ - \left.\int_{\mathbb{R}}\diff{x}e^{-{i}(k_xx)}\hat\varepsilon_x\psi_b(\vec{r})\pderiv{\left(\psi^{\star}_a(\vec{r})\right)}x +\psi^{\star}_a(\vec{r})e^{-{i}(k_xx)}\hat\varepsilon_x\psi_b(\vec{r})\right\}\, \end{multlined}\\ &=\text{other stuff I would like to add, but now the step above is not flushed to the right} \end{aligned}

\end{document}


What I would like is to highlight the steps in a mathematical chain of relations by flushing them to the left when the break is inside the same equality, and by alining at the same point if they are another step of the chain equation. So let's see an example. The environment that work exactly as I wan is the multline or multlined, in fact you can see that the following code

\begin{multline}
\bar{M}_{ab}^{(x)} = \int_{\mathbb{R}^2}\diff{y}\diff{z}\hat\varepsilon_x e^{-{i}\left(k_yy+k_zz\right)}\left\{\psi_a^{\star}(\vec{r})\left[e^{-{i}(k_xx)}\hat\varepsilon_x\psi_b(\vec{r})\right]_{-\infty}^{\infty} +\right.\\
- \left.\int_{\mathbb{R}}\diff{x}e^{-{i}(k_xx)}\hat\varepsilon_x\psi_b(\vec{r})\pderiv{\left(\psi^{\star}_a(\vec{r})\right)}x +\psi^{\star}_a(\vec{r})e^{-{i}(k_xx)}\hat\varepsilon_x\psi_b(\vec{r})\right\}\,
\end{multline}


produces the following output

And the problems come here, ad I woul like to continue the equality chain by adding another math line with a symbol = but which is aligned to the first =. So I tried the following solutions, which can be obtained by compiling my MWE

So how to achieve a result similar to the last one, but with the middle step flushed to the right in the most simple way? Why does the last solution do not work? I really would like not to implement strange self defined commands, but, in order to have a more readable code, I would prefer to solve the problem using these environments. Thanks for your time!

# EDIT:

The original solution proposed by @bernand is nice, but not really what I wanted, as I'm looking for something more automatic, that does not depend on whether you place it, but that fills the horizontal space, so that if I change the borders of my page it will be stretched properly, as this solution does not work for example in a document with different layout

Here's a solution, with multlined conveniently placed. Note that, using mathtools, you don't have to load amsmath. I added some spacing after the differential symbols.

\documentclass{article}

\usepackage[showframe]{geometry} %just to show the layout of the page
\usepackage{mathtools}% align, aligned, multline environments; extends amsmath
\usepackage{amssymb}
\usepackage{verbatim}
\usepackage{xcolor}
%my custom commands for diff, deriv and pderiv
\makeatletter
\providecommand*{\diff}%
{\@ifnextchar^{\DIfF}{\DIfF^{}}}
\def\DIfF^#1{%
\mathop{\mathrm{\mathstrut d}}%
\nolimits^{#1}\gobblespace}
\def\gobblespace{%
\futurelet\diffarg\opspace}
\def\opspace{%
\let\DiffSpace\!%
\ifx\diffarg(%
\let\DiffSpace\relax
\else
\ifx\diffarg[%
\let\DiffSpace\relax
\else
\ifx\diffarg\{%
\let\DiffSpace\relax
\fi\fi\fi\DiffSpace}
\makeatother
\providecommand*{\deriv}[3][]{%
\frac{\diff^{#1}#2}{\diff #3^{#1}}
}
\providecommand*{\pderiv}[3][]{%
\frac{\partial^{#1}#2}{\partial #3^{#1}}
}

\begin{document}

\begin{aligned} \bar{M}_{ab}^{(x)} & =\int_{\mathbb{R}^2}\diff{y}\diff{z}\,\hat\varepsilon_x e^{-{i}\left(k_yy+k_zz\right)} \begin{multlined}[t]\biggl\{\psi_a^{\star}(\vec{r})\Bigl[e^{-{i}(k_xx)}\hat\varepsilon_x\psi_b(\vec{r})\Bigr]_{-\infty}^{\infty} + \\ \mathllap{-}\int_{\mathbb{R}}\diff{x}\,e^{-{i}(k_xx)}\hat\varepsilon_x\psi_b(\vec{r})\pderiv{\left(\psi^{\star}_a(\vec{r})\right)}x +\psi^{\star}_a(\vec{r})e^{-{i}(k_xx)}\hat\varepsilon_x\psi_b(\vec{r})\biggr\} \end{multlined}\\ &=\text{other stuff I would like to add, but now the step above is not flushed to the right} \end{aligned}

\end{document}


• Sorry I just noticed that the answer is not really what I wanted. You forced the multilined environment manually to start at a certain point, but I would like something that does not depend on whether you place it, but that hfill the line automatically. Thanks in advance. – GiuTeX Apr 10 '18 at 15:16