# Align many matrices and operations so it's more beautiful

I have the following matrices, I'd like to align them under each other more beautifully. Can anyone recommend a good way to do this?

$\left[\begin{matrix} 1 & 1 & 1 & 6z-2 \\ -1 & 2 & 0 & 4z-5 \\ 1 & 0 & -1 & z-3 \\ 1 & 2 & 0 & 8z-7 \end{matrix}\right] R_2-(-1)R_1->R_2 \left[\begin{matrix} 1 & 1 & 1 & 6z-2 \\ 0 & 3 & 1 & 10z-7 \\ 1 & 0 & -1 & z-3 \\ 1 & 2 & 0 & 8z-7 \end{matrix}\right] R_3-1R_1->R_3$ \bigskip

$\left[\begin{matrix} 1 & 1 & 1 & 6z-2 \\ 0 & 3 & 1 & 10z-7 \\ 0 & -1 & -2 & -5z-1 \\ 1 & 2 & 0 & 8z-7 \end{matrix}\right] R_4-1R_1->R_4 \left[\begin{matrix} 1 & 1 & 1 & 6z-2 \\ 0 & 3 & 1 & 10z-7 \\ 0 & -1 & -2 & -5z-1 \\ 0 & 1 & -1 & 2z-5 \end{matrix}\right] R_2/(3)->R_2$ \bigskip

$\left[\begin{matrix} 1 & 1 & 1 & 6z-2 \\ 0 & 1 & \frac{1}{3} & \frac{10z-7}{3} \\ 0 & -1 & -2 & -5z-1 \\ 0 & 1 & -1 & 2z-5 \end{matrix}\right] R_3-(-1)R_2->R_3 \left[\begin{matrix} 1 & 1 & 1 & 6z-2 \\ 0 & 1 & \frac{1}{3} & \frac{10z-7}{3} \\ 0 & 0 & \frac{-5}{3} & \frac{-5z-10}{3} \\ 0 & 1 & -1 & 2z-5 \end{matrix}\right] R_4-1R_2->R_4$ \bigskip

$\left[\begin{matrix} 1 & 1 & 1 & 6z-2 \\ 0 & 1 & \frac{1}{3} & \frac{10z-7}{3} \\ 0 & 0 & \frac{-5}{3} & \frac{-5z-10}{3} \\ 0 & 0 & \frac{-4}{3} & \frac{-4z-8}{3} \end{matrix}\right] R_3/((-5)/3)->R_3 \left[\begin{matrix} 1 & 1 & 1 & 6z-2 \\ 0 & 1 & \frac{1}{3} & \frac{10z-7}{3} \\ 0 & 0 & 1 & z+2 \\ 0 & 0 & \frac{-4}{3} & \frac{-4z-8}{3} \end{matrix}\right] R_4-((-4)/3)R_3->R_4$ \bigskip

$\left[\begin{matrix} 1 & 1 & 1 & 6z-2 \\ 0 & 1 & \frac{1}{3} & \frac{10z-7}{3} \\ 0 & 0 & 1 & z+2 \\ 0 & 0 & 0 & 0 \end{matrix}\right] R_2-(1/3)R_3->R_2 \left[\begin{matrix} 1 & 1 & 1 & 6z-2 \\ 0 & 1 & 0 & 3z-3 \\ 0 & 0 & 1 & z+2 \\ 0 & 0 & 0 & 0 \end{matrix}\right] R_1-1R_3->R_1$ \bigskip

$\left[\begin{matrix} 1 & 1 & 0 & 5z-4 \\ 0 & 1 & 0 & 3z-3 \\ 0 & 0 & 1 & z+2 \\ 0 & 0 & 0 & 0 \end{matrix}\right] R_1-1R_2->R_1 \left[\begin{matrix} 1 & 0 & 0 & 2z-1 \\ 0 & 1 & 0 & 3z-3 \\ 0 & 0 & 1 & z+2 \\ 0 & 0 & 0 & 0 \end{matrix}\right]$

$\left[\begin{matrix} x_1 & = & 2z-1 \\ x_2 & = & 3z-3 \\ x_3 & = & z+2 \end{matrix}\right]$

• Please provide a minimal working example (with \documentclass, \begin{document}, etc.). You can probably put everything in a large array of three columns (for the desired alignment). And use the bmatrix environment from amsmath to make the code a bit shorter and nicer. – frougon May 15 at 18:56

I thought I'd use nicematrix package: https://ctan.org/pkg/nicematrix . You can see that the matrices are all the same. On each row you can make the transformations by row and by column as in the minimal example I showed you. I don't know if this can correspond to what you are looking for.

\documentclass[a4paper,12pt]{article}
\usepackage{mathtools,amssymb}
\usepackage{nicematrix}

\usepackage[left=.1in, right=.5in]{geometry}

