我尝试将样式应用于矩阵的子矩阵,最终通过两个 .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}