我正在学习 tikz。
我想将两个垂直置于三个节点右侧。
在附加的代码中,我可以将其定位在节点的右侧,但不能将其垂直居中。
有人可以指出如何使用 tikz 实现这一点吗?
\documentclass{minimal}
\usepackage{tikz}
\tikzset{block/.style={rectangle,draw}}
\begin{document}
\begin{tikzpicture}[node distance=0.10cm and 2.0cm]
\node[block] (node1) { Column 1 Row 1 };
\node[block, below=of node1] (node2) { Column 1 Row 2 };
\node[block, below=of node2] (node3) { Column 1 Row 3 };
\node[block, below=of node3] (node4) { Column 1 Row 4 };
\node[block, right=of node2] (node5) { Column 2 vertically centered between Nodes 2-4 };
\node[block, below=of node5] (node6) { Column 2 vertically centered between Nodes 2-4 };
\end{tikzpicture}
\end{document}
答案1
我也在学习Ti钾Z. 您需要加载库positioning
才能使代码通过。您想要这样的东西吗?
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\tikzset{block/.style={rectangle,draw}}
\begin{document}
\begin{tikzpicture}[node distance=0.10cm and 2.0cm]
\node[block] (node1) { Column 1 Row 1 };
\node[block, below=of node1] (node2) { Column 1 Row 2 };
\node[block, below=of node2] (node3) { Column 1 Row 3 };
\node[block, below=of node3] (node4) { Column 1 Row 4 };
\coordinate[right=of node1] (right);
\coordinate (vert) at ($(node2.center)!0.5!(node3.center)$);
\node[block,anchor=west] (node5) at (vert-|right) { Column 2 vertically centered between Nodes 2-4 };
\node[block, below=of node5] (node6) { Column 2 vertically centered between Nodes 2-4 };
\end{tikzpicture}
\end{document}
答案2
在软件包positioning
和的帮助下chains
:
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{calc, chains, positioning}
\tikzset{block/.style={rectangle, draw, on chain}}
\begin{document}
\begin{tikzpicture}[
node distance=1mm and 20mm,
start chain=going below
]
\node[block] (node1) { Column 1 Row 1 };
\node[block] (node2) { Column 1 Row 2 };
\node[block] (node3) { Column 1 Row 3 };
\node[block] (node4) { Column 1 Row 4 };
\begin{scope}[node distance=0.5mm and 20mm]
\node[block, above right=of $(node2.south)!0.5!(node4.north)$] (node5)
{ Column 2 vertically centered between Nodes 2-4 };
\node[block, below right=of $(node2.south)!0.5!(node4.north)$] (node6)
{ Column 2 vertically centered between Nodes 2-4};
\end{scope}
\end{tikzpicture}
\begin{tikzpicture}[
node distance=1mm and 20mm,
start chain=going below,
block/.append style={text width=32mm, align=left}
]
\node[block] (node1) { Column 1 Row 1 };
\node[block] (node2) { Column 1 Row 2 };
\node[block] (node3) { Column 1 Row 3 };
\node[block] (node4) { Column 1 Row 4 };
\begin{scope}[node distance=0.5mm and 20mm]
\node[block, above right=of $(node2.south)!0.5!(node4.north)$] (node5)
{ Column 2 vertically centered between Nodes 2-4 };
\node[block, below right=of $(node2.south)!0.5!(node4.north)$] (node6)
{ Column 2 vertically centered between Nodes 2-4};
\end{scope}
\end{tikzpicture}
\end{document}
然而,当第二列中的节点高度不一样时,它们的垂直居中就不再简单了。