\begin{document}
$\begin{matrix} \begin{bNiceArrayC}{CCCC} 1 & 1 & 1 & 6z-2 & \\ -1 & 2 & 0 & 4z-5 & R_2-(-1)R_1\rightarrow R_2 \\ 1 & 0 & -1 & z-3 & \\ 1 & 2 & 0 & 8z-7 & \\ \end{bNiceArrayC} & \hspace{4cm} \begin{bNiceArrayC}{CCCC} 1 & 1 & 1 & 6z-2 & \\ 0 & 3 & 1 & 10z-7 & \\ 1 & 0 & -1 & z-3 & R_3-1R_1\rightarrow R_3\\ 1 & 2 & 0 & 8z-7 & \\ \end{bNiceArrayC} \\[2cm] \begin{bNiceArrayC}{CCCC} 1 & 1 & 1 & 6z-2 & \\ -1 & 2 & 0 & 4z-5 & R_2-(-1)R_1\rightarrow R_2 \\ 1 & 0 & -1 & z-3 & \\ 1 & 2 & 0 & 8z-7 & \\ \end{bNiceArrayC} & \hspace{4cm} \begin{bNiceArrayC}{CCCC} 1 & 1 & 1 & 6z-2 & \\ 0 & 3 & 1 & 10z-7 & \\ 1 & 0 & -1 & z-3 & R_3-1R_1\rightarrow R_3\\ 1 & 2 & 0 & 8z-7 & \\ \end{bNiceArrayC} \\[2cm] \begin{bNiceArrayC}{CCCC} 1 & 1 & 1 & 6z-2 & \\ -1 & 2 & 0 & 4z-5 & R_2-(-1)R_1\rightarrow R_2 \\ 1 & 0 & -1 & z-3 & \\ 1 & 2 & 0 & 8z-7 & \\ \end{bNiceArrayC} & \hspace{4cm} \begin{bNiceArrayC}{CCCC} 1 & 1 & 1 & 6z-2 & \\ 0 & 3 & 1 & 10z-7 & \\ 1 & 0 & -1 & z-3 & R_3-1R_1\rightarrow R_3\\ 1 & 2 & 0 & 8z-7 & \\ \end{bNiceArrayC} \\ \end{matrix}$
\end{document}


\documentclass[a4paper,12pt]{article}
\usepackage{mathtools,amssymb}
\usepackage{nicematrix}

\usepackage[left=.1in, right=.5in]{geometry}

\begin{document}
$\begin{matrix} \begin{bNiceArrayC}{CCC|C} 1 & 1 & 1 & 6z-2 & \\ -1 & 2 & 0 & 4z-5 & R_2-(-1)R_1\rightarrow R_2 \\ 1 & 0 & -1 & z-3 & \\ 1 & 2 & 0 & 8z-7 & \\ \end{bNiceArrayC} & \hspace{4cm} \begin{bNiceArrayC}{CCC|C} 1 & 1 & 1 & 6z-2 & \\ 0 & 3 & 1 & 10z-7 & \\ 1 & 0 & -1 & z-3 & R_3-1R_1\rightarrow R_3\\ 1 & 2 & 0 & 8z-7 & \\ \end{bNiceArrayC} \\[2cm] \begin{bNiceArrayC}{CCC|C} 1 & 1 & 1 & 6z-2 & \\ -1 & 2 & 0 & 4z-5 & R_2-(-1)R_1\rightarrow R_2 \\ 1 & 0 & -1 & z-3 & \\ 1 & 2 & 0 & 8z-7 & \\ \end{bNiceArrayC} & \hspace{4cm} \begin{bNiceArrayC}{CCC|C} 1 & 1 & 1 & 6z-2 & \\ 0 & 3 & 1 & 10z-7 & \\ 1 & 0 & -1 & z-3 & R_3-1R_1\rightarrow R_3\\ 1 & 2 & 0 & 8z-7 & \\ \end{bNiceArrayC} \\[2cm] \begin{bNiceArrayC}{CCC|C} 1 & 1 & 1 & 6z-2 & \\ -1 & 2 & 0 & 4z-5 & R_2-(-1)R_1\rightarrow R_2 \\ 1 & 0 & -1 & z-3 & \\ 1 & 2 & 0 & 8z-7 & \\ \end{bNiceArrayC} & \hspace{4cm} \begin{bNiceArrayC}{CCC|C} 1 & 1 & 1 & 6z-2 & \\ 0 & 3 & 1 & 10z-7 & \\ 1 & 0 & -1 & z-3 & R_3-1R_1\rightarrow R_3\\ 1 & 2 & 0 & 8z-7 & \\ \end{bNiceArrayC} \\ \end{matrix}$
\end{document}

• This is really beautiful! Thank you sebastiano! – jubibanna May 15 at 21:56

Here is a TikZ solution:

\documentclass{article}
\usepackage{amsmath,showframe}
\usepackage{mathtools}  % for mathmakebox
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
% Having all the cells to be equal in width is taken from Gonzalo Medina:
% https://tex.stackexchange.com/a/191240/167081

