我希望节点c
和d
位于内部q
,并且的位置q
由确定p
。
p
第二个问题是,即使使用,和之间的距离q
也不正确on grid=false
(我猜测由于使用了?,所以宽度p
不可用fit
)。
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning,fit}
\begin{document}
\begin{tikzpicture}
\node (a) [draw=black] {a};
\node (b) [draw=black,on grid,below=of a] {long text};
\node (p) [draw=black,fit={(a) (b)}] {};
\node (c) [draw=red] {c};
\node (d) [draw=red,below=of c] {d};
\node (q) [right=of p,draw=red,fit={(c) (d)}] {};
\end{tikzpicture}
\end{document}
答案1
使用 怎么样matrix
?
\documentclass[tikz]{standalone}
\usetikzlibrary{fit,matrix}
\begin{document}
\begin{tikzpicture}
\matrix[column sep=8mm,row sep=3mm] {
\node (a) [draw=black] {a};
&
\node (c) [draw=red] {c};
\\
\node (b) [draw=black] {long text};
&
\node (d) [draw=red] {d};
\\
};
\node (p) [draw=black,fit={(a) (b)}] {};
\node (q) [draw=red,fit={(c) (d)}] {};
\end{tikzpicture}
\end{document}
使用一个矩阵(而不是两个)的优点是,如果节点的垂直尺寸变化,它也看起来不错。
\documentclass[tikz]{standalone}
\usetikzlibrary{fit,matrix}
\begin{document}
\begin{tikzpicture}
\matrix[column sep=8mm,row sep=3mm,anchor=center] (mat) {
\node (a) [draw=black] {a};
&
\node (c) [draw=red] {c};
\\
\node (b) [draw=black,align=center] {long\\ text};
&
\node (d) [draw=red] {d};
\\
};
\node (p) [draw=black,fit={(a) (b) (b|-mat.south)}] {};
\node (q) [draw=red,fit={(c) (d) (d|-mat.south)}] {};
\end{tikzpicture}
\end{document}
就像任何 Ti钾Z 矩阵,每一行(包括最后一行)都必须以 结束\\
。
答案2
土拨鼠建议使用matrix
,但我认为两个矩阵更好:
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning,matrix}
\begin{document}
\begin{tikzpicture}
\matrix[draw, matrix of nodes, nodes=draw, row sep=3mm] (a) {
a\\
long text\\
};
\matrix[draw=red, matrix of nodes, nodes={draw=red},
right=3mm of a, row sep=3mm] (b) {
c\\
d\vphantom{g}\\
};
\end{tikzpicture}
\end{document}
答案3
\documentclass[tikz,margin=3mm]{standalone}
\usetikzlibrary{fit, positioning}
\begin{document}
\begin{tikzpicture}[
node distance = 8mm and 4 mm,
every node/.style = {inner sep=1mm, minimum height=1.5em}
]
\node (a) [draw=black] {a};
\node (b) [draw=black,below=of a] {long text};
\node (p) [draw=black,fit={(a) (b)}] {};
\node (c) [draw=red,right=of b.east |- a] {c};
\node (d) [draw=red,below=of c] {d};
\node (q) [draw=red,fit={(c) (d)}] {};%right=of p,
\end{tikzpicture}
\end{document}