# Aligning matrices

Sorry if this might be a very banal question. I am fairly new to LaTeX and I am having issues with matrices alignment. I am trying to achieve pretty much what is shown in this picture, I have tried with the align environment, but I found that it doesn't quite do what I'm after since I would like all columns of Z, V and R to be aligned, as well as all rows of Z, Y and X.

Is there a way to do it efficiently?

Of course, ideally the parenthesis around Z, Y and X would span all 5 lines as in regular matrix notation.

• If something needs to be aligned in TeX, nothing is a banal question. – percusse Nov 18 '15 at 9:31
• Welcome! do you need the A, B, C, D, E in the output? – cmhughes Nov 18 '15 at 9:50
• Thanks for al the replies, the letters are needed in the output, but I'm positive that now that I have an idea of how to do the alignment, I can put them there myself – ltr Nov 19 '15 at 10:57

One way to achieve your layout objective is to use pmatrix* environments (in order to right-align all columns), \hphantom statements (to widen various columns as needed), and \mkern directives (to align the left-hand most columns of the Z, V, and R matrices/row vectors).

If you're not comfortable with \hphantom directives inside pmatrix* environments, you could load the siunitx package to define columns of integer values with widths corresponding to 2, 3, and 4 digits. In the example below, I suggest using \mleft( and \mright) instead of the usual \left( and \right) in order to avoid excessive spacing around the matrices. Note that the \mkern7.5mu directives continue to be employed.

\documentclass{article}
\usepackage{mathtools,siunitx,array,mleftright}
\newcolumntype{i}[1]{S[table-format=#1]} % columns of right-aligned integers
\begin{document}
First method:
\begin{align*}
Z &= \begin{pmatrix*}[r]
\hphantom{000}0 & 70 & 0 & 30 & \hphantom{000}0 \\
0 & 0 & 120 & 0 & 0 \\
0 & 0 & 0 & 40 & 0  \\
0 & 0 & 0 & 0 & 70  \\
0 & 50 & 0 & 0 & 0
Y = \begin{pmatrix*}[r] 0 \\ 0\\ 80 \\ 0 \\ 20 \end{pmatrix*} \\
V &= \mkern7.5mu \begin{pmatrix*}[r]
\hphantom{0}100 & \phantom{0}0 & \phantom{00}0 & \phantom{0}0 & \hphantom{000}0
\end{pmatrix*} \\
R &= \mkern7.5mu\begin{pmatrix*}[r]
5000 & \hphantom{0}0 & \hphantom{00}0 & \hphantom{0}0 & 2000
\end{pmatrix*}
\end{align*}

\bigskip
Second method:
\begin{align*}
Z &= \mleft( \begin{array}{@{}i{4}i{2}i{3}i{2}i{4}@{}}
0 & 70 & 0 & 30 & 0 \\
0 & 0 & 120 & 0 & 0 \\
0 & 0 & 0 & 40 & 0  \\
0 & 0 & 0 & 0 & 70  \\
0 & 50 & 0 & 0 & 0
Y = \begin{pmatrix*}[r] 0 \\ 0\\ 80 \\ 0 \\ 20 \end{pmatrix*} \\
V &= \mkern7.5mu
\mleft( \begin{array}{@{}i{4}i{2}i{3}i{2}i{4}@{}}
100 & 0 & 0 & 0 & 0
\end{array} \mright) \\
R &= \mkern7.5mu
\mleft( \begin{array}{@{}i{4}i{2}i{3}i{2}i{4}@{}}
5000 & 0 & 0 & 0 & 2000
\end{array} \mright)
\end{align*}
\end{document}


You can use the matrix library of TikZ together with the positioning library to achieve something like this:

## Code

\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{matrix, positioning}

\begin{document}

%\matrix (m) [matrix of math nodes,left delimiter=(,right delimiter=),
%  inner sep=2pt,outer sep=0pt]{
%  a & b & [1em] 0 \\
%  c & d & 1 \\ };

\begin{tikzpicture}
[   on grid,
inner sep=2pt,
outer sep=0pt,
minimum width=9mm,
every left delimiter/.style={xshift=3.5ex},
every right delimiter/.style={xshift=-3.5ex}
]

\matrix (R) [matrix of math nodes]
{   A & B & C & D & E \\ };

\matrix (MZ) [matrix of math nodes, left delimiter=(, right delimiter=), below=1.5cm of R]
{ 400 & 0   & 0   & 0   & 0   \\
0   & 0   & 0   & 0   & 700 \\
0   & 0   & 200 & 0   & 0   \\
0   & 300 & 0   & 0   & 0   \\
0   & 0   & 0   & 500 & 0   \\
};

\node[left=2.8cm of MZ] (Z) {$Z=$};

\matrix (C) [matrix of math nodes, left=1.2cm of Z]
{   A \\ B \\ C \\ D \\ E \\ };

\node[right=3.3cm of MZ] (Y) {$Y=$};

\matrix (MY) [matrix of math nodes, left delimiter=(, right delimiter=), right=0.9cm of Y]
{   0 \\ 30 \\ 80 \\ 0 \\ 0 \\ };

\node[right=1.5cm of MY] (X) {$X=$};

\matrix (MX) [matrix of math nodes, left delimiter=(, right delimiter=), right=0.9cm of X]
{   70 \\ 0 \\ 10 \\ 90 \\ 0 \\ };

\matrix (MV) [matrix of math nodes, left delimiter=(, right delimiter=), below=2cm of MZ]
{   400 & 0 & 0 & 2000 & 0 \\ };

\node[left=2.8cm of MV] (V) {$V=$};

\matrix (MR) [matrix of math nodes, left delimiter=(, right delimiter=), below=1cm of MV]
{   4000 & 0 & 0 & 2000 & 7000 \\ };

\node[left=2.8cm of MR] (R) {$R=$};

\end{tikzpicture}

\end{document}


## Output

Since the input in the OP's question has the appearance of a spreadsheet, it is possible that the OP would prefer a tabular solution, rather than one set in mathematical notation. The only twist for adapting it was the introduction of \mybig{rows}{glyph} for making large parens.

Currently, column alignments are centered, but that is easily customized. EDITED to incorporate \arraystretch.

\documentclass{article}
\usepackage{scalerel}
\def\myvshft{\dimexpr+.5\ht\strutbox-.5\dp\strutbox}
\newcommand\mybig[2]{\smash{\raisebox{\myvshft}{\scaleto[2ex]{\raisebox{%
-\myvshft}{#2}}{\arraystretch\dimexpr#1\normalbaselineskip}}}}
\begin{document}
\renewcommand\arraystretch{1.3}
\tabcolsep3pt
\begin{tabular}{cccccccccccccccccccc}
&&&&A&B&C&D&E\\
\\
A&&&&0&70&0&30&0&&&&&0&&&&&0\\
B&&&&0&0&120&0&0&&&&&0&&&&&0\\
C&&Z =&\mybig{5}{(}&0&0&0&40&0&\mybig{5}{)}&&
Y =&\mybig{5}{(}&0&\mybig{5}{)}&&X =&\mybig{5}{(}&0&\mybig{5}{)}\\
D&&&&0&0&0&0&70&&&&&0&&&&&0\\
E&&&&0&50&0&0&0&&&&&20&&&&&20\\
\\
&&V =&(&100&0&0&0&0&)\\
\\
&&R =&(&5000&0&0&0&2000&)\\
\end{tabular}
\end{document}