# Align multiple matrix equations and their elements

I have a set of multiple equations with matrices and I would like that both the brackets and the elements are aligned together.

To do so, I followed the accepted answer given in this thread: Multiple matrix equations, alignment

However, with that solution I was able to align only the brackets and not the elements:

Is there a way to center-align also the different columns of the matrices and the first letter on the leftside of the equations?

Here the current code:

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

\setstackEOL{ }
\setstackgap{L}{16pt}
\fixTABwidth{T}
\stackMath
\newsavebox\boxA
\newsavebox\boxB
\newcommand\eqstencil[3]{%
\savebox\boxA{$R_x\left(\theta _{y}\right)$}
\savebox\boxB{\setstackEOL{\cr}\csname xform2\endcsname{y}}
\makebox[\wd\boxA][l]{$#1$}
=
\begin{bmatrix}\makebox[\wd\boxB]{\setstackEOL{\cr}\csname xform#3\endcsname{#2}}\end{bmatrix}}
\expandafter\def\csname xform1\endcsname#1{%
\tabbedCenterstack{
1 & 0 & 0 & 0\cr
0 & \cos\left(\theta _{#1}\right) & -\sin\left(\theta _{#1}\right) & 0\cr
0 & \sin\left(\theta _{#1}\right) & \cos\left(\theta _{#1}\right) & 0\cr
0 & 0 & 0 & 1
}}
\expandafter\def\csname xform2\endcsname#1{%
\tabbedCenterstack{
\cos\left(\theta _{#1}\right) & 0 & \sin\left(\theta _{#1}\right) & 0\cr 0 & 1 & 0 & 0\cr -\sin\left(\theta _{#1}\right) & 0 & \cos\left(\theta _{#1}\right) & 0\cr 0 & 0 & 0 & 1
}}
\expandafter\def\csname xform3\endcsname#1{%
\tabbedCenterstack{
\cos\left(\theta _{#1}\right) & -\sin\left(\theta _{#1}\right) & 0 & 0\cr \sin\left(\theta _{#1}\right) & \cos\left(\theta _{#1}\right) & 0 & 0\cr 0 & 0 & 1 & 0\cr 0 & 0 & 0 & 1
}}
\expandafter\def\csname xform4\endcsname#1{%
\tabbedCenterstack{
1 & 0 & 0 & \Delta _{x}\cr 0 & 1 & 0 & \Delta _{y}\cr\ 0 & 0 & 1 & \Delta _{z}\cr 0 & 0 & 0 & 1
}}

\begin{document}
\begin{alignat}{4}
& \eqstencil{R_x\left(\theta _{x}\right)}{x}{1}\\[5pt]
& \eqstencil{R_y\left(\theta _{y}\right)}{y}{2}\\[5pt]
& \eqstencil{R_z\left(\theta _{z}\right)}{z}{3}\\[5pt]
& \eqstencil{T}{}{4}
\end{alignat}
\end{document}


One can use \mathmakebox in one row to establish a standard cell width. It doesn't have to be the first row.

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

\newlength{\cellwidth}
\settowidth{\cellwidth}{$\displaystyle -\sin\left(\theta _x\right)$}

\begin{document}
\begin{align}
R_x\left(\theta _{x}\right) &= \begin{bmatrix}
\mathmakebox[\cellwidth]{1} & \mathmakebox[\cellwidth]{0} & \mathmakebox[\cellwidth]{0} & \mathmakebox[\cellwidth]{0} \\
0 & \cos\left(\theta _{x}\right) & -\sin\left(\theta _{x}\right) & 0 \\
0 & \sin\left(\theta _{x}\right) & \cos\left(\theta _{x}\right) & 0 \\
0 & 0 & 0 & 1
\end{bmatrix} \\
R_y\left(\theta _{y}\right) &= \begin{bmatrix}
\mathmakebox[\cellwidth]{\cos\left(\theta _{y}\right)} & \mathmakebox[\cellwidth]{0}
& \mathmakebox[\cellwidth]{\sin\left(\theta _{y}\right)} & \mathmakebox[\cellwidth]{0} \\
0 & 1 & 0 & 0 \\
-\sin\left(\theta _{y}\right) & 0 & \cos\left(\theta _{y}\right) & 0 \\
0 & 0 & 0 & 1
\end{bmatrix} \\
R_z\left(\theta _{z}\right) &= \begin{bmatrix}
\mathmakebox[\cellwidth]{\cos\left(\theta _{z}\right)} & \mathmakebox[\cellwidth]{-\sin\left(\theta _{z}\right)}
& \mathmakebox[\cellwidth]{0} & \mathmakebox[\cellwidth]{0} \\
\sin\left(\theta _{z}\right) & \cos\left(\theta _{z}\right) & 0 & 0 \\
0 & 0 & 1 & 0  \\
0 & 0 & 0 & 1
\end{bmatrix} \\
T &= \begin{bmatrix}
\mathmakebox[\cellwidth]{1} & \mathmakebox[\cellwidth]{0} & \mathmakebox[\cellwidth]{0} & \mathmakebox[\cellwidth]{\Delta _{x}}\\
0 & 1 & 0 & \Delta _{y}\\
0 & 0 & 1 & \Delta _{z}\\
0 & 0 & 0 & 1
\end{bmatrix}
\end{align}
\end{document}


• Thank you! It works and it is also less demanding to script than the code I had. May 17, 2018 at 18:57

To first approximation, this is just another iteration of your \savebox-\makebox trick.

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

\setstackEOL{ }
\setstackgap{L}{16pt}
\fixTABwidth{T}
\stackMath
\newsavebox\boxA
\newsavebox\boxB
\newsavebox\boxC
\newcommand\eqstencil[3]{%
\savebox\boxA{$R_x\left(\theta _{y}\right)$}
\savebox\boxB{\setstackEOL{\cr}\csname xform2\endcsname{y}}
\savebox\boxC{$-\cos\left(\theta _{#1}\right)$}
\makebox[\wd\boxA][l]{$#1$}
=
\begin{bmatrix}\makebox[\wd\boxB]{\setstackEOL{\cr}\csname xform#3\endcsname{#2}}\end{bmatrix}}
\expandafter\def\csname xform1\endcsname#1{%
\tabbedCenterstack{
1 & 0 & 0 & 0\cr
0 & \cos\left(\theta _{#1}\right) & -\sin\left(\theta _{#1}\right) & 0\cr
0 & \sin\left(\theta _{#1}\right) & \cos\left(\theta _{#1}\right) & 0\cr
0 & 0 & 0 & 1
}}
\expandafter\def\csname xform2\endcsname#1{%
\tabbedCenterstack{
\cos\left(\theta _{#1}\right) & 0 & \sin\left(\theta _{#1}\right) & 0\cr 0 & 1 & 0 & 0\cr -\sin\left(\theta _{#1}\right) & 0 & \cos\left(\theta _{#1}\right) & 0\cr 0 & 0 & 0 & 1
}}
\expandafter\def\csname xform3\endcsname#1{%
\tabbedCenterstack{
\cos\left(\theta _{#1}\right) & -\sin\left(\theta _{#1}\right) & 0 & 0\cr \sin\left(\theta _{#1}\right) & \cos\left(\theta _{#1}\right) & 0 & 0\cr 0 & 0 & 1 & 0\cr 0 & 0 & 0 & 1
}}
\expandafter\def\csname xform4\endcsname#1{%
\tabbedCenterstack{
\makebox[\wd\boxC][c]{1} & 0 & 0 & \Delta _{x}\cr
0 & 1 & 0 & \Delta _{y}\cr 0 & 0 & 1 & \Delta _{z}\cr 0 & 0 & 0 & 1
}}

\begin{document}
\begin{alignat}{4}
& \eqstencil{R_x\left(\theta _{x}\right)}{x}{1}\\[5pt]
& \eqstencil{R_y\left(\theta _{y}\right)}{y}{2}\\[5pt]
& \eqstencil{R_z\left(\theta _{z}\right)}{z}{3}\\[5pt]
& \eqstencil{T}{}{4}
\end{alignat}
\end{document}


• Thanks a lot, the script works and it nicely improves the code I had! Very helpful solution that complements the other one. May 17, 2018 at 19:01