How can we produce this on latex using tikz:

enter image description here

I am not quite sure how to produce this using tikz, especially the chaining of the boxes below.


One possibility is to use the chains and positioning libraries for that.

\usetikzlibrary{chains,positioning} %

\begin{tikzpicture}[start chain=1 going right,every node/.style={draw,minimum
\begin{scope}[node distance=-\pgflinewidth,thick]
\foreach \X [count=\Y] in {0,...,15}
{\node [on chain=1] (X\X) {\X};}
\begin{scope}[node distance=2mm]
\node[below=of X1] (Y23){23};
\node[below=of Y23] (Y57){57};
\draw (X1) -- (Y23) -- (Y57);
\node[below=of X4] (Y4){4};
\draw (X4) -- (Y4);
\node[below=of X7] (Y12){12};
\node[below=of Y12] (Y31){31};
\node[below=of Y31] (Y34){34};
\draw (X7) -- (Y12) -- (Y31) -- (Y34);
\node[below=of X10] (Y8){8};
\draw (X10) -- (Y8);
\node[below=of X14] (Y15){15};
\node[below=of Y15] (Y21){21};
\draw (X14) -- (Y15) -- (Y21);
\node[left=of X0,draw=none] {$h(x)$};

enter image description here

  • 1
    How would you fix this node alignment in this example: puu.sh/AmrdI/be41a92e85.png, the nodes are overlapping? – bigfocalchord May 15 '18 at 4:36
  • @bigfocalchord You need to make the nodes wider, i.e. adjust the minimum width=0.6cm key. – user121799 May 15 '18 at 12:04

This is essentially marmot's answer but using the \chainin command to start a chain from an existing node to make the code more concise:

\scoped[start chain=going right, node distance=-0.1ex]{
  \node[on chain] {$h(x)$};
  \foreach \i in {0,...,15}
    \node [on chain, line width=0.2ex, minimum width=5ex, draw] (n-\i) {\i};
\foreach \i/\J in {1/{23,57},4/{4},7/{12,31,34},8/{204},10/{8},14/{15,29}}{
\scoped[start chain=going below, node distance=0.75ex]{
  \chainin (n-\i);
  \foreach \j in \J 
    \node [on chain, join, minimum width=3ex, draw] {\j};

enter image description here

  • Does this fix the problem I commented on the other pic? – bigfocalchord May 15 '18 at 7:41
  • @bigfocalchord, make nodes in the main chain wider. – Zarko May 15 '18 at 8:03

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.