![如何将样式应用于 tikz 矩阵的子矩阵元素?](https://linux22.com/image/323593/%E5%A6%82%E4%BD%95%E5%B0%86%E6%A0%B7%E5%BC%8F%E5%BA%94%E7%94%A8%E4%BA%8E%20tikz%20%E7%9F%A9%E9%98%B5%E7%9A%84%E5%AD%90%E7%9F%A9%E9%98%B5%E5%85%83%E7%B4%A0%EF%BC%9F.png)
我尝试将样式应用于矩阵的子矩阵,最终通过两个 .list-handler 传递参数。事情并没有像我预期的那样顺利进行,我再次只能设置行或列,但不能同时设置两者。
有什么问题?你还有其他想法吗?
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
\tikzset{
rows/.style 2 args={
@/.style={row ##1 column #2/.style={nodes={rectangle,draw=black}}},
@/.list={#1}
},
box/.style 2 args={
@/.style={rows={#1}{##1}},
@/.list={#2}
}
}
\tikzset{square matrix/.style={
matrix of nodes,
column sep=-\pgflinewidth, row sep=-\pgflinewidth,
nodes={
minimum height=#1,
anchor=center,
text width=#1,
align=center,
inner sep=0pt
},
},
square matrix/.default=1.2cm
}
\begin{document}
\begin{tikzpicture}[]
\matrix[square matrix,
box={1,...,3}{3,2} % apply the style to cells {1,3},{2,3},{3,3},{1,2},{2,2},{3,2}
]
{
& x & x & & & \\
& & x & & x & \\
x & x & x & x & x & \\
& & x & & & \\
};
\end{tikzpicture}
\end{document}
答案1
@
您的代码基本没问题。您只需要在rows
和中不要使用相同的样式box
。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
\tikzset{
rows/.style 2 args={
sub@rows/.style={row ##1 column #2/.style={nodes={rectangle,draw=red}}},
sub@rows/.list={#1}
},
box/.style 2 args={
sub@box/.style={rows={#1}{##1}},
sub@box/.list={#2}
}
}
\tikzset{square matrix/.style={
matrix of nodes,nodes in empty cells,
column sep=-\pgflinewidth, row sep=-\pgflinewidth,
nodes={
minimum height=#1,
anchor=center,
text width=#1,
align=center,
inner sep=0pt
},
},
square matrix/.default=1.2cm
}
\begin{document}
\begin{tikzpicture}[]
\matrix[square matrix,
box={1,...,3}{3,2} % apply the style to cells {1,3},{2,3},{3,3},{1,2},{2,2},{3,2}
]
{
& x & x & & & \\
& & x & & x & \\
x & x & x & x & x & \\
& & x & & & \\
};
\end{tikzpicture}
\end{document}