# switch between left and right alignment in multiline equation

I would like to typeset an equation, with multiple equalities and long terms.
Each equals sign should be left-aligned on a new line. But leftover terms that flow to a new line should be right-aligned.
Here's a minimal example, I've used alignment symbols to position the parts of the equation:

\begin{align*}
&p_{very\;long\;function}(x) =&& \\
&&\int f_{very \; long \; integral \; terms}(x)&\\
&&\mathcal{N}(x; 0, \sigma^2) dx&\\
&= 2&&
\end{align*}


The result looks like this:

The alignment pushes the equation apart. It does left- and right- align it, but the individual columns can't overlap. So it's not the same as just flowing the lines left and right.
What I would like to get is this:

In the image above, the equation is as wide as the widest line. And individual lines are left or right-aligned.

I've searched for related questions, on how to configure alignment. There are many similar questions, but none of them seem to answer my problem:

UPDATE:

To clarify my question: The document is in two-column mode. That's why I need a compact way to typeset my equations.
Here is an example equation with 4 lines. The first and last should be left-bound. The second one determines the width of the equation, the third one should be right-bound.
I think the key problem is that align and alignat columns may not overlap.
The code is this:

\begin{align*}
&p_{long \; function}(x) =\\
& \int f_{many\; very \; long \; integral \; terms}(x)\\
&\mathcal{N}(x; 0, \sigma^2) dx\\
&= 2
\end{align*}


It should look like this:

• I don't understand what you'd like to obtain. Could you be more precise or/and post a sketch? Are you in two-column mode? And the = sign in the first line cannot possibley be left-aligned with the others (unless you put it at the beginning of the second line). Aug 10, 2020 at 11:40

I supposed you're in two-column mode, and, if I understand well your requirements, they can be satisfied with an aligned environment nested in the outer align* (with a single alignment column). However, I also propose another solution, based on multlined:

\documentclass[twocolumn]{article}
\usepackage{mathtools}

\begin{document}

\begin{align*}
& p_\text{very\;long\;function}(x) = \\
& \phantom{{}={}} \begin{aligned}[t]\int f_\text{very long integral terms}(x)\\
\mathcal{N}(x; 0, \sigma^2)\, dx
\end{aligned}\\
& = 2
\end{align*}

\begin{align*}
& p_\text{very\;long\;function}(x) \\
& = \begin{multlined}[t]\int f_\text{very long integral terms}(x)\\
\mathcal{N}(x; 0, \sigma^2)\, dx
\end{multlined}\\
& = 2
\end{align*}

\end{document}


• thanks! this looks very good. I'll try to make it work and then accept it :)
– lhk
Aug 10, 2020 at 12:44
• Was I correct in supposing you're in two-column mode? Aug 10, 2020 at 12:46
• you mean the document as a whole, right? Yes that's in two-column mode
– lhk
Aug 10, 2020 at 12:51
• hm, some things are not clear to me. The use of phantom (I tried just the equals and it doesn't work). The \, in front of the dx. And why the end of the second and third line are not aligned in the multiline version (they end at different lengths). I thought the \, would introduce a new column in the multiline, but it doesn't seem to do that.
– lhk
Aug 10, 2020 at 13:06
• ah sorry, the \, is simply for space. The phantom is clear. I still don't understand why the second version doesn't end the second and third line at the same position. The dx of line 3 stands out.
– lhk
Aug 10, 2020 at 13:10

You have to properly align your equation:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{align*}
p_{\text{very long function}}(x) &= &\\
\int f_{\text{very long integral terms}}(x)&\\
\mathcal{N}(x; 0, \sigma^2) dx &\\
&= 2 &
\end{align*}

\begin{alignat*}{3}
&p_{\text{very long function}}(x) =\\
&\int f_{\text{very long integral terms}}(x)&\\
&\mathcal{N}(x; 0, \sigma^2) dx&&\\
&= 2
\end{alignat*}

\end{document}


• For my humble opinion I will use \text{...} for the words. Aug 10, 2020 at 11:55
• @Sebastiano, excellent! Thank you. As a historian I'm always pleasantly surprised when I get math typesetting problems right even half the time. Aug 10, 2020 at 12:02

I would write your equation on the following way:

\documentclass{article}
\usepackage{newtxtext,newtxmath}
\usepackage{mathtools}

\begin{document}
\begin{align*}
p_{A}(x)  & = \int f_{B}(x) \mathcal{N}(x; 0, \sigma^2)\, dx  \\
& = 2
\shortintertext{where are}
A   & = \text{very long function} \\
B   & = \text{very long integral terms} \\
\end{align*}

\end{document}


In case you're working with IEEE styles, there also the IEEEeqnarray, which allows you to specify a different alignment for each column.
Here is an example:

\begin{IEEEeqnarray*}{llr}
p(x) &=& \int f_{many\; very \; long \; integral \; terms}(x)\\
&&\mathcal{N}(x; 0, \sigma^2) dx\\
&= 2&
\end{IEEEeqnarray*}


which renders: Please note that strictly speaking, this does not completely solve my question.
It's a convenient way to switch between left and right alignment, but individual columns still can't overlap. The rendered output only looks good, because I shortened the length of p(x).