2

I have a node with the shape rectangle split. In it, the nodepart with "32" is the main part, the rest are annotations for it.

Question: How do I get the nodepart with "32" to be a square?

The other split parts should remain rectangular and be of the lowest possible height:
· so that's why I used \scalebox{0.25}{...}, I do not know, whether there is a better way or a more TikZ-like way....
· By the way: the value 0.25 does not have to be fixed.

enter image description here

\documentclass[margin=5pt, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes}

\begin{document} \begin{tikzpicture}[font=\footnotesize\sffamily, very thin, ] \node [rectangle split, rectangle split parts=7, draw, anchor=center, draw, anchor=text, inner ysep=0.5pt, %inner xsep=0.5pt, minimum width=5mm, minimum height=5mm, % seems to have no effect... rectangle split part fill={brown, orange, yellow, green, blue!33, red, lightgray}, name=mynode]{ \nodepart{four}\scalebox{0.25}{high (3/9)} \nodepart{five}\scalebox{0.25}{even (1/9)} \nodepart{six}\color{white}32 \nodepart{seven}\scalebox{0.225}{1. Place}};

\draw[<-, thick, purple] (mynode.six east) -- +(0.3,0) node[right, align=left, draw, font=\tiny, inner sep=1pt]{How to get a \ \bfseries squared 'nodepart' \ here? \ (The others can \ stay rectangled.)}; \end{tikzpicture} \end{document}

cis
  • 8,073
  • 1
  • 16
  • 45
  • From the manual: “When split vertically, the rectangle split will meet any minimum width requirements, but any minimum height will be ignored. Conversely when split horizontally, minimum height requirements will be met, but any minimum width will be ignored. In addition, inner sep is applied to every part that is used, so it cannot be specified independently for a particular part.” – Qrrbrbirlbel May 01 '23 at 20:54
  • The options text height and text depth don't even work for nodeparts which makes an automatic approach very hard. Does it need to be a rectangle split node? We can do almost the same with a matrix and individual nodes but the matrix can't be transformed (not even rotated) which the rectangle split shape would allow. – Qrrbrbirlbel May 01 '23 at 21:40

4 Answers4

4

As an alternative to a rectangle split you can use a matrix of nodes. In this case you can fix options for every particular part of the matrix like colors and fonts. You only need to fix a minimum width for all nodes because a every node size is independent for each one.

\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{matrix}

\begin{document} \begin{tikzpicture}

\matrix (A) [matrix of nodes, nodes in empty cells, row sep=-\pgflinewidth, nodes={draw, anchor=center, minimum width=2cm}]{ |[fill=brown]|\ |[fill=orange]|\ |[fill=yellow]|\ |[fill=green, font=\sffamily\small]| high (3/9)\ |[fill=cyan, font=\sffamily\small]| even (1/9)\ |[fill=red, minimum size=2cm, font=\bfseries\sffamily\Huge, text=white]| 32\ |[fill=gray, font=\sffamily\small]| 1. Place\ }; \end{tikzpicture} \end{document}

enter image description here

Ignasi
  • 136,588
3

Here's a way to do it, because after all we are setting types:

  • put a rule inside the nodes text
  • width=0pt (or invisible), by eye: height=1.5cm, shifted down by -.58cm
  • \textbf{\rule[-.58cm]{0pt}{1.5cm}\textcolor{white}{\LARGE{32}}}

"By eye" can be replaced by calculations, somehow.

result

\documentclass[10pt,border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}

\begin{document}

\begin{tikzpicture} \node [rectangle split, rectangle split parts=4,draw, rectangle split part fill= {red!80!blue,yellow,red,white}] { high (3/9) \nodepart{two} even (1/9) \nodepart{three} \textbf{\rule[-.58cm]{0pt}{1.5cm}\textcolor{white}{\LARGE{32}}} \nodepart{four} 1. Place }; \end{tikzpicture}

\end{document}

MS-SPO
  • 11,519
3

Interacting with the node parts will be tricky.

Here's a workaround with a matrix with only one cell so that we can use the whole construct as if it where one node (which it technically also is). However, a matrix can't be transformed, not even rotated.

You can however use the matrix anchor key to reference an anchor of a node inside the matrix to place the matrix so that a sub-node is placed where you need it.

I'm using my ext.node-families library so that all subnodes will have the same width. The undocumented macro \tikzextnodefamiliesgetwidth macro is used to set the (minimum) height to the same value as the width of all nodes in that matrix. (If there's a change the square node is on itself higher than the width of all nodes, more work is necessary.)

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{ext.node-families}
\tikzset{
  tight matrix/.style={
    every outer matrix/.append style={inner sep=+0pt, outer sep=+0pt}},
  @Rectangle Split part fill/.code={%
    \edef\pgfmathcounter{\the\numexpr\pgfmathcounter+1\relax}%
    \typeout{\pgfmathcounter: #1}%
    \tikzset{Rectangle Split \pgfmathcounter/.append style={fill=#1}}},
  Rectangle Split part fill/.style={%
    /utils/exec=\def\pgfmathcounter{0},
    @Rectangle Split part fill/.list={#1}},
  Rectangle Split/.style={
    matrix, tight matrix, path only, shape=rectangle,
    node contents={
      \coordinate(@);
      \foreach[count=\I] \T in {#1}
        \node[
          name=\tikzmatrixname-\I,
          node family/width=\tikzmatrixname,
          every Rectangle Split node/.try,
          Rectangle Split \I/.try,
          alias=@, at=(@.south), anchor=north, yshift=+.5\pgflinewidth]{\T};
      \\}}}
\begin{document}
\tikz
  \node[
    every Rectangle Split node/.style=draw, % probably a good default
    Rectangle Split part fill={             % almost the same
      brown, orange, yellow, green, blue!33, red, lightgray},
    Rectangle Split 6/.append style={
      text=white,
      minimum height=\tikzextnodefamiliesgetwidth{\tikzmatrixname}},
    Rectangle Split={,,,high (3/9), even (1/9), 32, 1. Place}];
\end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821
1

I made the square with a rule into a phantom and overlay the text with another node:

enter image description here

\documentclass[]{book}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}

\begin{document} \pgfmathsetlengthmacro\InnerSep{0.5pt} \pgfmathsetlengthmacro\squarelength{10.24mm-2*\InnerSep} \begin{tikzpicture}[font=\tiny\sffamily, very thin] \node (mynode)[ rectangle split, rectangle split parts=4, rectangle split ignore empty parts, draw, align=center,
text width=\squarelength, inner sep=\InnerSep, rectangle split part fill={orange, yellow, red, lightgray} ]{ \nodepart{one} first \nodepart{two} even \hfill \hspace{-2mm}(2/9) \nodepart{three} \phantom{\rule[-\squarelength]{\squarelength}{\squarelength}} \nodepart{four} 1. Place };

\path[draw=none] (mynode.two split west) -- (mynode.three split east) node[midway, font=\sffamily\Huge\bfseries, text=white]{32};

% Test-Square \draw[cyan] ([shift={(\pgflinewidth,\pgflinewidth)}]mynode.three split west) rectangle +(\squarelength+2\InnerSep,\squarelength+2\InnerSep) node[right]{Test-Square}; \end{tikzpicture} \end{document}

cis
  • 8,073
  • 1
  • 16
  • 45