我正在尝试获取图片上的内容:
我的示例看起来比图片中的更大,后者看起来更紧凑、更美观。此外,图片中的矩阵看起来更清晰,也就是说,它们具有粗体效果或类似效果。
\documentclass{article}
\usepackage{geometry}
\usepackage{mathtools, nccmath}
\usepackage{nicematrix}
\usetikzlibrary{fit,
shapes.geometric}
\tikzset{highlight/.style={ellipse, draw=red, semithick, inner sep=1pt}}
\usepackage{lipsum}
\begin{document}
\begin{equation*}\medmath{
\begin{NiceArray}{|C|C|C|C|C|C|C|C|C|C|C|C|}%
\hline
1& & 3 & & & 5 & & & 5 & & 4 & \\ \hline
& & 5 & 4 & & & 4& & & 2 & 1 & 3 \\ \hline
2 & 4 & & 1 & 2 & & 3 & & 4 & 3 & 5 & \\ \hline
& 2& 4 & & 5 & & & 4 & & & 2& \\ \hline
& & 4 & 3 & 4 & 2 & & & & & 2 &5 \\ \hline
1 & & 3 & & 3 & & & 2& & & 4& \\
\hline
\end{NiceArray}
\quad=\quad
\begin{NiceArray}{|C|C|C|}%
\hline
& & \\ \hline
& & \\ \hline
& & \\ \hline
& & \\ \hline
& & \\ \hline
& & \\
\hline
\end{NiceArray}
\quad\times\quad
\begin{NiceArray}{|C|C|C|C|C|C|C|C|C|C|C|C|}%
\hline
& & & & & & & & & & & \\ \hline
& & & & & & & & & & & \\ \hline
& & & & & & & & & & & \\ \hline
\end{NiceArray}
}
\medskip
\end{equation*}
\end{document}
答案1
只是为了完整性:即使矩阵有空节点,也允许某种样式轻松绘制垂直线和水平线。这些附加功能不是回答问题所必需的,但很容易添加到上述代码中。但是,每列和每行必须至少有一个节点。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix,calc,positioning,fit,backgrounds}
\makeatletter
\long\def\ifnodedefined#1#2#3{%
\@ifundefined{pgf@sh@ns@#1}{#3}{#2}%
}
\tikzset{matrix vlines/.style={execute at end matrix={
\edef\tikzmatrixname{\tikz@fig@name}
\foreach \XX in {1,...,\the\pgf@matrix@numberofcolumns}
{\xdef\FitList{}
\foreach \YY in {1,...,\the\pgfmatrixcurrentrow}
{\ifnodedefined{\tikzmatrixname-\YY-\XX}{\xdef\FitList{\FitList (\tikzmatrixname-\YY-\XX)}}{}
}
\node[fit=\FitList,draw=none,fill=none,inner sep=0pt,draw=none] (\tikzmatrixname-col-\XX) {};
}
\foreach \XX in {2,...,\the\pgf@matrix@numberofcolumns}
{\draw[#1] ($(\tikzmatrixname-col-\XX.west)!0.5!(\tikzmatrixname-col-\the\numexpr\XX-1\relax.east)$)
coordinate (aux) (aux|-\tikzmatrixname.north)
-- (aux|-\tikzmatrixname.south);
}
}},matrix hlines/.style={execute at end matrix={
\edef\tikzmatrixname{\tikz@fig@name}
\foreach \YY in {1,...,\the\pgfmatrixcurrentrow}
{\xdef\FitList{}
\foreach \XX in {1,...,\the\pgf@matrix@numberofcolumns}
{\ifnodedefined{\tikzmatrixname-\YY-\XX}{\xdef\FitList{\FitList (\tikzmatrixname-\YY-\XX)}}{}
}
\node[fit=\FitList,draw=none,fill=none,inner sep=0pt,draw=none] (\tikzmatrixname-row-\YY) {};
}
\foreach \XX in {2,...,\the\pgfmatrixcurrentrow}
{\draw[#1] ($(\tikzmatrixname-row-\XX)!0.5!(\tikzmatrixname-row-\the\numexpr\XX-1\relax)$)
coordinate (aux) (aux-|\tikzmatrixname.west)
-- (aux-|\tikzmatrixname.east);
}
}},
matrix dividers/.style={matrix vlines=#1,matrix hlines=#1},
matrix frame/.style={execute at end matrix={
\draw[#1] (\tikz@[email protected] west) rectangle (\tikz@[email protected] east);
}}}
\makeatother
\begin{document}
\begin{tikzpicture}
\matrix[matrix vlines={blue},matrix of math nodes] (mat1) {
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& \\
};
\matrix[matrix hlines={red},below=2em of mat1,matrix of math nodes] (mat2) {
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& \\
};
\matrix[matrix dividers={thin,dashed},below=2em of mat2,matrix of math nodes] (mat3) {
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& \\
};
\begin{scope}[on background layer,fill opacity=0.3]
\fill[red] (mat3-col-2.west|-mat3.north) rectangle (mat3-col-2.east|-mat3.south);
\fill[orange] (mat3-row-2.north-|mat3.west) rectangle (mat3-row-2.south-|mat3.east);
\end{scope}
\end{tikzpicture}
\end{document}
答案2
这个答案带有一种新风格matrix dividers
,可以传递给 Ti钾Z 矩阵。它将像表格一样绘制分界线。此样式利用了execute at end matrix
尚未进入手册的键。它还会找出行数和列数,请参阅这个答案。请注意,此样式不适用于具有空单元格或非常不均匀单元格的矩阵,因为这些单元格在这里不会出现。但是,也可以(并且不太困难)将此样式扩展到这种情况。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix,calc,positioning}
\definecolor{dgreen}{RGB}{72,127,30}
\makeatletter
\tikzset{matrix dividers/.style={execute at end matrix={
\foreach \XX in {2,...,\the\pgf@matrix@numberofcolumns}
{\draw[#1] ($(\tikz@fig@name-1-\XX)!0.5!(\tikz@fig@name-1-\the\numexpr\XX-1)$)
coordinate (aux) (aux|-\tikz@[email protected])
-- (aux|-\tikz@[email protected]);
}
\foreach \XX in {2,...,\the\pgfmatrixcurrentrow}
{\draw[#1] ($(\tikz@fig@name-\XX-1)!0.5!(\tikz@fig@name-\the\numexpr\XX-1\relax-1)$)
coordinate (aux) (aux-|\tikz@[email protected])
-- (aux-|\tikz@[email protected]);
}
}},matrix frame/.style={execute at end matrix={
\draw[#1] (\tikz@[email protected] west) rectangle (\tikz@[email protected] east);
}}}
% from https://tex.stackexchange.com/a/386805/121799
\def\tikz@lib@matrix@empty@cell{\iftikz@lib@matrix@empty\node[name=\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn,empty node]{};\fi}
\makeatother
\tikzset{matrix of mathsf nodes/.style={%
matrix of nodes,
nodes={%
execute at begin node=$\mathsf\bgroup,%
execute at end node=\egroup$%
}%
}}
\begin{document}
\begin{tikzpicture}[nmat/.style={matrix of mathsf nodes,inner sep=0pt,nodes in empty cells,column sep=-\pgflinewidth,
row sep=-\pgflinewidth,nodes={text height=1.7ex,text depth=0.2ex,inner
sep=2pt,minimum width=1.8ex},matrix dividers={thin},matrix
frame={thick}},font=\sffamily,
empty node/.style={fill=none}]
\matrix[nmat,nodes={fill=yellow}] (mat1) {
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[below=0.2ex of mat1,scale=2.5,dgreen] {$\mathsf{R}$};
\node[rotate=90,left=1.3ex of mat1,xshift=3em,dgreen] {items};
\node[above=0ex of mat1,xshift=1em,dgreen] {users};
\node[right=0.1ex of mat1,scale=2] (approx) {$\approx$};
\matrix[nmat,right=0.1ex of approx] (mat2) {
& & \\
& & \\
& & \\
& & \\
& & \\
& & \\
};
\node[rotate=90,left=1.3ex of mat2,xshift=-1em,dgreen] {items};
\node[above=0ex of mat2,dgreen] {factors};
\node[below=0.2ex of mat2,scale=2.5,dgreen] {$\mathsf{Q}$};
\node[right=0.1ex of mat2,scale=2] (cdot) {$\times$};
\matrix[nmat,right=0.1ex of cdot] (mat3) {
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
};
\node[rotate=-90,right=1.5ex of mat3,xshift=-2em,dgreen] {factors};
\node[above=0ex of mat3,dgreen] {users};
\node[below=0.2ex of mat3,scale=2.5,dgreen] {$\mathsf{P^T}$};
\end{tikzpicture}
\end{document}
更新:添加了非常好的\tikz@lib@matrix@empty@cell
技巧这个答案。
答案3
对我来说,绘制方程式比用标准数学工具写方程式更容易,因为你的要求相当苛刻。基于我的答案并考虑这个答案它的代码可以是:
\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]
{
& & \\
& & \\
& & \\
& & \\
& & \\
& & \\
};
\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}
附录
如果您希望矩阵的标签“项目”位于其左下角,则将其替换label={[rotate=90, anchor=south]left: items},
为label={[rotate=90, anchor=south east]left: items},
并将标签样式更改为every label/.append style = {inner xsep=1em, font=\large, text=teal}
。对于较大的数学运算符号,请将其添加font=\larger
到其节点选项中。例如\node (times) [right=of m2,font=\large] {$\times$};