5

I am trying to align 3 columns of an equation using the aligned block:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation*}
    \begin{aligned}
        a[i:j] &= \langle\rangle                &\text{if } i > j \\
        a[i:j] &= \langle a_i, ..., a_j \rangle &\text{if } a = \langle a_0, ..., a_i, ... a_j, ... \rangle \\
        a[i:]  &= \langle a_i, ... \rangle      &\text{if } a = \langle a_o, ..., a_i, ... \rangle
    \end{aligned}
\end{equation*}
\end{document}

This results in the 2nd column being left-aligned, but the 1st and 3rd being right-aligned.

I would like for all of them to be left-aligned, like this: result with desired arrows

0

2 Answers 2

9

Just expanding a bit on @vonbrand's answer ...

The alignat environment is an extension of the align environment. It uses the & alignment symbol to align the blocks in alternating left and right manner.

  • If two left-aligned blocks are supposed to follow each other, use && rather than &.

  • You can create extra space between two left-aligned blocks by inserting \quad or \qquad between the consecutive & symbols.

  • The alignat environments requires an argument, an integer. To calculate this integer, take the maximal number of & symbols in any row, add 1, and divide by 2. In the example below, the maximal number & symbols is 5; adding 1 and dividing by 2 gives 3. (If the maximal number is an even number, add 1 before proceeding.)

Some additional comments: I would replace all instances of ... with \dots in order to create typographical ellipses; and I'd encase the 3 : symbols in curly braces in order to avoid the extra spacing that otherwise gets inserted.

enter image description here

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{alignat}{3} % max. # of '&' symbols in any row: 5. (5+1)/2 = 3 
& a[i{:}j] &&= \langle\,\rangle            
      &\qquad&\text{if $i > j$} \\ % or '\quad', if you prefer
& a[i{:}j] &&= \langle a_i, \dots, a_j \rangle 
      &&\text{if $a = \langle a_0, \dots, a_i, \dots a_j, \dots \rangle$} \\
& a[i{:}]  &&= \langle a_i, \dots \rangle  
      &&\text{if $a = \langle a_o, \dots, a_i, \dots \rangle$}
\end{alignat}
\end{document}
5
  • Hi, is there particular reason why you use \text{if $i > j$} rather than \text{if } i > j ?
    – Surb
    Commented Sep 16, 2020 at 12:11
  • 2
    @Surb - The reason is syntactic: the entire string if $i > j$ forms a sentence (fragment). To get the balance of text and math mode right, it's necessary to encase this sentence in a \text "wrapper", i.e., write it as \text{if $i > j$}. It's true that the outputs of \text{if $i > j$} and \text{if } i > j are the same, but this is more a happy coincidence than truly a consequence of deliberate design of [$]\text{if } i > j[$]. In a LaTeX document, it's almost always a good idea to emphasize the syntactic structure and to avoid visual formatting as much as possible.
    – Mico
    Commented Sep 16, 2020 at 12:44
  • 1
    @Mico Are you avoiding syntax highlighting on purpose? Please, don't edit old answers: you're filling the main page with bumps of old threads, with no real advantage.
    – egreg
    Commented Sep 27, 2020 at 15:43
  • @egreg - I truly cannot stand the new code coloring system that the site's powers-that-be have foisted on us. It contains semantic errors and inconsistencies, and it makes code look like as gaudy as an overdecorated Christmas tree. Better no coloring at all than bad coloring... But I'll stop now de-coloring old answers. (I will de-color my new answers, though.)
    – Mico
    Commented Sep 27, 2020 at 15:57
  • 1
    @Mico I agree that it's anything but pretty.
    – egreg
    Commented Sep 27, 2020 at 16:02
2

Use alignat, and read the documentation carefully. It has quite funny ideas of how to align column contents, you have to pick the ones to fill with some care. A bit of experimentation (and some dummy columns, perhaps for spacing only) will get you the results you desire.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .