# Numbers on matrix do not look good

I am trying to get what's on this picture:

The problem is that when I add numbers the Q matrix, they don't look the same as in the original picture.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,
positioning}
\tikzset{
mymatrix/.style = {matrix of nodes,
nodes in empty cells,
nodes={minimum height=3ex, minimum width=1em,
inner sep=0pt, outer sep=0pt, anchor=center,
draw, very thin, fill=yellow},
column sep=0pt,
row sep=0pt,
inner sep=0.5\pgflinewidth, outer sep=1pt,
draw, thick},
empty node/.style = {draw,fill=none},
every label/.append style = {font=\large, text=teal}
}
\makeatletter
\def\tikz@lib@matrix@empty@cell{%
\iftikz@lib@matrix@empty%
\node[name=\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn,empty node]{};\fi}
\makeatother

\begin{document}
$\begin{tikzpicture}[node distance=0pt] \matrix (m1) [mymatrix, label={[rotate=90, anchor=south]left: items}, label=below:R, label=above:users] { 1 & & 3 & & & 5 & & & 5 & & 4 & \\ & & 5 & 4 & & & 4 & & & 2 & 1 & 3 \\ 2 & 4 & & 1 & 2 & & 3 & & 4 & 3 & 5 & \\ & 2 & 4 & & 5 & & & 4 & & & 2 & \\ & & 4 & 3 & 4 & 2 & & & & & 2 & 5 \\ 1 & & 3 & & 3 & & & 2 & & & 4 & \\ }; \node (approx) [right=of m1] {\approx}; \matrix (m2) [mymatrix=none, right=1em of approx, label={[rotate=90, anchor=south]left: items}, label=below:Q, label=above:factors] { 0.1& -0.4 & -0.2 \\ -0.5& 0.6 & 0.5 \\ -0.2& 0.3 &0.5 \\ 1.1& 2.1 & 0.3 \\ -0.7&2.1 & -2 \\ -1& 0.7 & 0.3 \\ }; \node (times) [right=of m2] {\times}; \matrix (m3) [mymatrix=none, right=of times, label={[rotate=90, anchor=north]right: factors}, label=below:P^T, label=above:users] { 1.1&-2 & 0.3 & 0.5 &-2 & -0.5 & 0.8 & -0.4 & 0.3 & 1.4 & 2.4 & -0.9\\ -0.8& 0.7 & 0.5 & 1.4 &0.3 & -1 & 1.4 & 2.9 & 0.7 & - 1.2 & & 1.3\\ 2.1& -0.4 & 0.6 & 1.7 & 2.4 & .9 & -0.3 & 0.4 & 0.8 & 0.7 & -0.6&0.1 \\ }; \end{tikzpicture}$
\end{document}


I have problems with the size, also:

This is because the widths of the nodes were hardcoded. If you want to fix this with this code, you need to increase these widths.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,
positioning}
\tikzset{
mymatrix/.style = {matrix of nodes,
nodes in empty cells,
nodes={minimum height=3ex, minimum width=1em,
inner sep=0pt, outer sep=0pt, anchor=center,
draw, very thin, fill=yellow},
column sep=0pt,
row sep=0pt,
inner sep=0.5\pgflinewidth, outer sep=1pt,
draw, thick},
empty node/.style = {draw,fill=none},
every label/.append style = {font=\large, text=teal}
}
\makeatletter
\def\tikz@lib@matrix@empty@cell{%
\iftikz@lib@matrix@empty%
\node[name=\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn,empty node]{};\fi}
\makeatother

\begin{document}
$\begin{tikzpicture}[node distance=0pt] \matrix (m1) [mymatrix, label={[rotate=90, anchor=south]left: items}, label=below:R, label=above:users] { 1 & & 3 & & & 5 & & & 5 & & 4 & \\ & & 5 & 4 & & & 4 & & & 2 & 1 & 3 \\ 2 & 4 & & 1 & 2 & & 3 & & 4 & 3 & 5 & \\ & 2 & 4 & & 5 & & & 4 & & & 2 & \\ & & 4 & 3 & 4 & 2 & & & & & 2 & 5 \\ 1 & & 3 & & 3 & & & 2 & & & 4 & \\ }; \node (approx) [right=of m1] {\approx}; \matrix (m2) [mymatrix=none, right=1em of approx, label={[rotate=90, anchor=south]left: items}, label=below:Q, label=above:factors, nodes={minimum width=2em}] { 0.1& -0.4 & -0.2 \\ -0.5& 0.6 & 0.5 \\ -0.2& 0.3 &0.5 \\ 1.1& 2.1 & 0.3 \\ -0.7&2.1 & -2 \\ -1& 0.7 & 0.3 \\ }; \node (times) [right=of m2] {\times}; \matrix (m3) [mymatrix=none, right=of times, label={[rotate=90, anchor=north]right: factors}, label=below:P^T, label=above:users] { & & & & & & & & & & & \\ & & & & & & & & & & & \\ & & & & & & & & & & & \\ }; \end{tikzpicture}$
\end{document}


If you do not want to fill the cells in the second matrix yellow, add nodes={fill=yellow} only to the first matrix

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,
positioning}
\tikzset{
mymatrix/.style = {matrix of math nodes,
nodes in empty cells,
nodes={minimum height=3ex, minimum width=1em,
inner sep=0pt, outer sep=0pt, anchor=center,
draw, very thin,scale=0.9,transform shape},
column sep=0pt,
row sep=0pt,
inner sep=0.5\pgflinewidth, outer sep=1pt,
draw, thick},
empty node/.style = {draw,fill=none},
every label/.append style = {font=\large, text=teal}
}
\makeatletter
\def\tikz@lib@matrix@empty@cell{%
\iftikz@lib@matrix@empty%
\node[name=\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn,empty node]{};\fi}
\makeatother

\begin{document}
$\begin{tikzpicture}[node distance=0pt] \matrix (m1) [mymatrix, label={[rotate=90, anchor=south]left: items}, label=below:R, label=above:users, nodes={fill=yellow}] { 1 & & 3 & & & 5 & & & 5 & & 4 & \\ & & 5 & 4 & & & 4 & & & 2 & 1 & 3 \\ 2 & 4 & & 1 & 2 & & 3 & & 4 & 3 & 5 & \\ & 2 & 4 & & 5 & & & 4 & & & 2 & \\ & & 4 & 3 & 4 & 2 & & & & & 2 & 5 \\ 1 & & 3 & & 3 & & & 2 & & & 4 & \\ }; \node (approx) [right=of m1] {\approx}; \matrix (m2) [mymatrix=none, right=0.1em of approx, label={[rotate=90, anchor=south east,xshift=-0.7em]left: items}, label=below:Q, label=above:factors, nodes={minimum width=2.4em}] { 0.1& -0.4 & -0.2 \\ -0.5& 0.6 & 0.5 \\ -0.2& 0.3 &0.5 \\ 1.1& 2.1 & 0.3 \\ -0.7&2.1 & -2 \\ -1& 0.7 & 0.3 \\ }; \node (times) [right=of m2] {\times}; \matrix (m3) [mymatrix=none, right=of times, label={[rotate=90, anchor=north]right: factors}, label=below:P^T, label=above:users] { & & & & & & & & & & & \\ & & & & & & & & & & & \\ & & & & & & & & & & & \\ }; \end{tikzpicture}$
\end{document}


• @naveganTeX This is written in the lower part of the answer. You need to add nodes={fill=none} or add fill=yellow only to the first matrix. – user121799 Jun 27 '19 at 5:42
• @egreg For that one only needs to replace matrix of nodes by matrix of math nodes. – user121799 Jun 27 '19 at 14:41