2

I am trying to produce a three line equation, aligned on the start of each line and on the plus signs. I have tried this

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\begin{equation} \begin{aligned} &1\cdot \varepsilon_1&+ 1\cdot \varepsilon_2 &+ \dots + 1\cdot \varepsilon_n\ &X_{21} \cdot \varepsilon_1 &+ X_{22} \cdot \varepsilon_2 &+ \dots + X_{2n} \cdot \varepsilon_n\ &X_{k1} \cdot \varepsilon_1 &+ X_{k 2} \cdot \varepsilon_2 &+ \dots + X_{kn} \cdot \varepsilon_n \ \end{aligned} \end{equation}

\end{document}

Which produces this

enter image description here

I tried replacing the 1's in the first line with X_{21}, X_{22} etc as in the second line, and it aligns properly then, but when I use 1's again, the equations go back to being unaligned again.

user202729
  • 7,143
L Robinson
  • 21
  • 3

2 Answers2

2

Remember 2n-1 &'s on each line with n alignments. n &'s for alignments and n-1 &'s to separate the alignment columns. I'd also use alignedat instead as in this example

\documentclass[a4paper]{article}

\usepackage{amsmath}

\begin{document}

\begin{equation} \begin{alignedat}{3} &1\cdot \varepsilon_1 &&+ 1\cdot \varepsilon_2 &&+ \dots + 1\cdot \varepsilon_n\ &X_{21} \cdot \varepsilon_1 &&+ X_{22} \cdot \varepsilon_2 &&+ \dots + X_{2n} \cdot \varepsilon_n\ &X_{k1} \cdot \varepsilon_1 &&+ X_{k 2} \cdot \varepsilon_2 &&+ \dots + X_{kn} \cdot \varepsilon_n \ \end{alignedat} \end{equation} \end{document}

enter image description here

daleif
  • 54,450
1

Here are three more array-based solutions. One each depending on whether the cell contents should be left-aligned, centered, or right-aligned. Observe that contents of the three array environments are identical; they only differ in the use of the L, C, and R column types.

A separate topic: The code uses \cdots rather than \dots, as LaTeX doesn't "know" that the dots will be flanked by + (a binary operator) on either side -- but in the adjacent cells.( If +\dots+ is found, LaTeX is smart enough to change \dots to \cdots.)

enter image description here

\documentclass{article} % or some other suitable document class

\usepackage{array} % for '\newcolumntype' macro \newcolumntype{O}{>{{}}c<{{}}} % for operators ( + , \cdot ) \newcolumntype{C}{>{\displaystyle}c} \newcolumntype{L}{>{\displaystyle}l} \newcolumntype{R}{>{\displaystyle}r}

\usepackage{amsmath} % for 'gather' environment

\begin{document}

\setlength\arraycolsep{0pt} \renewcommand\arraystretch{1.33} \begin{gather} \begin{array}{ {6}{LO} L } % left-aligned 1 &\cdot& \varepsilon_1 &+& 1 &\cdot& \varepsilon_2 &+& \cdots &+& 1 &\cdot& \varepsilon_n\ X_{21} &\cdot& \varepsilon_1 &+& X_{22} &\cdot& \varepsilon_2 &+& \cdots &+& X_{2n} &\cdot& \varepsilon_n\ X_{k1} &\cdot& \varepsilon_1 &+& X_{k 2} &\cdot& \varepsilon_2 &+& \cdots &+& X_{kn} &\cdot& \varepsilon_n \end{array} \[2\jot] \begin{array}{ {6}{CO} C } % centered 1 &\cdot& \varepsilon_1 &+& 1 &\cdot& \varepsilon_2 &+& \cdots &+& 1 &\cdot& \varepsilon_n\ X_{21} &\cdot& \varepsilon_1 &+& X_{22} &\cdot& \varepsilon_2 &+& \cdots &+& X_{2n} &\cdot& \varepsilon_n\ X_{k1} &\cdot& \varepsilon_1 &+& X_{k 2} &\cdot& \varepsilon_2 &+& \cdots &+& X_{kn} &\cdot& \varepsilon_n \end{array} \[2\jot] \begin{array}{ *{6}{RO} R } % right-aligned 1 &\cdot& \varepsilon_1 &+& 1 &\cdot& \varepsilon_2 &+& \cdots &+& 1 &\cdot& \varepsilon_n\ X_{21} &\cdot& \varepsilon_1 &+& X_{22} &\cdot& \varepsilon_2 &+& \cdots &+& X_{2n} &\cdot& \varepsilon_n\ X_{k1} &\cdot& \varepsilon_1 &+& X_{k 2} &\cdot& \varepsilon_2 &+& \cdots &+& X_{kn} &\cdot& \varepsilon_n \end{array} \end{gather}

\end{document}

Mico
  • 506,678