矩阵上的数字看起来不太好

矩阵上的数字看起来不太好

我正在尝试了解这张图片上的内容:

在此处输入图片描述

问题是,当我添加数字时矩阵,它们看起来与原始图片中的不一样。

\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}

在此处输入图片描述

我也有尺寸问题:

在此处输入图片描述

答案1

这是因为节点的宽度是硬编码的。如果您想用此代码修复此问题,则需要增加这些宽度。

\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}

在此处输入图片描述

如果您不想将第二个矩阵中的单元格填充为黄色,nodes={fill=yellow}则仅添加到第一个矩阵

\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}

反而。

在此处输入图片描述

相关内容