我正在尝试在 tikz 中重新创建下表(我需要这样做,因为我可以在 jupyter 笔记本中使用 tikz 来渲染嵌入输出但尚未乳胶)
当我通过 tikz 尝试时,我只能走到这一步......
我的 MWE:
\begin{tikzpicture}
\tikzset{square matrix/.style={
matrix of nodes,
column sep=-\pgflinewidth, row sep=-\pgflinewidth,
nodes={draw,
minimum height=#1,
anchor=center,
text width=#1,
align=center,
inner sep=0pt
},
},
square matrix/.default=1.2cm
}
\matrix[square matrix]
{
1 & & & & & & \\
2 & x & & & & & \\
3 & x & x & & & & \\
4 & x & x & x & & & \\
5 & x & x & x & x & & \\
6 & x & x & x & x & x & \\
};
\end{tikzpicture}
答案1
Sebastiano 提供了缺失单元格的解决方案以及如何填充每个单元格,但也可以定义styles
完整的行和列。并通过一些额外的工作绘制左上角的单元格。
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{matrix, positioning}
\begin{document}
\begin{tikzpicture}
\tikzset{%
square matrix/.style={
matrix of nodes,
column sep=-\pgflinewidth,
row sep=-\pgflinewidth,
nodes in empty cells,
nodes={draw,
minimum size=#1,
anchor=center,
align=center,
inner sep=0pt
},
column 1/.style={nodes={fill=cyan!30}},
row 1/.style={nodes={fill=cyan!30}},
},
square matrix/.default=1.2cm
}
\matrix[square matrix] (A)
{
& 1 & 2 & 3 & 4 & 5 & 6\\
1 & & & & & & \\
2 & x & & & & & \\
3 & x & x & & & & \\
4 & x & x & x & & & \\
5 & x & x & x & x & & \\
6 & x & x & x & x & x & \\
};
\draw (A-1-1.north west)--(A-1-1.south east);
\node[below left=2mm and 2mm of A-1-1.north east] {$i$};
\node[above right=2mm and 2mm of A-1-1.south west] {$j$};
\end{tikzpicture}
\end{document}
更新:
为了获得一些特殊的颜色分布,就像在你的关联
您可以混合一些常规行和列的自动填充和一些背景的手动填充:
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{matrix, positioning, backgrounds}
\tikzset{%
square matrix/.style={
matrix of math nodes,
column sep=-\pgflinewidth,
row sep=-\pgflinewidth,
nodes in empty cells,
nodes={draw,
minimum width=#1,
minimum height=.5*#1,
anchor=center,
% align=center,
inner sep=0pt
},
column 1/.style={nodes={fill=gray!30}},
row 1/.style={nodes={fill=gray!30}},
},
square matrix/.default=1.2cm
}
\begin{document}
\begin{tikzpicture}
\matrix (A) [square matrix]
{
j\backslash i & x_1 & x_2 & x_3 & x_4 & x_5 & x_6\\
y_1 & R_{11} & R_{12} & R_{13} & R_{14} & R_{15} & R_{16} \\
y_2 & R_{21} & R_{22} & R_{23} & R_{24} & R_{25} & R_{26} \\
y_3 & R_{31} & R_{32} & R_{33} & R_{34} & R_{35} & R_{36} \\
y_4 & R_{41} & R_{42} & R_{43} & R_{44} & R_{45} & R_{46} \\
y_5 & R_{51} & R_{52} & R_{53} & R_{54} & R_{55} & R_{56} \\
y_6 & R_{61} & R_{62} & R_{63} & R_{64} & R_{65} & R_{66} \\
};
\begin{scope}[on background layer]
\fill[cyan!15] (A-3-2.north west)-|(A-4-3.north west)-|(A-5-4.north west)-|(A-6-5.north west)-|(A-7-6.north west)-|(A-7-6.south east)-|cycle;
\fill[red!15] (A-2-3.north west)|-(A-3-4.north west)
|-(A-4-5.north west)|-(A-5-6.north west)|-(A-6-7.north west)|-(A-6-7.south east)
|-cycle;
\foreach \i in {2,...,7}
\fill[gray!30] (A-\i-\i.north west) rectangle (A-\i-\i.south east);
\end{scope}
\end{tikzpicture}
\end{document}
第二次更新:
您可以构建一个自动解决方案:
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{matrix, positioning, backgrounds}
\tikzset{%
myfill/.code={%
\ifnum\pgfmatrixcurrentcolumn<\pgfmatrixcurrentrow
\pgfkeysalso{fill=cyan!30}
\else
\ifnum\pgfmatrixcurrentcolumn>\pgfmatrixcurrentrow
\pgfkeysalso{fill=red!30}
\else
\pgfkeysalso{fill=gray!20}
\fi
\fi
},
square matrix/.style={
matrix of math nodes,
column sep=-\pgflinewidth,
row sep=-\pgflinewidth,
nodes in empty cells,
nodes={draw,
myfill,
minimum width=#1,
minimum height=.5*#1,
anchor=center,
inner sep=0pt,
node contents={
R_{\the\numexpr\pgfmatrixcurrentcolumn-1\relax%
\the\numexpr\pgfmatrixcurrentrow-1\relax%
}}
},
column 1/.style={
nodes={fill=gray!30,
node contents={y_\the\numexpr\pgfmatrixcurrentrow-1\relax}
}},
row 1/.style={
nodes={fill=gray!30,
node contents={x_\the\numexpr\pgfmatrixcurrentcolumn-1\relax}
}},
row 1 column 1/.style={
nodes={fill=gray!30,
node contents={j\backslash i}
}},
},
square matrix/.default=1.2cm
}
\begin{document}
\begin{tikzpicture}
\matrix (A) [square matrix]
{
&&&&&&\\
&&&&&&\\
&&&&&&\\
&&&&&&\\
&&&&&&\\
&&&&&&\\
&&&&&&\\
};
\end{tikzpicture}
\end{document}
答案2
我已向您的 LaTeX 代码添加了以下选项:
\matrix[square matrix, nodes in empty cells]
{
& & & &\\
当然,添加或删除&
可以创建(不创建)其他空单元格。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\usetikzlibrary{matrix}
\tikzset{square matrix/.style={
matrix of nodes,
column sep=-\pgflinewidth, row sep=-\pgflinewidth,
nodes={draw,
minimum height=#1,
anchor=center,
text width=#1,
align=center,
inner sep=0pt
},
},
square matrix/.default=1.2cm
}
\matrix[square matrix, nodes in empty cells]
{
& & & &\\
1 & & & & & & \\
2 & x & & & & & \\
3 & x & x & & & & \\
4 & x & x & x & & & \\
5 & x & x & x & x & & \\
6 & x & x & x & x & x & \\
};
\end{tikzpicture}
\end{document}
附录:
我只填充了一个单元格。使用相同的结构,您可以填充一个或多个单元格。
\documentclass{article}
\usepackage{tikz}
\usepackage{xcolor}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\tikzset{square matrix/.style={
matrix of nodes,
column sep=-\pgflinewidth, row sep=-\pgflinewidth,
nodes={draw,
minimum height=#1,
anchor=center,
text width=#1,
align=center,
inner sep=0pt
},
},
square matrix/.default=1.2cm
}
\matrix[square matrix, nodes in empty cells]
{
|[fill=green]|& & & &\\
1 & & & & & & \\
2 & x & & & & & \\
3 & x & x & & & & \\
4 & x & x & x & & & \\
5 & x & x & x & x & & \\
6 & x & x & x & x & x & \\
};
\end{tikzpicture}
\end{document}