建立具有不同高度部分的分割矩形節點

建立具有不同高度部分的分割矩形節點

我正在嘗試找到一個優雅的解決方案來製作一堆這樣的形狀帶有附加節點的分割矩形

我想出的最好的方法是繪製一個分割矩形,其樣式如下

\tikzset{
entry/.style={
    draw,
    rectangle split,
    rectangle split parts=2,
}

(取自這裡)。但除了每次創建一個額外的節點並正確定位它之外,我無法找到一種優雅的方法來添加第三部分。我希望能夠使用像這樣的單行來建立節點\node [entry] (A) {1001 \nodepart{second} 1010 \nodepart{third} 2};,但我不知道如何做。

答案1

好吧,如果您願意對語法進行輕微更改和一些黑客攻擊,您可以通過濫用 TikZ 的label,pinappend after command選項來創建 Frankenstein 節點。

形狀rectangle split已經接近你想要的了。但是,它不允許對分隔符號使用不同的線條樣式。但是,我們可以完全阻止分隔符號被繪製並提供我們自己的路徑。這是使用 中的邊緣操作來完成的append after command

可以使用該label選項繪製附加的矩形。正確定位它涉及移動,以便邊界相互重疊,這是透過該outer xsep選項完成的。為了允許引用這個新節點的錨點,我們也給它一個由主節點的名稱建構的名稱。

這是一個 MWE:

\documentclass[border=1mm,tikz]{standalone}
\usetikzlibrary{shapes.multipart}

\tikzset{entry/.style={
    % appearance settings for the main node
    draw,
    rectangle split,
    rectangle split parts=2,
    rectangle split draw splits=false,
    % draw the small attached node
    label={[name=label of \tikzlastnode,anchor=north west,draw,outer xsep=-.5\pgflinewidth]north east:#1},
    % draw the split using a dashed line
    append after command={(\tikzlastnode.text split west) edge[draw,dashed] (\tikzlastnode.text split east)},
}}

\begin{document}
    \begin{tikzpicture}
        \node[entry=2] (myentry) at (0,0) {1001 \nodepart{second} 1010};

        % examples of how we can refer to coordinates of this monster
        \node[coordinate,pin=4:a] at (label of myentry.south east) {};
        \node[coordinate,pin=4:b] at (myentry.south east) {};
    \end{tikzpicture}
\end{document}

輸出

需要注意的是,在放置構造時不能使用額外節點的錨點。此外,該節點的內容是樣式的一個選項,而不是真正的節點部分。

相關內容