答案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 style
,请参阅手册第 3.8.8 节。要获得看起来很疯狂的c1b7b4b5b6b2b3
,请写下您想要绘制节点的顺序(1745623
;每个数字表示“移动到第 N 个子节点”;在此示例中,节点重新编号以匹配子节点编号),用b
(向后移动(使用假步骤))分隔数字,并在整个节点前面加上c
(当前;由于根节点是幻影节点,因此这实际上什么也不做,但它为我们提供了一个通过 可以返回的节点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}