# Align matrices problem

I have an equation of matrices and I want to align the dimensions of the matrices below the equation of matrices. However, I produce something very ugly. Can someone advise how to make it look better or properly aligned ?

\documentclass[10pt,a4paper]{article}
\usepackage{amsmath, amsfonts}
\begin{document}
\begin{align*}
A
&
\begin{bmatrix}
&&\\
\vec{v}_1 & \cdots & \vec{v}_r \\
&&
\end{bmatrix}
&= &
\begin{bmatrix}
&&\\
\vec{u}_1 & \cdots & \vec{u}_r\\
&&
\end{bmatrix}
&
\begin{bmatrix}
\sigma_1 &&\\
&\ddots&\\
&& \sigma_r
\end{bmatrix}
\\
(m \times n) & (n \times r) & & (m \times r) & (r \times r)
\end{align*}
\end{document}

• What is the message you want to pass to? The dimension compability or the vector structure? Jan 21, 2014 at 16:17

A possible solution using tabular:

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\setlength{\tabcolsep}{0pt}
\begin{tabular}{*{5}{c}}
$A$
& $\begin{bmatrix} & & \\ \vec{v}_1 & \cdots & \vec{v}_r \\ & & \end{bmatrix}$
& $=$
& $\begin{bmatrix} & & \\ \vec{v}_1 & \cdots & \vec{v}_r \\ & & \end{bmatrix}$
& $\begin{bmatrix} \sigma_1 & & \\ & \ddots & \\ & & \sigma_r \end{bmatrix}$\\
& $(m \times n)(n \times r)$
&
& $(m \times r)$
& $(r \times r)$
\end{tabular}

\end{document}


Update

If you want to color an entire row or individual cells, you can do the following (see also Highlighting in tabular environment):

\documentclass{article}

\usepackage{amsmath}
\usepackage{color,colortbl}

\definecolor{ColorA}{rgb}{0.25,0.5,0.75}

\begin{document}

\setlength{\tabcolsep}{0pt}
\begin{tabular}{*{5}{c}}
\cellcolor{ColorA}\,$A$
& \cellcolor{ColorA}$\begin{bmatrix} & & \\ \vec{v}_1 & \cdots & \vec{v}_r \\ & & \end{bmatrix}$
& \cellcolor{ColorA}$=$
& \cellcolor{ColorA}$\begin{bmatrix} & & \\ \vec{v}_1 & \cdots & \vec{v}_r \\ & & \end{bmatrix}$
& \cellcolor{ColorA}$\begin{bmatrix} \sigma_1 & & \\ & \ddots & \\ & & \sigma_r \end{bmatrix}$\\
& $(m \times n)(n \times r)$
&
& $(m \times r)$
& $(r \times r)$
\end{tabular}

\end{document}


\documentclass{article}

\usepackage{amsmath}
\usepackage{color,colortbl}

\definecolor{ColorA}{rgb}{0,1,1}
\definecolor{ColorB}{rgb}{1,0,1}
\definecolor{ColorC}{rgb}{1,1,0}
\definecolor{ColorD}{rgb}{0.25,0.5,0.75}

\begin{document}

\setlength{\tabcolsep}{0pt}
\begin{tabular}{*{5}{c}}
\cellcolor{ColorA}\,$A$\,
& \cellcolor{ColorB}$\begin{bmatrix} & & \\ \vec{v}_1 & \cdots & \vec{v}_r \\ & & \end{bmatrix}$
& \,$=$\,
& \cellcolor{ColorC}$\begin{bmatrix} & & \\ \vec{v}_1 & \cdots & \vec{v}_r \\ & & \end{bmatrix}$
& \cellcolor{ColorD}$\begin{bmatrix} \sigma_1 & & \\ & \ddots & \\ & & \sigma_r \end{bmatrix}$\\
& $(m \times n)(n \times r)$
&
& \,$(m \times r)$
& $(r \times r)$
\end{tabular}

\end{document}


• Hi, one more question, How can I highlight the entire line of matrices using your code ? Jan 21, 2014 at 16:41
• @asd See updated answer. Jan 21, 2014 at 16:58

