해시 테이블을 가로로 그리기

해시 테이블을 가로로 그리기

이 디자인을 만드는 방법은 무엇입니까?

여기에 이미지 설명을 입력하세요

으로 가능합니까 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. 이것은 숲에 노드를 그리는 순서를 알려주는 nodewalk 스타일입니다. 매뉴얼의 섹션 3.4.3을 참조하세요.

핸들러를 사용하여 nodewalk를 정의하는 것이 가장 쉽습니다 .nodewalk style. 매뉴얼의 섹션 3.8.8을 참조하십시오. 미친 모양을 얻으려면 c1b7b4b5b6b2b3노드를 그리려는 순서를 적고( 1745623각 숫자는 "N번째 하위 항목으로 이동"을 의미합니다. 이 예에서는 하위 번호와 일치하도록 노드 번호가 다시 지정됨) 숫자를 다음과 같이 구분합니다. b(뒤로 이동(가짜 단계 사용)), 전체 항목 앞에 c(현재; 루트가 팬텀 노드이므로 이는 실제로 아무 작업도 수행하지 않지만 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 순서 제어

관련 정보