5

I'm having an odd problem with TikZ rectangle split horizontal and rectangle split ignore empty parts. The problem is that if you have empty parts (unused) then the last rectangle seems to have an extra rule on its right-hand side -- note in the example below that the rule width for lines 2-4 appear to be doubled in width:

enter image description here

Evidence of this problem can be seen in other questions (e.g., Draw a tree with tikzpicture, node collapsed problem) though this particular issue has not drawn comment that I can find.

Is there a way to overcome this problem?

Update

I can confirm that this does not happen (or does not appear to happen) with vertically split nodes.

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{shapes.multipart}

\tikzset{hmul/.style={
    rectangle split,
    rectangle split horizontal,
    rectangle split parts = 4,
    rectangle split ignore empty parts,
    draw
    }%
}

\begin{document}

\begin{tikzpicture}
    \node[hmul]{\nodepart{one}1 \nodepart{two}2 \nodepart{three}3 \nodepart{four}4};
\end{tikzpicture}

\begin{tikzpicture}
    \node[hmul]{\nodepart{one}1 \nodepart{two}2 \nodepart{three}3};
\end{tikzpicture}

\begin{tikzpicture}
    \node[hmul]{\nodepart{one}1 \nodepart{two}2};
\end{tikzpicture}

\begin{tikzpicture}
    \node[hmul]{\nodepart{one}1};
\end{tikzpicture}

\end{document}
3
  • 1
    Good question. I thought that adding rectangle split every empty part={},rectangle split empty part width=-\pgflinewidth, would do the trick but it does not.
    – SebGlav
    Jul 24, 2021 at 17:53
  • @SebGlav Too bad, but very clever -- wouldn't have thought of that myself...
    – sgmoye
    Jul 24, 2021 at 17:55
  • 2
    Bug is reported <github.com/pgf-tikz/pgf/issues/1032>
    – Symbol 1
    Jul 24, 2021 at 19:00

3 Answers 3

7

I hacked the code so that the splitting lines are drawn in red

Now you can see the problem: In the vertical case, all small boxes are of the same height. But this is not the case for horizontal split.

Notice how 1 and 4 (and 3) are not horizontally centered? This is because the left-most boundary is short by line width/2; similarly, the right-most boundary is short by line width/2.

So when PGF wants to draw the splitting line between three and four, it will draw it at the arithmetic progression position, which exceed the right boundary by line width/2.

I don't know how to fix this. Feel free to play with adding and subtracting \pgflinewidth and \innerxsep and stuffs like that.

1
  • 3
    Nice detective work. Thanks for the bug report.
    – sgmoye
    Jul 24, 2021 at 19:13
2

As for a trick to avoid this ugly thick line, while some of the library maintainers find a way to overcome the issue, I propose a solution by using a numeric parameter.

rectangle split ignore empty cells

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{shapes.multipart}

\tikzset{hmul/.style={
    rectangle split,
    rectangle split horizontal,
    rectangle split parts = #1,
    rectangle split ignore empty parts,
    draw
    }%
}

\begin{document}

\begin{tikzpicture}
    \node[hmul=4]{\nodepart{one}1 \nodepart{two}2 \nodepart{three}3 \nodepart{four}4};
\end{tikzpicture}

\begin{tikzpicture}
    \node[hmul=3]{\nodepart{one}1 \nodepart{two}2 \nodepart{three}3};
\end{tikzpicture}

\begin{tikzpicture}
    \node[hmul=2]{\nodepart{one}1 \nodepart{two}2};
\end{tikzpicture}

\begin{tikzpicture}
    \node[hmul=1]{\nodepart{one}1};
\end{tikzpicture}

\end{document}
1
  • Nice catch. I had pondered that sort of solution (thank you for confirming this approach), and, for the nonce, may use it. The bulk of the original application is automated (untouched by human hands ;) so I'll continue to hope that the Maintainers are inspired to find a solution... Thanks again.
    – sgmoye
    Jul 24, 2021 at 20:29
2

As an alternative, it's easy to use matrix nodes to draw these kind of structures.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\tikzset{hmul/.style={
    matrix,
    matrix of nodes,
    inner sep=0pt,
    nodes={
        anchor=center,
        inner sep=.3333em,
        draw},
    column sep=-\pgflinewidth,
    }%
}

\begin{document}
\begin{tikzpicture}
    \node[hmul]{1 & 2 & 3 & 4\\};
\end{tikzpicture}

\begin{tikzpicture}
    \node[hmul]{1 & 2 & 3\\};
\end{tikzpicture}

\begin{tikzpicture}
    \node[hmul]{1 & 2\\};
\end{tikzpicture}

\begin{tikzpicture}
    \node[hmul]{1\\};
\end{tikzpicture}
\end{document}

enter image description here

2
  • Is it a screen resolution issue or is the line between 3 and 4 in the first line, and at the right of the 3 in the second line heavier (doubled?)?
    – sgmoye
    Jul 27, 2021 at 13:48
  • @sgmoye It was a screen resolution problem. I've changed the figure and now all lines show similar width.
    – Ignasi
    Jul 27, 2021 at 15:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .