2

I'm trying to find an elegant solution to make a bunch of shapes like this split rectangle with attached node

The best I've come up with is drawing a split rectangle, with a style like

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

(taken from here). But I can't figure out an elegant way to add a third part other than create an extra node every time and position it properly. I want to be able to create the nodes with a one liner like \node [entry] (A) {1001 \nodepart{second} 1010 \nodepart{third} 2};, but I'm not sure how to go about that.

2

Well, if you're open to a slight change in the syntax and some hackery, you can create frankenstein nodes by abusing TikZ's label, pin and append after command options.

The rectangle split shape already is close to what you want. However, it doesn't allow using a different line style for the separator. We can, however, prevent the separator from being drawn at all and provide our own path. This is done using an edge operation in the append after command.

The attached rectangle can be drawn by using the label option. Properly positioning it involves a shift so that the borders overlap each other, which is done with the outer xsep option. To allow references to this new node's anchors, we also give it a name which is constructed from the name of the main node.

Here's a 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}

output

A caveat is that you can't use the anchors of the extra node while the construct is being placed. Also, the contents of this node are an option to the style instead of a real node part.

  • Didn't think to use a label! Nice idea – Adam Martin Mar 23 '16 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.