[Tex/LaTex] Multiple matrix equations, alignment

alignatamsmathhorizontal alignmentmatricesstackengine

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

enter image description here

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}

Best Answer

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}

enter image description here


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}

enter image description here


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}

enter image description here

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

enter image description here

Related Question