我想创建一个有垂直和水平部分的节点。经过一番搜索,我发现可以使用矩阵来实现这一点。我尝试了以下代码:
\documentclass{article}
\usepackage{tikz,listings}
\usetikzlibrary{shapes.multipart,matrix}
\begin{document}
\begin{tikzpicture}[->, my shape/.style={
rectangle split, rectangle split parts=#1, draw, anchor=center}]
%transactional descriptor
\node[matrix, align=center, label=above:transactional descriptor] (up_part) at (7, 0) {
\node [my shape=4, rectangle split horizontal] (up_line) at (3, 0)
{\nodepart{one}\lstinline!start!\nodepart{two}\lstinline!readSet!\nodepart{three}
\lstinline!writeSet!\nodepart{four}\lstinline!commitTime!}; \\
};
\node[matrix, below of=up_part, node distance=0.5cm] (down_part)
{
\node [my shape=2, rectangle split horizontal] (down_line) at (3, 0)
{\nodepart{one}\lstinline!overwrittenVersions!\quad\nodepart{two}\lstinline!next!}; \\
};
\end{tikzpicture}
\end{document}
问题是包含两部分的第二个节点的宽度与其上方的节点的宽度不一样,如下图所示:
我尝试使用minimum width
或text width
,但没有作用。
任何想法如何解决这一问题?
答案1
像这样吗?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{chains,calc}
\begin{document}
\usetikzlibrary{chains,calc}
\begin{tikzpicture}[start chain,node distance=0mm,every node/.style={font=\ttfamily,outer sep=0,text height=1.5ex}]
\node [draw,on chain] (a) {start};
\node [draw,on chain] {readSet};
\node [draw,on chain] {writeSet};
\node [draw,on chain] (b) {commitTime};
\node[draw,anchor=north west] at (a.south west) (c) {overwrittenVersions};
\path
let \p1=(c.east),\p2=(b.east),\n1={(\x2-\x1)}
in
node[draw,anchor=west,minimum width=\n1] at (c.east) {next};
\end{tikzpicture}
\end{document}
答案2
除非你真的需要每个元素都是一个实际的 TikZ 节点,否则使用简单的表格可能会更简单:
\documentclass{article}
\usepackage{tikz,listings}
\usepackage{array}
\newcolumntype{C}{>{\ttfamily}c}
\usetikzlibrary{shapes.multipart,matrix}
\begin{document}
\begin{tikzpicture}[->, my shape/.style={
rectangle split, rectangle split parts=#1, draw, anchor=center}]
%transactional descriptor
\node (A) {%
\begin{tabular}{|C|C|C|C|}
\hline
start & readSet & writeSet & commitTime\\
\hline
\multicolumn{3}{|C|}{overwrittenVersions} & next\\
\hline
\end{tabular}
};
\end{tikzpicture}
\end{document}