![TikZ 中跨越整个矩阵列的节点](https://linux22.com/image/334047/TikZ%20%E4%B8%AD%E8%B7%A8%E8%B6%8A%E6%95%B4%E4%B8%AA%E7%9F%A9%E9%98%B5%E5%88%97%E7%9A%84%E8%8A%82%E7%82%B9.png)
来自另一个问题的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}