![带阴影的矩阵](https://linux22.com/image/398356/%E5%B8%A6%E9%98%B4%E5%BD%B1%E7%9A%84%E7%9F%A9%E9%98%B5.png)
我正在尝试绘制阴影以及两个矩阵之间的点:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\centering
\begin{tabular}{|c|c|c|c|c|c|c|c|}
\hline
5 & 1 & 3& &2 & 1 & & \\ \hline
4 & 2 & & 3& & 4 & 3 & 1 \\ \hline
1 & 3 & & & & 2 & 5 & \\ \hline
2 & 5 & 1& 2 & & & & 1 \\ \hline
0 & 4 & & & 5 &3 &1 & \\ \hline
& 2 & & 4& &4 & 1& 2 \\ \hline
\hline
\end{tabular}
\begin{tabular}{|c|c|c|c|c|c|c|c|}
\hline
1 & 1 & 1& &1 & 1 & & \\ \hline
0 & 1 & & & & 0 & 0 & 1 \\ \hline
0 & & & & & 0 & 0 & \\ \hline
1 & & 1& 1 & & & & 1 \\ \hline
0 & 1 & & & 1 &1 &1 & \\ \hline
& 1 & & & & & & \\ \hline
\hline
\end{tabular}
\end{document}
答案1
shadows
阴影可以由(a)土拨鼠、(b)和投射shadows.blur
。我在这里使用shadows.blur
,因为你似乎没有要求鸭形阴影。分割线和框架的样式来自这里。我在更新中将单元格变成了二次方。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix,calc,positioning,fit,backgrounds,shadows.blur}
\makeatletter
\long\def\ifnodedefined#1#2#3{%
\@ifundefined{pgf@sh@ns@#1}{#3}{#2}%
}
\tikzset{matrix vlines/.style={execute at end matrix={
\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={
\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}[font=\sffamily\itshape,node distance=0.3em,
s-matrix/.style={matrix dividers,matrix frame,matrix of math nodes,
nodes in empty cells,fill=white,blur shadow,inner sep=0pt,
cells={text height=0.8em,text depth=0.2em,minimum width=1.8em,minimum height=1.8em}}]
\matrix[s-matrix] (mat1) {
5 & 1 & 3& &2 & 1 & & \\
4 & 2 & & 3& & 4 & 3 & 1 \\
1 & 3 & & & & 2 & 5 & \\
2 & 5 & 1& 2 & & & & 1 \\
0 & 4 & & & 5 &3 &1 & \\
& 2 & & 4& &4 & 1& 2 \\
};
\node[right=of mat1] (users) {users};
\matrix[s-matrix,right=of users] (mat2) {
1 & 1 & 1& &1 & 1 & & \\
0 & 1 & & & & 0 & 0 & 1 \\
0 & & & & & 0 & 0 & \\
1 & & 1& 1 & & & & 1 \\
0 & 1 & & & 1 &1 &1 & \\
& 1 & & & & & & \\
};
\foreach \X in {1,2}
{\node[above=of mat\X] (items-\X) {items};
\node at (mat\X-1-1|-items-\X) {i$_1$};
\node at (mat\X-1-8|-items-\X) {i$_8$};}
\foreach \X in {1,6}
{\draw[dotted] (mat1-\X-2-|mat1.east) -- (mat2-\X-2-|mat2.west)
node[midway,fill=white,inner sep=0.5pt]{u$_\X$};}
\node[below=1em of mat1,font=\sffamily\bfseries] {Explicit ratings};
\node[below=1em of mat2,font=\sffamily\bfseries] {Implicit ratings};
\end{tikzpicture}
\end{document}
答案2
不如优雅土拨鼠的解决方案但也许也很有趣:
\documentclass{article}
\usepackage{amsmath,blkarray}
\usepackage{tikz}
\usetikzlibrary{shadows, shapes, positioning, matrix}
\makeatletter % from https://tex.stackexchange.com/a/129322/121799
\tikzset{no shadows/.code=\let\tikz@preactions\pgfutil@empty}
\makeatother
\begin{document}
\begin{tikzpicture}[
every node/.style = {draw,
fill=white,
drop shadow,
anchor=center,
text height=2ex,
text depth=0.25ex},
]
\matrix (leftmatrix) [no shadows,
draw=none,
matrix of nodes,
nodes in empty cells,
row sep =-\pgflinewidth,
column sep = -\pgflinewidth,
minimum width=0.5cm,
minimum height=0.5cm,
]
{
5 & 1 & 3 & & 2 & 1 & & \\
4 & 2 & & 3 & & 4 & 3 & 1 \\
1 & 3 & & & & 2 & 5 & \\
2 & 5 & 1 & 2 & & & 1 & \\
0 & 4 & & & 5 & 3 & 1 & \\
& 2 & & 4 & & 4 & 1 & 2 \\
};
\node[right=0.25 cm of leftmatrix, draw=none, no shadows, font=\itshape] (users) {users};
\matrix (rightmatrix) [no shadows,
draw=none,
matrix of nodes,
nodes in empty cells,
row sep =-\pgflinewidth,
column sep = -\pgflinewidth,
minimum width=0.5cm,
minimum height=0.5cm,
right= 0.25cm of users]
{
1 & 1 & 1 & & 1 & 1 & & \\
0 & 1 & & & & 0 & 0 & 1 \\
0 & & & & & 0 & 0 & \\
1 & & 1 & 1 & & & & 1 \\
0 & 1 & & & 1 & 1 & 1 & \\
& 1 & & & & & & \\
};
\node[above= 0.1cm of leftmatrix, draw=none, no shadows, font=\itshape] (itemsleft) {items};
\node[above= 0.1cm of rightmatrix, draw=none, no shadows, font=\itshape] (itemsright) {items};
\node[below= 0.25cm of leftmatrix, draw=none, no shadows, font=\bfseries] (captionleft) {Explicit Rating};
\node[below= 0.25cm of rightmatrix, draw=none, no shadows, font=\bfseries] (captionright) {Implicit Rating};
\node[draw=none, no shadows] at (leftmatrix-1-1|-itemsleft) {$i_1$};
\node[draw=none, no shadows] at (leftmatrix-1-8|-itemsleft) {$i_8$};
\node[draw=none, no shadows] at (rightmatrix-1-1|-itemsleft) {$i_1$};
\node[draw=none, no shadows] at (rightmatrix-1-8|-itemsleft) {$i_8$};
\draw[dotted] (leftmatrix-1-2-|leftmatrix.east) -- (rightmatrix-1-2-|rightmatrix.west)
node[midway,fill=white,inner sep=0.5pt, draw=none, no shadows]{$u_1$};
\draw[dotted] (leftmatrix-6-2-|leftmatrix.east) -- (rightmatrix-6-2-|rightmatrix.west)
node[midway,fill=white,inner sep=0.5pt, draw=none, no shadows]{$u_6$};
\end{tikzpicture}
\end{document}
或者没有no shadows
。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shadows, shapes, positioning, matrix}
\begin{document}
\begin{tikzpicture}[
every node/.style = {draw,
fill=white,
anchor=center,
text height=2ex,
text depth=0.25ex},
]
\matrix (leftmatrix) [fill=white,drop shadow,inner sep=0pt,
draw=none,
matrix of nodes,
nodes in empty cells,
row sep =-\pgflinewidth,
column sep = -\pgflinewidth,
cells={minimum width=0.5cm,
minimum height=0.5cm}
]
{
5 & 1 & 3 & & 2 & 1 & & \\
4 & 2 & & 3 & & 4 & 3 & 1 \\
1 & 3 & & & & 2 & 5 & \\
2 & 5 & 1 & 2 & & & 1 & \\
0 & 4 & & & 5 & 3 & 1 & \\
& 2 & & 4 & & 4 & 1 & 2 \\
};
\node[right=0.25 cm of leftmatrix, draw=none, font=\itshape] (users) {users};
\matrix (rightmatrix) [fill=white,drop shadow,inner sep=0pt,
draw=none,
matrix of nodes,
nodes in empty cells,
row sep =-\pgflinewidth,
column sep = -\pgflinewidth,
cells={minimum width=0.5cm,
minimum height=0.5cm},
right= 0.25cm of users]
{
1 & 1 & 1 & & 1 & 1 & & \\
0 & 1 & & & & 0 & 0 & 1 \\
0 & & & & & 0 & 0 & \\
1 & & 1 & 1 & & & & 1 \\
0 & 1 & & & 1 & 1 & 1 & \\
& 1 & & & & & & \\
};
\node[above= 0.1cm of leftmatrix, draw=none, font=\itshape] (itemsleft) {items};
\node[above= 0.1cm of rightmatrix, draw=none, font=\itshape] (itemsright) {items};
\node[below= 0.25cm of leftmatrix, draw=none, font=\bfseries] (captionleft) {Explicit Rating};
\node[below= 0.25cm of rightmatrix, draw=none, font=\bfseries] (captionright) {Implicit Rating};
\node[draw=none] at (leftmatrix-1-1|-itemsleft) {$i_1$};
\node[draw=none] at (leftmatrix-1-8|-itemsleft) {$i_8$};
\node[draw=none] at (rightmatrix-1-1|-itemsleft) {$i_1$};
\node[draw=none] at (rightmatrix-1-8|-itemsleft) {$i_8$};
\draw[dotted] (leftmatrix-1-2-|leftmatrix.east) -- (rightmatrix-1-2-|rightmatrix.west)
node[midway,fill=white,inner sep=0.5pt, draw=none]{$u_1$};
\draw[dotted] (leftmatrix-6-2-|leftmatrix.east) -- (rightmatrix-6-2-|rightmatrix.west)
node[midway,fill=white,inner sep=0.5pt, draw=none]{$u_6$};
\end{tikzpicture}
\end{document}
答案3
如果您不想学习 Tikz,那么只需制作一个表格即可。它不需要太多代码,最复杂的东西是“阴影”。
\documentclass{article}
% \usepackage{amsmath}
\usepackage{array}
\usepackage{booktabs, colortbl}
\begin{document}
\renewcommand{\arraystretch}{1.3}\sffamily{}
\begin{tabular}{*{18}{|c} |c|}
\multicolumn{3}{l}{$i_1$} & \multicolumn{2}{c}{\raisebox{2ex}{items}} &
\multicolumn{3}{r}{$i_8$} & \multicolumn{3}{c}{} &
\multicolumn{3}{l}{$i_8$} & \multicolumn{2}{c}{\raisebox{2ex}{items}} &
\multicolumn{3}{r}{$i_8$}\\\cmidrule{1-8} \cmidrule{12-19}
5 & 1 & 3& &2 & 1 & & & \multicolumn{3}{c|}{\dots $u_1$ \dots} & 5 & 1 & 3& &2 & 1 & &\\\cmidrule{1-8} \cmidrule{12-19}
4 & 2 & & 3& & 4 & 3 & 1 & \multicolumn{3}{c|}{} & 4 & 2 & & 3& & 4 & 3 & 1 \\\cmidrule{1-8} \cmidrule{12-19}
1 & 3 & & & & 2 & 5 & & \multicolumn{3}{c|}{} & 1 & 3 & & & & 2 & 5 &\\\cmidrule{1-8} \cmidrule{12-19}
2 & 5 & 1& 2 & & & & 1 & \multicolumn{3}{c|}{users} & 2 & 5 & 1& 2 & & & & 1\\\cmidrule{1-8} \cmidrule{12-19}
0 & 4 & & & 5 &3 &1 & & \multicolumn{3}{c|}{} & 0 & 4 & & & 5 &3 &1 & \\\cmidrule{1-8} \cmidrule{12-19}
& 2 & & 4& &4 & 1& 2 & \multicolumn{3}{c|}{\dots $u_6$ \dots} & & 2 & & 4& &4 & 1& 2\\\cmidrule{1-8}
\cmidrule{12-19}\addlinespace[-4pt]
\arrayrulecolor[gray]{0.5}
\cmidrule[2pt](l{0.2ex}r{-.2ex}){1-8}
\cmidrule[2pt](l{0.2ex}r{-.2ex}){12-19}\addlinespace[2ex]
\multicolumn{8}{c}{\textbf{Explicit ratings}}& \multicolumn{3}{c}{} & \multicolumn{8}{c}{\textbf{Implicit ratings}}\\
\end{tabular}
\end{document}
结果:
编辑
根据建议,使用\clines
,而不是\cmidrules
。我们需要后者来表示阴影(据我所知,您无法修剪 \clines)。
我也趁机精简了一下布局,版本2的代码如下:
\documentclass{article}
% \usepackage{amsmath}
\usepackage{array}
\usepackage{booktabs, colortbl, ragged2e}
\begin{document}
\renewcommand{\arraystretch}{1.8}\sffamily{}
\begin{tabular}{*{18}{|>{\Centering}p{1em}} |>{\Centering}p{1em}|}
\multicolumn{3}{l}{$i_1$} & \multicolumn{2}{c}{\raisebox{2ex}{items}} &
\multicolumn{3}{r}{$i_8$} & \multicolumn{3}{c}{} &
\multicolumn{3}{l}{$i_8$} & \multicolumn{2}{c}{\raisebox{2ex}{items}} &
\multicolumn{3}{r}{$i_8$}\\\cline{1-8} \cline{12-19}
5 & 1 & 3& &2 & 1 & & & \multicolumn{3}{c|}{\dots $u_1$ \dots} & 5 & 1 & 3& &2 & 1 & &\\\cline{1-8} \cline{12-19}
4 & 2 & & 3& & 4 & 3 & 1 & \multicolumn{3}{c|}{} & 4 & 2 & & 3& & 4 & 3 & 1 \\\cline{1-8} \cline{12-19}
1 & 3 & & & & 2 & 5 & & \multicolumn{3}{c|}{} & 1 & 3 & & & & 2 & 5 &\\\cline{1-8} \cline{12-19}
2 & 5 & 1& 2 & & & & 1 & \multicolumn{3}{c|}{users} & 2 & 5 & 1& 2 & & & & 1\\\cline{1-8} \cline{12-19}
0 & 4 & & & 5 &3 &1 & & \multicolumn{3}{c|}{} & 0 & 4 & & & 5 &3 &1 & \\\cline{1-8} \cline{12-19}
& 2 & & 4& &4 & 1& 2 & \multicolumn{3}{c|}{\dots $u_6$ \dots} & & 2 & & 4& &4 & 1& 2\\\cline{1-8}
\cline{12-19}\addlinespace[-1pt]
\arrayrulecolor[gray]{0.4}
\cmidrule[3pt](l{0.2ex}r{-.2ex}){1-8}
\cmidrule[3pt](l{0.2ex}r{-.2ex}){12-19}\addlinespace[1ex]
\multicolumn{8}{c}{\textbf{Explicit ratings}}& \multicolumn{3}{c}{} & \multicolumn{8}{c}{\textbf{Implicit ratings}}\\
\end{tabular}
\end{document}
还有图片: