2

In a multipart rectangle i want to reduce the space between the parts. For a horizontally splitted rectangle i achieved that with an \hskip after the \nodepart command.

For a vertically splitted rectangle i tried it analogously and replaced \hskip with \vskip. Unfortunately the compiler doesn't like \vskip here an ends with an error. The appended code illustrates that.

Why vskip doesn't work in my example and how could i alternatively reduce the space between vertically splitted parts of the rectangle?


\documentclass{standalone}

\newcommand{\NodeTwoColsH}[2]{%
   #1\nodepart{two}\hskip -5pt #2%
} 

\newcommand{\NodeTwoColsV}[2]{%
   #1\nodepart{two}\vskip -5pt #2%
} 

\usepackage{tikz}
\usetikzlibrary{shapes.multipart}

\begin{document}
\begin{tikzpicture}[
         sibling distance = 2.5cm,
         every node/.style = {
            draw
         },
         split/.style = {
            rectangle split draw splits=false,
            rectangle split,
            draw,
            rectangle split parts=2
         },
         v split/.style = {
            split
         },
         h split/.style = {
            split,
            rectangle split horizontal
         }
   ]

   \node{root}
   child { node[h split] {\NodeTwoColsH{Left}{Right}} }
   child { node[h split] {\NodeTwoColsH{Left}{Right}} 
      child { node[v split]  {\NodeTwoColsV{Up}{Down}} }
      child { node[v split]  {\NodeTwoColsV{Up}{Down}} }
   };

\end{tikzpicture}
\end{document}
3

Under ordinary circumstances node contents are put in a TeX horizontal box. But horizontal boxes cannot contain commands like \vskip so

\hbox{A \vskip-5pt B}

will produce an error. This is because TeX is in "internal horizontal mode" inside the \hbox and vertical commands (e.g., \vskip) will produce an error (a useful reference for this kind of stuff is "TeX by Topic" by Victor Eijkhout which is freely available on the internet).

But how does this help with TikZ? I suppose the cleanest way to get \vskip to work inside a node is to set the text width of the node so that the contents are first placed inside a vertical box where \vskip can be used.

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

\begin{document}
\begin{tikzpicture}[every node/.style={
  rectangle split, 
  rectangle split draw splits=false, 
  rectangle split parts=2, draw, text width=1.5cm}]

\node at (0,0) {Top\nodepart{two} Bottom};
\node at (2,0) {Top\nodepart{two}\vskip-10pt Bottom};
\end{tikzpicture}
\end{document}

enter image description here

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.