TransWikia.com

Draw a hash table horizontally

TeX - LaTeX Asked by L. WD on February 1, 2021

How to create this design?

enter image description here

It’s possible with forest?

3 Answers

A solution with tikz:

enter image description here

documentclass{article}
usepackage{tikz}
usepackage{expl3}
usepackage{xparse}

begin{document}

newlength{cellsize}
setlength{cellsize}{6mm}

tikzset{
  cell/.style = {
    draw,
    anchor=west,
    minimum width=cellsize,
    minimum height=cellsize,
    text width=cellsize,
    inner sep=0pt,
    outer sep=0pt,
    line width=1pt,
    execute at begin node = {smallcentering}
  }
}

% current x coord
newlength{xcoord}
% current y coord
newlength{ycoord}

ExplSyntaxOn
tl_new:N l_template_tl
tl_set:Nn l_template_tl {
  node[cell,*1] at (*2) {*3};
}

DeclareDocumentCommand{drawnode}{O{}m}{
  % load template
  tl_set_eq:NN l_tmpb_tl l_template_tl
  % fill node parameters
  tl_set:Nn l_tmpa_tl {#1}
  regex_replace_once:nnN {*1} {u{l_tmpa_tl}} l_tmpb_tl
  
  % increment x coord
  dim_add:Nn xcoord {cellsize}
  % fill node location
  tl_set:Nx l_tmpa_tl {dim_use:N xcoord, dim_use:N ycoord}
  regex_replace_once:nnN {*2} {u{l_tmpa_tl}} l_tmpb_tl
  
  % fill node text
  tl_set:Nn l_tmpa_tl {#2}
  regex_replace_once:nnN {*3} {u{l_tmpa_tl}} l_tmpb_tl
  
  % use node
  tl_use:N l_tmpb_tl
}
ExplSyntaxOff

begin{tikzpicture}
% initialize x, y coords before drawing
setlength{xcoord}{0mm}
setlength{ycoord}{0mm}

drawnode[fill]{}
drawnode{1}
drawnode{2}
drawnode{3}
drawnode[draw=green]{4}
drawnode[draw=green]{5}
drawnode[draw=green]{6}

% draw another table
% initialize x, y coords before drawing
setlength{xcoord}{0mm}
setlength{ycoord}{-2cm}

drawnode[draw=orange,fill=orange]{}
drawnode{1}
drawnode{2}
drawnode{3}
drawnode[draw=red]{4}
drawnode[draw=blue]{5}
drawnode[draw=green]{6}
drawnode{7}
end{tikzpicture}

end{document}

Adding extra information

Use starred version of drawnode

enter image description here

documentclass{article}
usepackage{tikz}
usepackage{expl3}
usepackage{xparse}

begin{document}

newlength{cellsize}
setlength{cellsize}{6mm}

tikzset{
  cell/.style = {
    draw,
    anchor=west,
    minimum width=cellsize,
    minimum height=cellsize,
    text width=cellsize,
    inner sep=0pt,
    outer sep=0pt,
    line width=1pt,
    execute at begin node = {smallcentering}
  }
}

% current x coord
newlength{xcoord}
% current y coord
newlength{ycoord}

ExplSyntaxOn
tl_new:N l_template_tl
tl_set:Nn l_template_tl {
  node[cell,*1] at (*2) {*3};
}

DeclareDocumentCommand{drawnode}{sO{}m}{
  % load template
  tl_set_eq:NN l_tmpb_tl l_template_tl
  % fill node parameters
  tl_set:Nn l_tmpa_tl {#2}
  regex_replace_once:nnN {*1} {u{l_tmpa_tl}} l_tmpb_tl
  
  % increment x coord if it is not starred
  IfBooleanF{#1}{dim_add:Nn xcoord {cellsize}}
  % fill node location
  tl_set:Nx l_tmpa_tl {dim_use:N xcoord, dim_use:N ycoord}
  regex_replace_once:nnN {*2} {u{l_tmpa_tl}} l_tmpb_tl
  
  % fill node text
  tl_set:Nn l_tmpa_tl {#3}
  regex_replace_once:nnN {*3} {u{l_tmpa_tl}} l_tmpb_tl
  
  % use node
  tl_use:N l_tmpb_tl
}


ExplSyntaxOff

begin{tikzpicture}
% initialize x, y coords before drawing
setlength{xcoord}{0mm}
setlength{ycoord}{0mm}

drawnode[fill]{}
drawnode{1}
% starred command will not increment x coord
drawnode*[draw=none,yshift=5mm]{tiny id1}
drawnode*[draw=none,yshift=8mm]{tiny id2}
drawnode{2}
drawnode{3}
drawnode[draw=green]{4}
drawnode[draw=green]{5}
drawnode[draw=green]{6}
end{tikzpicture}

end{document}

Answered by Alan Xiang on February 1, 2021

As I mentioned in a comment, it can be done with a simple tabular, xcolor and hhline:

documentclass{article}
usepackage{hhline, array}
usepackage[table, svgnames]{xcolor}
usepackage{bigstrut} 


begin{document}

sffamilysetlength{bigstrutjot}{1ex}setlength{arrayrulewidth}{1pt}
begin{tabular}{*{4}{!{color{black}vrule width 1pt}wc{3.2mm}}*{3}{|wc{3.2mm}}|}
hhline{>{arrayrulecolor{black}}---->{arrayrulecolor{SeaGreen!60}}|---|<{arrayrulecolor{black}} }
cellcolor{black}bigstrut & 1 & 2 & 3 & 4 & 5 & 6 
hhline{>{arrayrulecolor{black}}---->{arrayrulecolor{SeaGreen!60}}|---|}
end{tabular}

end{document} 

enter image description here

Answered by Bernard on February 1, 2021

Using forest for this wouldn't be my first thought either, but as the OP asked for forest specifically ...

We use a phantom node as the root; all the other other nodes are children of the root. The overlapping-borders effect is achieved by setting the TikZ option outer xsep=0. (And the nodes are drawn thick.)

documentclass{article}
usepackage{forest}

begin{document}    
begin{forest}
  [,phantom,s sep=0,for descendants={draw,thick,outer xsep=0}
    [phantom{0},fill]
    [1]
    [2]
    [3]
    [4,for current and following siblings=green]
    [5]
    [6]
  ]
end{forest}
end{document}

The result for the original request

Control over the z-order of the nodes is possible using draw tree processing order. This is the nodewalk style telling forest in which order to draw nodes, see section 3.4.3 of the manual.

It is easiest to define a nodewalk using the .nodewalk style handler, see section 3.8.8 of the manual. To get the crazy-looking c1b7b4b5b6b2b3, write down the order in which you want to draw the nodes (1745623; each number means "move to the Nth child"; the nodes are renumbered in this example to match the child numbers), separate the numbers by b (move back (using a fake step)), and precede the entire thing by c (current; as the root is a phantom node, this does nothing, really, but it gives us a node to return to by b). In this particular case, where each back is moving up to the parent (u), 1u7u4u5u6u2u3 would work just as well.

documentclass{article}
usepackage{forest}

begin{document}
begin{forest}
  draw tree processing order/.nodewalk style=c1b7b4b5b6b2b3,
  [,phantom,s sep=0,for descendants={draw,thick,outer xsep=0}
    [1,fill,text=white]
    [2,red]
    [3,green]
    [4,orange]
    [5,blue]
    [6,yellow]
    [7,brown]
  ]
end{forest}
end{document}

Controlling the z-order of nodes

Answered by Sašo Živanović on February 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP