我尝试使用自定义形状(交叉矩形),如下所示:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,calc,decorations.pathreplacing,shapes.misc}
\usetikzlibrary{matrix}
\tikzset{
module/.style={draw, rectangle, fill=white, minimum size=1em,outer sep=0,inner sep=0},
mycross/.style={path picture={
\draw[#1] (path picture bounding box.south west) rectangle (path picture bounding box.north east);
\draw[#1] (path picture bounding box.south west) -- (path picture bounding box.north east);
\draw[#1] (path picture bounding box.south east) -- (path picture bounding box.north west);
}}
}
\begin{document}
\begin{tikzpicture}
\tikzset{column align/.style 2 args={column #1/.append style={nodes={execute at begin
node={\setbox\matrixcellbox=\hbox\bgroup},
execute at end
node={\egroup\eqmakebox[\tikzmatrixname\the\pgfmatrixcurrentcolumn][#2]{\copy\matrixcellbox}}}}}}
\matrix[fill=black!20, matrix of nodes, nodes={module},
row sep=-\pgflinewidth,column sep=-\pgflinewidth
]
{
Y & Y & Y \\
Y & Y & |[mycross]| \\
Y & Y & Y \\
};
\end{tikzpicture}
\end{document}
但是自定义的单元格与其他单元格不合适,并且我希望添加带有红色!30 颜色的红色背景。
答案1
您只需添加anchor=center
。即替换nodes={module}
为nodes={module,anchor=center}
即可获得
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,calc,decorations.pathreplacing,shapes.misc}
\tikzset{
module/.style={draw, rectangle, fill=white, minimum size=1em,outer sep=0,inner sep=0},
mycross/.style={path picture={
\draw[#1] (path picture bounding box.south west) rectangle (path picture bounding box.north east);
\draw[#1] (path picture bounding box.south west) -- (path picture bounding box.north east);
\draw[#1] (path picture bounding box.south east) -- (path picture bounding box.north west);
}}
}
\begin{document}
\begin{tikzpicture}
\tikzset{column align/.style 2 args={column #1/.append style={nodes={execute at begin
node={\setbox\matrixcellbox=\hbox\bgroup},
execute at end
node={\egroup\eqmakebox[\tikzmatrixname\the\pgfmatrixcurrentcolumn][#2]{\copy\matrixcellbox}}}}}}
\matrix[fill=black!20, matrix of nodes, nodes={module,anchor=center},
row sep=-\pgflinewidth,column sep=-\pgflinewidth
]
{
Y & Y & Y \\
Y & Y & |[mycross]| \\
Y & Y & Y \\
};
\end{tikzpicture}
\end{document}
顺便说一句,在当前用例中,文档可以简化为
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
module/.style={draw, rectangle, fill=white, minimum size=1em,outer sep=0,inner sep=0},
mycross/.style={path picture={
\draw[#1] (path picture bounding box.south west) rectangle (path picture bounding box.north east);
\draw[#1] (path picture bounding box.south west) -- (path picture bounding box.north east);
\draw[#1] (path picture bounding box.south east) -- (path picture bounding box.north west);
}}
}
\begin{document}
\begin{tikzpicture}
\matrix[fill=black!20, matrix of nodes, nodes={module,anchor=center},
row sep=-\pgflinewidth,column sep=-\pgflinewidth
]
{
Y & Y & Y \\
Y & Y & |[mycross]| \\
Y & Y & Y \\
};
\end{tikzpicture}
\end{document}
特别是,column align
没有使用,如果要使用它,请确保加载该eqmakebox
包。