我想创建一个在单个矩阵单元中有多个节点的矩阵。这些节点应该彼此居中。我设法通过使用嵌套的 tikz 环境来实现这一点,如下所示:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[auto, semithick]
\tikzstyle{block} = [rectangle, draw,
minimum width=5em, text centered, rounded corners, minimum
height=4em]
\matrix[matrix of nodes, row sep = 2em,
nodes={anchor=center}
] (mx2){
% First row:
label1
&
\node{\tikz{
\node[block](n1){node1};
\node[block, right=of n1](n2){node2};
}};
\\
% Second row:
label2
&
\node{\tikz{
\node[block] (n3)
{node 3};
\node[block] (n4) [right=of n3]
{node 4};
\node[block] (n5) [right=of n4]
{node 5};
}};
\\
};
\draw (n1) -- (n4); % this fails
\end{tikzpicture}
\end{document}
现在由于嵌套的 tikz 环境,连接这些节点是不可能的。我曾尝试使用该fit
库获取类似的图像,但未能成功。你们有人能帮我吗?
先感谢您。
答案1
一般情况下,可以使用remember picture
选项并应用它全部图片,稍后应可访问。由于您的图片是嵌套的,并且选项是可继承的,因此remember picture
仅应用于上层就足够了{tikzpicture}
。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[auto, semithick, remember picture,
block/.style={rectangle, draw,
minimum width=5em, text centered, rounded corners,
minimum height=4em, text width=5em}
]
\matrix[matrix of nodes, row sep = 2em,
nodes={anchor=center}
] (mx2){
% First row:
label1
&
\node{\tikz{
\node[block] (n1) {node1};
\node[block, right=of n1] (n2) {node2};
}};
\\
% Second row:
label2
&
\node{\tikz{
\node[block] (n3) {node 3};
\node[block] (n4) [right=of n3] {node 4};
\node[block] (n5) [right=of n4] {node 5};
}};
\\
};
\draw (n1) -- (n4); % this works
\end{tikzpicture}
\end{document}
编译两次即可获得正确结果。
请注意,我\tikzstyle{block}
用替换了block/.style
这是首选方法。请参阅应该使用 \tikzset 还是 \tikzstyle 来定义 TikZ 样式?。
答案2
我认为matrix
这里库是多余的(尽管我一直在滥用它)。直接使用也是可能的,而且(在我看来)更方便。positioning
无论如何都会使用库,因此我们可以继续依赖它。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
block/.style={rectangle, draw,
minimum width=5em, text centered, rounded corners, minimum
height=4em,text width=5em}
]
\node (l1) at (0,2) {label 1};
\node (l2) at (0,0) {label 2};
\node[right = 2.5cm of l1,block] (n1) {Node 1};
\node[right = of n1,block] (n2) {Node 2};
\node[right = 1cm of l2,block] (n3) {Node 3};
\node[right = of n3,block] (n4) {Node 4};
\node[block,right=of n4] (n5) {node 5};
\draw (n1) -- (n4); % this fails not :)
\end{tikzpicture}
\end{document}
node distance
请注意,您可以通过选项跟踪单独的定位和默认设置,就像在使用矩阵时需要<direction> = x cm of nodename
做的那样。row sep
column sep
编辑正如 Altermundus 所评论的那样,您可以通过以预定义的坐标网格为中心来堆叠行
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
block/.style={rectangle, draw, text centered, rounded corners, minimum height=4em}
]
\foreach \x in {0,...,2} \node[circle,inner sep=1mm,fill] (cent\x) at (2,2*\x) {};
%The center goes in between
\node[right=of cent0,block] (n1) {A very wide node 1};
\node[left =of cent0,block] (n2) {2};
%The center hits the node
\node[block] at (cent1) (n4) {node 4};
\node[block,left =of n4] (n3) {Also a quite wide node 3};
\node[block,right=of n4] (n5) {5};
\draw (n1) -- (n4); % this fails not :)
%This uses eyeballing after compiling. Choose the widest and adjust.Can be absolute too.
\node[left= of n3] (l2) {label 2};
\node (l1) at (l2 |- cent0){label 1};
\node (l3) at (l2 |- cent2){label 3};
\end{tikzpicture}
\end{document}
答案3
你可以避免图片嵌套
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[auto, semithick,remember picture,
block/.style={rectangle, draw,
minimum width=5em, text centered, rounded corners, minimum
height=4em,text width=5em}
]
\matrix[matrix of nodes, row sep = 2em,
nodes={anchor=center}
] (mx2){
% First row:
label1
&
\node[block,right=1em,anchor=west](n1){node1};
\node[block, right=of n1](n2){node2};
\\
% Second row:
label2
&
\node[block] (n3) {node 3};
\node[block] (n4) [right=of n3] {node 4};
\node[block] (n5) [right=of n4] {node 5};
\\
};
\draw (n1) -- (n4); % this fails
\end{tikzpicture}
\end{document}