来自另一个问题的MWE这里:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix, backgrounds}
\begin{document}
\centering
\begin{tikzpicture}[%
block/.style={draw, text width=5em, text centered, rounded corners, minimum height=2em},
entity/.style={draw, text centerd}
]
\begin{scope}
\matrix (A) [matrix of nodes, inner sep=1em, row sep= 2em, column sep= 2em, nodes={block}] {
& A & \\
B & & C\\
& D & \\
E & & \\
};
\end{scope}
\path (A.north) -- coordinate[pos=.25] (aux1) coordinate[pos=.75] (aux2) (A.south);
\begin{scope}[on background layer]
\draw[fill=green!30] (A.north west) rectangle (aux1-|A.east);
\draw[fill=red!30] (A.west|-aux1) rectangle (A.east);
\draw[fill=blue!30] (A.west) rectangle (aux2-|A.east);
\draw[fill=orange!30] (A.west|-aux2) rectangle (A.south east);
\end{scope}
\end{tikzpicture}
\end{document}
是否可以添加节点:
- 可以跨越矩阵的整个列,而不会使其行过大
- 并且被彩色带覆盖吗?
答案1
一种方法是使用fit
库tikz
。对相关代码进行最小程度的更改,我在空节点中使用\phantom
并使用fit
库添加了一些虚拟文本。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix, backgrounds,fit}
\begin{document}
\centering
\begin{tikzpicture}[%
block/.style={draw, text width=5em, text centered, rounded corners, minimum height=2em},
entity/.style={draw, text centerd}
]
\begin{scope}
\matrix (A) [matrix of nodes, inner sep=1em, row sep= 2em, column sep= 2em, nodes={block}] {
& |[draw=none]| \phantom{A} & \\
B & & C\\
& |[draw=none]| \phantom{A} & \\
E & |[draw=none]| \phantom{A} & \\
};
\end{scope}
\path (A.north) -- coordinate[pos=.25] (aux1) coordinate[pos=.75] (aux2) (A.south);
\begin{scope}[on background layer]
\draw[fill=green!30] (A.north west) rectangle (aux1-|A.east);
\draw[fill=red!30] (A.west|-aux1) rectangle (A.east);
\draw[fill=blue!30] (A.west) rectangle (aux2-|A.east);
\draw[fill=orange!30] (A.west|-aux2) rectangle (A.south east);
\end{scope}
\node[fit={(A-1-2) (A-4-2)},block] {Here comes the naughty column}; %% fit here
\end{tikzpicture}
\end{document}