% The next is used to make the arrow fit the longest formula width.
% Copied from: Stefan Kottwitz, https://tex.stackexchange.com/a/6840/167081
\newlength{\arrow}
\settowidth{\arrow}{\tiny$R_4-(\frac{-4}{3})R_3$}% Longest formula
\newcommand*{\matrixArrow}[1]{\xrightarrow{\mathmakebox[\arrow]{#1}}}

\newcommand\mymatrix[3]{%
\noindent
\begin{tikzpicture}
\matrix[ampersand replacement=\&,matrix of math nodes,left delimiter = {[},
right delimiter ={]}, inner xsep=0pt,inner ysep=3pt, outer xsep=0pt,
align = center,font=\footnotesize,
every node/.style={anchor=base,text depth=.5ex,text height=2ex,text width=2.6em},
nodes={execute at begin node=$, execute at end node=$}] (m){#1};
\node[right=0.5ex of m] (b) {$\matrixArrow{\textcolor{red}{#2}#3}$};
\end{tikzpicture}
}

\begin{document}
% How to use:
% Place the matrix (delimited by \&) in the first argument.
% Place the row that will be changed in the second (to be shown in red).
% Place rest of the formula of the row reduction in the 3rd and last arugment.
% Row 1
\mymatrix{
1   \& 1 \& 1   \& 6z - 2   \\
-1  \& 2 \& 0   \& 4z - 5   \\
1   \& 0 \& -1  \& z - 3    \\
1   \& 2 \& 0   \& 8z - 7   \\
}{R_2}{- (-1)R_1}
\mymatrix{
1 \& 1 \& 1     \& 6z - 2   \\
0 \& 3 \& 1     \& 10z - 7  \\
1 \& 0 \& -1    \& z - 3    \\
1 \& 2 \& 0     \& 8z - 7   \\
}{R_3}{- 1R_1}\$2ex] % Row 2 \mymatrix{ 1 \& 1 \& 1 \& 6z - 2 \\ 0 \& 3 \& 1 \& 10z - 7 \\ 0 \& -1 \& -2 \& -5z - 1 \\ 1 \& 2 \& 0 \& 8z - 7 \\ }{R_4}{- R_1} \mymatrix{ 1 \& 1 \& 1 \& 6z - 2 \\ 0 \& 3 \& 1 \& 10z - 7 \\ 0 \& -1 \& -2 \& -5z - 1 \\ 0 \& 1 \& -1 \& 2z - 5 \\ }{R_2}{/3}\\[2ex] % Row 3 \mymatrix{ 1 \& 1 \& 1 \& 6z - 2 \\ 0 \& 1 \& \frac{1}{3} \& \frac{10z-7}{3} \\ 0 \& -1 \& -2 \& -5z - 1 \\ 0 \& 1 \& -1 \& 2z - 5 \\ }{R_3}{-(-1)R_2} \mymatrix{ 1 \& 1 \& 1 \& 6z - 2 \\ 0 \& 1 \& \frac{1}{3} \& \frac{10z-7}{3} \\ 0 \& 0 \& \frac{-5}{3} \& \frac{-5z-10}{3} \\ 0 \& 1 \& -1 \& 2z - 5 \\ }{R_4}{-1R_2}\\[2ex] % Row 4 \mymatrix{ 1 \& 1 \& 1 \& 6z-2 \\ 0 \& 1 \& \frac{1}{3} \& \frac{10z-7}{3} \\ 0 \& 0 \& \frac{-5}{3} \& \frac{-5z-10}{3} \\ 0 \& 0 \& \frac{-4}{3} \& \frac{-4z-8}{3} \\ }{R_3}{/(\frac{-5}{3})} \mymatrix{ 1 \& 1 \& 1 \& 6z - 2 \\ 0 \& 1 \& \frac{1}{3} \& \frac{10z-7}{3} \\ 0 \& 0 \& \frac{-5}{3} \& \frac{-5z-10}{3} \\ 0 \& 1 \& -1 \& 2z - 5 \\ }{R_4}{-(\frac{-4}{3})R_3}\\[2ex] % Row 5 \mymatrix{ 1 \& 1 \& 1 \& 6z - 2 \\ 0 \& 1 \& \frac{1}{3} \& \frac{10z-7}{3} \\ 0 \& 0 \& 1 \& z + 2 \\ 0 \& 0 \& 0 \& 0 \\ }{R_1}{-1R_3} \mymatrix{ 1 \& 1 \& 0 \& 5z - 4 \\ 0 \& 1 \& 0 \& 3z - 3 \\ 0 \& 0 \& 1 \& z + 2 \\ 0 \& 0 \& 0 \& 0 \\ }{R_1}{-1R_2}\\[2ex] % Row 6 One matrix only \[ \begin{matrix} x_1 & = & 2z-1 \\ x_2 & = & 3z-3 \\ x_3 & = & z+2 \end{matrix}$
\end{document}

• This is insane! Thank you! Really what I was looking for! – jubibanna May 16 at 3:32