You can use subscripts, but in this particular case, this approach doesn't look completely fine. A better option could be to take a more "wordy" approach. The following example shows both possibilities:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

$A_{m\times n} \begin{bmatrix} && \\ \vec{v}_1 & \cdots & \vec{v}_r \\ && \end{bmatrix}_{n\times r} = \begin{bmatrix} && \\ \vec{u}_1 & \cdots & \vec{u}_r\\ && \end{bmatrix}_{m\times r} \begin{bmatrix} \sigma_1 &&\\ &\ddots&\\ && \sigma_r \end{bmatrix}_{r\times r}$

So we have
$AV=U\Sigma,$
where $A$ is an $m\times n$ matrix and $V$, $U$ and $\Sigma$ are the $n\times r$, $m\times r$ and $r\times r$, respectively, matrices given by
$V= \begin{bmatrix} && \\ \vec{v}_1 & \cdots & \vec{v}_r \\ && \end{bmatrix},\quad U= \begin{bmatrix} && \\ \vec{u}_1 & \cdots & \vec{u}_r\\ && \end{bmatrix}\quad\text{and}\quad \Sigma= \begin{bmatrix} \sigma_1 &&\\ &\ddots&\\ && \sigma_r \end{bmatrix}.$

\end{document}


An attempt with \underset:

\documentclass[10pt,a4paper]{article}
\usepackage{amsmath, amsfonts}
\begin{document}
\begin{align*}
A \underset{\textstyle (m \times n) \, (n \times r)}{\begin{bmatrix}
&&\\
\vec{v}_1 & \cdots & \vec{v}_r \\
&&
\end{bmatrix}}
&=
\underset{\textstyle (m \times r)}{\begin{bmatrix}
&&\\
\vec{u}_1 & \cdots & \vec{u}_r\\
&&
\end{bmatrix}}
\underset{\textstyle (r \times r)}{\begin{bmatrix}
\sigma_1 &&\\
&\ddots&\\
&& \sigma_r
\end{bmatrix}}
\end{align*}
\end{document}


• This is probably the best solution if no coloring is needed. Jan 21, 2014 at 18:08

Here is a solution with nicematrix.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\setlength{\arraycolsep}{8pt}
\NiceMatrixOptions{nullify-dots, last-row, code-for-last-row = \color{black}}

$\color{blue} A \begin{bNiceMatrix} \\ \vec{v}_1 & \Cdots & \vec{v}_r \\ \\ \Block{1-3}{(m \times n)(n \times r)} \end{bNiceMatrix} = \begin{bNiceMatrix} \\ \vec{v}_1 & \Cdots & \vec{v}_r \\ \\ \Block{1-3}{(m \times r)} \end{bNiceMatrix} \begin{bNiceMatrix} \sigma_1 \\ & \Ddots \\ & & \sigma_r \\ \Block{1-3}{(r \times r)} \end{bNiceMatrix}$

\end{document}


You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

If you wish, you can add a background under the matrices with Tikz (by using the PGF/Tikz nodes created by nicematrix under the row, columns and cells of the matrices).

\documentclass{article}
\usepackage{nicematrix,tikz}

\begin{document}

\setlength{\arraycolsep}{8pt}
\NiceMatrixOptions{nullify-dots, last-row}

$A \begin{bNiceMatrix}[name=A] \\ \vec{v}_1 & \Cdots & \vec{v}_r \\ \\ \Block{1-3}{(m \times n)(n \times r)} \end{bNiceMatrix} = \begin{bNiceMatrix} \\ \vec{v}_1 & \Cdots & \vec{v}_r \\ \\ \Block{1-3}{(m \times r)} \end{bNiceMatrix} \begin{bNiceMatrix}[name=B] \sigma_1 \\ & \Ddots \\ & & \sigma_r \\ \Block{1-3}{(r \times r)} \end{bNiceMatrix} \tikz [overlay, remember picture] \fill [red!15,blend mode = multiply] (A-1-|A-1) ++ (-6pt,0pt) rectangle (B-4-|B-4) ;$

\end{document}