# Multiple matrix equations, alignment

I want to write three matrix equations on multiple lines so I'm using alignat.

However, the widths of the matrices on each line are not equal. How can I make sure they will be? I've been looking into the tabstackengine but it doesn't work so well inside alignat. I'd like to avoid using \phantom. Any suggestions?

\documentclass[11pt,twoside,a4paper]{article}
\usepackage{amsmath}

\begin{alignat}{3}
& \begin{bmatrix} X \\ Y \\ Z \end{bmatrix} &&= \textbf{T}_3 (\phi) \begin{bmatrix} X \\ Y \\ Z \end{bmatrix} &&= \begin{bmatrix}
\text{cos($\psi$)} & \text{sin($\psi$)} & 0\\
\text{-sin($\psi$)} & \text{cos($\psi$)} & 0 \\
0 & 0 & 1 \\
\end{bmatrix}
\begin{bmatrix}
X \\
Y \\
Z \\
\end{bmatrix} \\
& \begin{bmatrix} X' \\ Y' \\ Z' \end{bmatrix} &&= \textbf{T}_2 (\theta) \begin{bmatrix} X' \\ Y' \\ Z' \end{bmatrix} &&= \begin{bmatrix}
\text{cos($\theta$)} & 0 & \text{-sin($\theta$)}\\
0 & 1 & 0 \\
\text{sin($\theta$)} & 0 & \text{cos($\theta$)} \\
\end{bmatrix}
\begin{bmatrix}
X' \\
Y' \\
Z' \\
\end{bmatrix} \\
& \begin{bmatrix} X'' \\ Y'' \\ Z'' \end{bmatrix} &&= \textbf{T}_1 (\psi) \begin{bmatrix} X'' \\ Y'' \\ Z'' \end{bmatrix} &&= \begin{bmatrix}
1 & 0 & 0\\
0 & \text{cos($\phi$)} & \text{sin($\phi$)} \\
0 & \text{-sin($\phi$)} & \text{cos($\phi$)} \\
\end{bmatrix}
\begin{bmatrix}
X'' \\
Y'' \\
Z'' \\
\end{bmatrix}
\end{alignat}


Here, \eqstencil has to be tailor made for your equation.

RE-REVISED ANSWER to make transformation matrix of uniform total width and fixed inter-column width (using TABstacks), as well as increased vertical interrow spacing.

