水平繪製哈希表

水平繪製哈希表

如何創建這個設計?

在此輸入影像描述

有可能嗎forest

答案1

解決方案tikz

在此輸入影像描述

\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 = {\small\centering}
  }
}

% 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}

添加額外信息

使用有星號的版本\drawnode

在此輸入影像描述

\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 = {\small\centering}
  }
}

% 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}

答案2

正如我在評論中提到的,它可以透過簡單的表格、xcolor 和 hhline 來完成:

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


\begin{document}

\sffamily\setlength{\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} 

在此輸入影像描述

答案3

為此使用森林也不是我的第一個想法,但由於OP特別要求森林...

我們使用幻影節點作為根;所有其他節點都是根的子節點。重疊邊框效果是透過設定 TikZ 選項來實現的outer xsep=0。 (並繪製節點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}

原始請求的結果

可以使用 控制節點的 z 順序draw tree processing order。這是節點行走風格,告訴森林以什麼順序繪製節點,請參考手冊的第 3.4.3 節。

使用處理程序定義節點行走是最簡單的.nodewalk style,請參閱手冊的第 3.8.8 節。為了獲得瘋狂的外觀c1b7b4b5b6b2b3,請寫下要繪製節點的順序(1745623;每個數字表示「移動到第 N 個子節點」;在本範例中,節點被重新編號以匹配子節點數字),將數字分隔開b(向後移動(使用假步驟)),並在整個事物之前加上c(current; 由於根是一個幻像節點,這實際上什麼也不做,但它給了我們一個可以返回到的節點 by b)。在這種特殊情況下,每個back都向上移動到父級 ( u),1u7u4u5u6u2u3也同樣有效。

\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}

控制節點的 z 順序

相關內容