2

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: align

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: edited

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:

enter image description here

lhk
  • 185
  • 1
    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). – Bernard Aug 10 '20 at 11:40

4 Answers4

4

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}

enter image description here

Bernard
  • 271,350
  • thanks! this looks very good. I'll try to make it work and then accept it :) – lhk Aug 10 '20 at 12:44
  • Was I correct in supposing you're in two-column mode? – Bernard Aug 10 '20 at 12:46
  • you mean the document as a whole, right? Yes that's in two-column mode – lhk Aug 10 '20 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 '20 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 '20 at 13:10
  • For the use of \phantom, I wanted to align the integral sign with the $2$ in the row below. I inserted a thin unbreakable space in front of the differential operator (which should be in roman, by the way). The aim of multline(d) is not to align, but to use all the available space: the first line starts at the left of the column, and the second line ends at the right. That's a different layout from align(ed), used for instance when you have no real reason to align whatever. – Bernard Aug 10 '20 at 13:18
  • B.t.w. for the differential symbol, I often use a macro found on this site, to have a correct spacing: \newcommand*{\dd}{\mathop{}\!\mathrm{d}}. – Bernard Aug 10 '20 at 13:21
  • thanks for the explanations. great answer, it helps me a lot! – lhk Aug 10 '20 at 14:00
3

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}

align vs alignat

phil-elkabat
  • 2,055
3

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}

enter image description here

Zarko
  • 296,517
0

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: enter image description here 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).

lhk
  • 185