\documentclass[11pt,twoside,a4paper]{article}
\usepackage{amsmath,tabstackengine}
\setstackEOL{ }
\setstackgap{L}{16pt}
\fixTABwidth{T}
\stackMath
\newsavebox\boxA
\newsavebox\boxB
\newsavebox\boxC
\newcommand\eqstencil[3]{%
\savebox\boxA{$X''$}
\savebox\boxB{$\textbf{T}_3 (\psi)$}
\savebox\boxC{\setstackEOL{\cr}\csname xform3\endcsname{\psi}}
\begin{bmatrix}\makebox[\wd\boxA]{\Centerstack{X#1 Y#1 Z#1}}\end{bmatrix}
=
\makebox[\wd\boxB][l]{$\textbf{T}_#2 (#3)$}
\begin{bmatrix}\makebox[\wd\boxA]{%
\Centerstack{X#1 Y#1 Z#1}}\end{bmatrix}
=
\begin{bmatrix}\makebox[\wd\boxC]{\setstackEOL{\cr}\csname xform#2\endcsname{#3}}\end{bmatrix}
\begin{bmatrix}\makebox[\wd\boxA]{\Centerstack{X#1 Y#1 Z#1}}\end{bmatrix}}
\expandafter\def\csname xform1\endcsname#1{%
\tabbedCenterstack{
\cos(#1) & 0 & -\sin(#1) \cr
0 & 1 & 0 \cr
\sin(#1) & 0 & \cos(#1)
}}
\expandafter\def\csname xform2\endcsname#1{%
\tabbedCenterstack{
\cos(#1) & 0 & -\sin(#1) \cr
0 & 1 & 0 \cr
\sin(#1) & 0 & \cos(#1)
}}
\expandafter\def\csname xform3\endcsname#1{%
\tabbedCenterstack{
1 & 0 & 0 \cr
0 & \cos(#1) & \sin(#1) \cr
0 & -\sin(#1) & \cos(#1)
}}
\begin{document}
\begin{alignat}{3}
& \eqstencil{}{3}{\psi}\5pt] & \eqstencil{'}{2}{\theta} \\[5pt] & \eqstencil{''}{1}{\phi} \end{alignat} \end{document}  REVISED ANSWER to make vectors of equal width \documentclass[11pt,twoside,a4paper]{article} \usepackage{amsmath,stackengine} \setstackgap{L}{12pt} \stackMath \newsavebox\boxA \newsavebox\boxB \newsavebox\boxC \newcommand\eqstencil[3]{% \savebox\boxA{X''} \savebox\boxB{\textbf{T}_3 (\psi)} \savebox\boxC{\csname xform3\endcsname{\psi}} \begin{bmatrix}\makebox[\wd\boxA]{\Centerstack{X#1 Y#1 Z#1}}\end{bmatrix} = \makebox[\wd\boxB][l]{\textbf{T}_#2 (#3) } \begin{bmatrix}\makebox[\wd\boxA]{\Centerstack{X#1 Y#1 Z#1}}\end{bmatrix} = \makebox[\wd\boxC]{\csname xform#2\endcsname{#3}} \begin{bmatrix}\makebox[\wd\boxA]{\Centerstack{X#1 Y#1 Z#1}}\end{bmatrix}} \expandafter\def\csname xform1\endcsname#1{% \begin{bmatrix} \cos(#1) & 0 & -\sin(#1) \\ 0 & 1 & 0 \\ \sin(#1) & 0 & \cos(#1) \\ \end{bmatrix}} \expandafter\def\csname xform2\endcsname#1{% \begin{bmatrix} \cos(#1) & 0 & -\sin(#1) \\ 0 & 1 & 0 \\ \sin(#1) & 0 & \cos(#1) \\ \end{bmatrix}} \expandafter\def\csname xform3\endcsname#1{% \begin{bmatrix} 1 & 0 & 0 \\ 0 & \cos(#1) & \sin(#1) \\ 0 & -\sin(#1) & \cos(#1) \\ \end{bmatrix}} \begin{document} \begin{alignat}{3} & \eqstencil{}{3}{\psi}\\ & \eqstencil{'}{2}{\theta} \\ & \eqstencil{''}{1}{\phi} \end{alignat} \end{document}  ORIGINAL ANSWER \documentclass[11pt,twoside,a4paper]{article} \usepackage{amsmath} \newsavebox\boxA \newsavebox\boxB \newsavebox\boxC \newcommand\eqstencil[3]{% \savebox\boxA{\begin{bmatrix} X''\\Y''\\Z'' \end{bmatrix}} \savebox\boxB{\textbf{T}_3 (\psi)} \savebox\boxC{\csname xform3\endcsname{\psi}} \makebox[\wd\boxA]{\begin{bmatrix} X#1 \\ Y#1 \\ Z#1 \end{bmatrix}} = \makebox[\wd\boxB][l]{\textbf{T}_#2 (#3) } \makebox[\wd\boxA]{\begin{bmatrix} X#1 \\ Y#1 \\ Z#1 \end{bmatrix}} = \makebox[\wd\boxC]{\csname xform#2\endcsname{#3}} \makebox[\wd\boxA]{\begin{bmatrix} X#1 \\ Y#1 \\ Z#1 \end{bmatrix}}} \expandafter\def\csname xform1\endcsname#1{% \begin{bmatrix} \cos(#1) & 0 & -\sin(#1) \\ 0 & 1 & 0 \\ \sin(#1) & 0 & \cos(#1) \\ \end{bmatrix}} \expandafter\def\csname xform2\endcsname#1{% \begin{bmatrix} \cos(#1) & 0 & -\sin(#1) \\ 0 & 1 & 0 \\ \sin(#1) & 0 & \cos(#1) \\ \end{bmatrix}} \expandafter\def\csname xform3\endcsname#1{% \begin{bmatrix} 1 & 0 & 0 \\ 0 & \cos(#1) & \sin(#1) \\ 0 & -\sin(#1) & \cos(#1) \\ \end{bmatrix}} \begin{document} \begin{alignat}{3} & \eqstencil{}{3}{\psi}\\ & \eqstencil{'}{2}{\theta} \\ & \eqstencil{''}{1}{\phi} \end{alignat} \end{document}  Different alignments can be obtained by setting alignments on the \makeboxes. For example, the redefinition \newcommand\eqstencil[3]{% \savebox\boxA{\begin{bmatrix} X''\\ Y''\\Z'' \end{bmatrix}} \savebox\boxB{\textbf{T}_3 (\psi)} \savebox\boxC{\csname xform3\endcsname{\psi}} \makebox[\wd\boxA][r]{\begin{bmatrix} X#1 \\ Y#1 \\ Z#1 \end{bmatrix}} = \makebox[\wd\boxB][l]{\textbf{T}_#2 (#3) } \makebox[\wd\boxA][r]{\begin{bmatrix} X#1 \\ Y#1 \\ Z#1 \end{bmatrix}} = \makebox[\wd\boxC][r]{\csname xform#2\endcsname{#3}} \makebox[\wd\boxA][l]{\begin{bmatrix} X#1 \\ Y#1 \\ Z#1 \end{bmatrix}}}  gives • This is a nice approach and the alignment looks much better now. However, just to mess with you, the matrices do not have equal width. I asked about this before and was redirected to this post. So I tried to use tabstackengine inside the alignat, to specify the width of the matrices, but that didn't work. Commented Jun 2, 2015 at 15:57 • @MichaelDeSanta When you say "equal width" to you mean the total width, or were you referring to the notion of equal column widths? I regret to admit that tabstackengine does not do best when mixed with the amsmath environments, though I can often overcome that inefficiently, which sort of removes the whole reason for going there. One thing I have found that sometimes helps is to change the EOL and tabbing delimiters to something different, e.g., \setstackEOL{\cr}\setstackTAB{\&} and use, for example, \tabbedCenterstack{0 \& 1 \& 0 \cr ...}. Commented Jun 2, 2015 at 16:03 • @MichaelDeSanta Another thing tabstackengine cannot do is assign unique equation numbers to different rows of a TABstack. Commented Jun 2, 2015 at 16:06 • What I would like is to have the column matrix [X Y Z] to have equal width as column matrix [X' Y' Z'] and [X'' Y'' Z''] Commented Jun 2, 2015 at 16:11 • @MichaelDeSanta The \fixTABwidth feature of tabstackengine will not help you there, because it can make the columns of a single matrix equal width, but it cannot make the columns of different matrices to matching width. But I will see if I can adjust my answer to provide it some other way. Commented Jun 2, 2015 at 16:16 You can space out each of the entries in each matrix based on the widest element across the equations. For this the command \spaceto{<source>}{<target>} uses \ooalign to create a "symbol overlay" with a \phantom{<source>} and <target>: \documentclass{article} \usepackage{amsmath} \newcommand{\spaceto}[2]{{\ooalign{\phantom{#1}\cr\hidewidth#2\hidewidth}}} \begin{document} % Original layout \begin{alignat}{3} & \begin{bmatrix} X \\ Y \\ Z \end{bmatrix} &&= \textbf{T}_3 (\phi) \begin{bmatrix} X \\ Y \\ Z \end{bmatrix} &&= \begin{bmatrix} \text{cos(\psi)} & \text{sin(\psi)} & 0\\ \text{-sin(\psi)} & \text{cos(\psi)} & 0 \\ 0 & 0 & 1 \\ \end{bmatrix} \begin{bmatrix} X \\ Y \\ Z \\ \end{bmatrix} \\ & \begin{bmatrix} X' \\ Y' \\ Z' \end{bmatrix} &&= \textbf{T}_2 (\theta) \begin{bmatrix} X' \\ Y' \\ Z' \end{bmatrix} &&= \begin{bmatrix} \text{cos(\theta)} & 0 & \text{-sin(\theta)}\\ 0 & 1 & 0 \\ \text{sin(\theta)} & 0 & \text{cos(\theta)} \\ \end{bmatrix} \begin{bmatrix} X' \\ Y' \\ Z' \\ \end{bmatrix} \\ & \begin{bmatrix} X'' \\ Y'' \\ Z'' \end{bmatrix} &&= \textbf{T}_1 (\psi) \begin{bmatrix} X'' \\ Y'' \\ Z'' \end{bmatrix} &&= \begin{bmatrix} 1 & 0 & 0\\ 0 & \text{cos(\phi)} & \text{sin(\phi)} \\ 0 & \text{-sin(\phi)} & \text{cos(\phi)} \\ \end{bmatrix} \begin{bmatrix} X'' \\ Y'' \\ Z'' \\ \end{bmatrix} \end{alignat} % Original layout \begin{alignat}{3} & \begin{bmatrix} \spaceto{X''}{X} \\ Y \\ Z \end{bmatrix} &&= \spaceto{\textbf{T}_1(\psi)}{\textbf{T}_3 (\phi)} \begin{bmatrix} \spaceto{X''}{X} \\ Y \\ Z \end{bmatrix} &&= \begin{bmatrix} \cos(\psi) & \sin(\psi) & \spaceto{-\sin(\theta)}{0} \\ -\sin(\psi) & \cos(\psi) & 0 \\ 0 & \spaceto{-\sin(\phi)}{0} & 1 \\ \end{bmatrix} \begin{bmatrix} \spaceto{X''}{X} \\ Y \\ Z \\ \end{bmatrix} \\ & \begin{bmatrix} \spaceto{X''}{X'} \\ Y' \\ Z' \end{bmatrix} &&= \spaceto{\textbf{T}_1(\psi)}{\textbf{T}_2 (\theta)} \begin{bmatrix} \spaceto{X''}{X'} \\ Y' \\ Z' \end{bmatrix} &&= \begin{bmatrix} \cos(\theta) & \spaceto{-\sin(\phi)}{0} & -\sin(\theta) \\ \spaceto{-\sin(\psi)}{0} & 1 & 0 \\ \sin(\theta) & 0 & \cos(\theta) \\ \end{bmatrix} \begin{bmatrix} \spaceto{X''}{X'} \\ Y' \\ Z' \\ \end{bmatrix} \\ & \begin{bmatrix} X'' \\ Y'' \\ Z'' \end{bmatrix} &&= \textbf{T}_1 (\psi) \begin{bmatrix} X'' \\ Y'' \\ Z'' \end{bmatrix} &&= \begin{bmatrix} 1 & 0 & \spaceto{-\sin(\theta)}{0}\\ \spaceto{-\sin(\psi)}{0} & \cos(\phi) & \sin(\phi) \\ 0 & -\sin(\phi) & \cos(\phi) \\ \end{bmatrix} \begin{bmatrix} X'' \\ Y'' \\ Z'' \\ \end{bmatrix} \end{alignat} \end{document}  Note that there's no need for using \text when printing the tangential functions. Use the operators \sin and \cos. I have no solution for your request but two other recommendations. I would not enlarge the whitespace in the matrices but try to put some more alignments in order to get it tidier. The second approach would be to facilitate the whole thing and to avoid it completely. Btw. Why don't you like phantoms? It would be the typical way here and would just be needed six times in your above code... % arara: pdflatex \documentclass[11pt,twoside,a4paper]{article} \usepackage{mathtools} \newcommand*{\whateveryoutrytosaywiththat}[1]{\mathbf{#1}} % use custom commands which explain your syntax and make it changeable afterwards. \begin{document} You could try \begin{alignat}{3} \begin{bmatrix} X \\ Y \\ Z \end{bmatrix} &= \whateveryoutrytosaywiththat{T}_3(\phi) &\begin{bmatrix} X \\ Y \\ Z \end{bmatrix} &= \begin{bmatrix} \cos(\psi) & \sin(\psi) & 0\\ -\sin(\psi) & \cos(\psi) & 0 \\ 0 & 0 & 1 \\ \end{bmatrix} &&\begin{bmatrix} X \\ Y \\ Z \\ \end{bmatrix} \\ \begin{bmatrix} X' \\ Y' \\ Z' \end{bmatrix} &= \whateveryoutrytosaywiththat{T}_2(\theta) &\begin{bmatrix} X' \\ Y' \\ Z' \end{bmatrix} &= \begin{bmatrix} \cos(\theta) & 0 & -\sin(\theta)\\ 0 & 1 & 0 \\ \sin(\theta) & 0 & \cos(\theta) \\ \end{bmatrix} &&\begin{bmatrix} X' \\ Y' \\ Z' \\ \end{bmatrix} \\ \begin{bmatrix} X'' \\ Y'' \\ Z'' \end{bmatrix} &= \whateveryoutrytosaywiththat{T}_1(\psi) &\begin{bmatrix} X'' \\ Y'' \\ Z'' \end{bmatrix} &= \begin{bmatrix} 1 & 0 & 0\\ 0 & \cos(\phi) & \sin(\phi) \\ 0 & -\sin(\phi) & \cos(\phi) \\ \end{bmatrix} &&\begin{bmatrix} X'' \\ Y'' \\ Z'' \\ \end{bmatrix} \end{alignat} or you do \begin{alignat}{3} \vec{A} &= \whateveryoutrytosaywiththat{T}_3(\phi) \vec{A} &&= \begin{bmatrix} \cos(\psi) & \sin(\psi) & 0\\ -\sin(\psi) & \cos(\psi) & 0 \\ 0 & 0 & 1 \\ \end{bmatrix} &&\vec{A} \\ \vec{A'} &= \whateveryoutrytosaywiththat{T}_2(\theta) \vec{A'} &&= \begin{bmatrix} \cos(\theta) & 0 & -\sin(\theta)\\ 0 & 1 & 0 \\ \sin(\theta) & 0 & \cos(\theta) \\ \end{bmatrix} &&\vec{A'} \\ \vec{A''} &= \whateveryoutrytosaywiththat{T}_1(\psi) \vec{A''} &&= \begin{bmatrix} 1 & 0 & 0\\ 0 & \cos(\phi) & \sin(\phi) \\ 0 & -\sin(\phi) & \cos(\phi) \\ \end{bmatrix} &&\vec{A''} \end{alignat} where \[ \vec{A}=\begin{bmatrix} X \\ Y \\ Z \end{bmatrix}\wedge \vec{A'}=\begin{bmatrix} X' \\ Y' \\ Z' \end{bmatrix}\wedge \vec{A''}=\begin{bmatrix} X'' \\ Y'' \\ Z'' \end{bmatrix}
\end{document}


• I like the second approach of working around the problem altogether Commented Jun 2, 2015 at 15:53

I have started using LaTeX not so long ago and this is my first answer. Anyway I would do something like this using the {easybmat} package, which is shown here http://cs.brown.edu/about/system/managed/latex/doc/docbmat.pdf

        \documentclass[11pt,twoside,a4paper]{article}
\usepackage{amsmath}
\usepackage{easybmat}

\begin{document}

\begin{alignat}{4}
&
\left[
\begin{BMAT}(,20pt, 15pt){c}{ccc}
X \\
Y \\
Z
\end{BMAT}
\right]
&&= \textbf{T}_3 (\phi)
&& \left[
\begin{BMAT}(,20pt, 15pt){c}{ccc}
X \\
Y \\
Z
\end{BMAT}
\right]
&&= \left[
\begin{BMAT}(,26pt, 15pt){ccc}{ccc}
\text{cos($\psi$)} & \text{sin($\psi$)} & 0\\
\text{-sin($\psi$)} & \text{cos($\psi$)} & 0 \\
0 & 0 & 1 \\
\end{BMAT}
\right]
\left[
\begin{BMAT}(,20pt, 15pt){c}{ccc}
X \\
Y \\
Z
\end{BMAT}
\right] \\
& \left[
\begin{BMAT}(,20pt, 15pt){c}{ccc}
X' \\
Y' \\
Z'
\end{BMAT}
\right]
&&= \textbf{T}_2 (\theta)
&&    \left[
\begin{BMAT}(,19pt, 15pt){c}{ccc}
X' \\
Y' \\
Z'
\end{BMAT}
\right]
&&= \left[
\begin{BMAT}(,30pt, 15pt){ccc}{ccc}
\text{cos($\theta$)} & 0 & \text{-sin($\theta$)}\\
0 & 1 & 0 \\
\text{sin($\theta$)} & 0 & \text{cos($\theta$)} \\
\end{BMAT}
\right]
\left[
\begin{BMAT}(,20pt, 15pt){c}{ccc}
X' \\
Y' \\
Z' \\
\end{BMAT}
\right] \\
& \left[
\begin{BMAT}(,20pt, 15pt){c}{ccc}
X'' \\
Y'' \\
Z''
\end{BMAT}
\right]
&&= \textbf{T}_1 (\psi)
&& \left[
\begin{BMAT}(,19pt, 15pt){c}{ccc}
X'' \\
Y'' \\
Z''
\end{BMAT}
\right]
&&= \left[
\begin{BMAT}(,28pt, 15pt){ccc}{ccc}
1 & 0 & 0\\
0 & \text{cos($\phi$)} & \text{sin($\phi$)} \\
0 & \text{-sin($\phi$)} & \text{cos($\phi$)} \\
\end{BMAT}
\right]
\left[
\begin{BMAT}(,20pt, 15pt){c}{ccc}
X'' \\
Y'' \\
Z'' \\
\end{BMAT}
\right]
\end{alignat}

\end{document}


• It's quite long, but gives a nice looking result. Welcome to the site. Commented Jun 2, 2015 at 16:49
• Yes, very nice result. I'll see if I can combine @StevenB.Segletes suggestion with yours Commented Jun 2, 2015 at 16:52
• Welcome to TeX.SX! You could reduce this to an \begin{alignat}{2} (e.g. before first = and second vector). The widths of your vectors are slightly differing. You may want to set all to the same size. Commented Jun 3, 2015 at 7:50