4

The solutions below require up-to-date versions of LaTeX


When I tried to run the solutions below on my own machine I was not able to reproduce their results, but still got unwanted offsets. This was remedied by updating the latest distribution of LaTeX (2018). As pointed out in this comment, this is because amsmath has had its aligned environment updated.

Problem

I have several lines in a proof of mine, and I want to align several terms as shown:

Output

enter image description here

I can achieve the following using phantom where needed:

MWE

\documentclass{extarticle}

\usepackage{amsmath,bbm,bm,amssymb,amsthm,mathtools,cleveref,physics,letltxmacro}

% For usage in align environments. 
\newcommand\phantomrel[1]{\mathrel{\phantom{#1}}}
\newcommand\phantombin[1]{\mathbin{\phantom{#1}}}

\begin{document}
\begin{align*}
A
& = \text{several short lines}\\
& = \text{some very long line which takes up most of the width of the page} \\ 
& = \text{several short lines} \\
& = B + \mathbb{E}(\abs{a + b + c + d}) \qquad \text{(typically too long for a single line)} \\
& \leq B + \mathbb{E}(\abs{a})  \\ % Using the triangle inequality.
& \phantomrel{\leq} \phantom{B} + \mathbb{E}(\abs{b}) \\
& \phantomrel{\leq} \phantom{B} + \mathbb{E}(\abs{c}) \\
& \phantomrel{\leq} \phantom{B} + \mathbb{E}(\abs{d}) \\
\end{align*}
\end{document}

However, I am weary that this doesn't seem very scalable if I were to find an error and need to change B to a wider set of symbols, and would like to avoid falling into a copy/paste loop.

I am using phantomrel and phantombin because of a previous question of mine

Thinking of a better writing pattern my first two thoughts were:

  • Use an aligned within the original align.
  • Use an array.

Using an array?

\begin{align*}
A
& = \text{several short lines}\\
& = \text{some very long line which takes up most of the width of the page} \\ 
& = \text{several short lines} \\
& = B + \mathbb{E}(\abs{a + b + c + d}) \qquad \text{(typically too long for a single line)} \\
& \leq B 
\begin{array}[t]{r}
{} + \mathbb{E}(\abs{a}) \\
{} + \mathbb{E}(\abs{b}) \\
{} + \mathbb{E}(\abs{c}) \\
{} + \mathbb{E}(\abs{d}) \\
\end{array}
\end{align*}

enter image description here

Although this has the + symbols too far offset because of the array margin.

Use aligned?

\begin{align*}
A
& = \text{several short lines}\\
& = \text{some very long line which takes up most of the width of the page} \\ 
& = \text{several short lines} \\
& = B + \mathbb{E}(\abs{a + b + c + d}) \qquad \text{(typically too long for a single line)} \\
& 
\begin{aligned}
\leq B & + \mathbb{E}(\abs{a})  \\ % Using the triangle inequality.
& + \mathbb{E}(\abs{b}) \\
& + \mathbb{E}(\abs{c}) \\
& + \mathbb{E}(\abs{d}) 
\end{aligned}
\end{align*}

enter image description here

Almost, but the alignment is a bit off and I am not sure why.

Why not just use aligned for the whole thing?

I can't figure out how I would handle the very long lines without some selective use of mathclap (or similar), and would rather keep my use of ampersands to a minimum.

4

I would use a top-aligned aligned environment (pun intended) inside the align* environment.

Separately, I would also try to de-clutter the appearance of the equations by using fewer pairs of parentheses.

enter image description here

\documentclass{extarticle}

\usepackage{mathtools,bbm,bm,amssymb,amsthm,physics,
            letltxmacro,cleveref} % load 'cleveref' last
\DeclareMathOperator{\E}{\mathbb{E}} % expectation operator

\begin{document}
\begin{align*}
A
&= \text{several short lines}\\
&= \text{some very long line which takes up most of the width of the page} \\ 
&= \text{several short lines} \\
&= B + \E\abs{a + b + c + d} \qquad \text{(typically too long for a single line)} \\
&\leq B 
\begin{aligned}[t]
   &+ \E \abs{a} \\ % by triangle inequality
   &+ \E \abs{b} \\
   &+ \E \abs{c} \\
   &+ \E \abs{d} 
\end{aligned}
\end{align*}
\end{document}
  • Directly copying this code I get a result which is too far offset to the right ("similar" to the picture from when I tried to use array). Any idea why? – oliversm Oct 19 '18 at 8:48
  • @oliversm - Please clarify what you mean by "too far offset to the right". For sure, the screenshot I posted, which is based on the code I posted, does not indicate any issues "similar to" one of the screenshots you posted. – Mico Oct 19 '18 at 8:51
  • 1
    Shortly after commenting I tried all 3 solutions and each one has some small offset and so I can only conclude there is something either off with my build or release version. Looking into it now. – oliversm Oct 19 '18 at 9:02
  • 1
    @oliversm - How old is your TeX distribution, and when was it updated most recently? I seem to remember that either 1.5 or 2.5 years ago, the amsmath package (which is loaded automatically by the mathtools package) was updated. One of the main changes was the behavior of the aligned environment. Try inserting \! ("negative thinspace) immediately before \begin{aligned} in your code. If this resolves the alignment issue, then your TeX distribution still uses the old version of the amsmath package; my recommendation would be for you to update your TeX distribution. – Mico Oct 19 '18 at 9:12
  • I just tried it with a friend’s laptop with a 2018 distribution and it worked fine, so I will see if updating my version fixes the issue. – oliversm Oct 19 '18 at 9:17
4

enter image description here

I added {} so you get relation spacing

\documentclass{extarticle}

\usepackage{amsmath,bbm,bm,amssymb,amsthm,mathtools,cleveref,physics,letltxmacro}

% For usage in align environments. 
\newcommand\phantomrel[1]{\mathrel{\phantom{#1}}}
\newcommand\phantombin[1]{\mathbin{\phantom{#1}}}

\begin{document}
\begin{align*}
A
& = \text{several short lines}\\
& = \text{some very long line which takes up most of the width of the page} \\ 
& = \text{several short lines} \\
& = B + \mathbb{E}(\abs{a + b + c + d}) \qquad \text{(typically too long for a single line)} \\
& 
\begin{aligned}
{}\leq B & + \mathbb{E}(\abs{a})  \\ % Using the triangle inequality.
& + \mathbb{E}(\abs{b}) \\
& + \mathbb{E}(\abs{c}) \\
& + \mathbb{E}(\abs{d}) 
\end{aligned}
\end{align*}\end{document}
2

I have no problem with aligned (measure precision: 0.1pt) and this very code. Unrelated: I removed the unnecessary packages.

\documentclass{extarticle}

\usepackage{bm,amssymb, amsthm, mathtools, cleveref, physics, letltxmacro}

% For usage in align environments.
\newcommand\phantomrel[1]{\mathrel{\phantom{#1}}}
\newcommand\phantombin[1]{\mathbin{\phantom{#1}}}

\begin{document}

\begin{align*}
A
& = \text{several short lines}\\
& = \text{some very long line which takes up most of the width of the page} \\
& = \text{several short lines} \\
& = B + \mathbb{E}(\abs{a + b + c + d}) \qquad \text{(typically too long for a single line)} \\
& \leq B \begin{aligned}[t] & + \mathbb{E}(\abs{a}) \\ % Using the triangle inequality.
  & + \mathbb{E}(\abs{b}) \\
 & + \mathbb{E}(\abs{c}) \\
 & + \mathbb{E}(\abs{d})
\end{aligned}
\end{align*}

\end{document} 

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.