显示如下两张图片中的内容对于解释遗传算法的工作原理很有帮助。
在这些图片中,另外两个功能可能会很有用。
- 在开始和/或结束处显示虚线水平线,以表明数据未完全显示。
- 拥有一种简单的方法来改变背景颜色,以指示所使用的不同技术。
如何才能实现这一切呢?
以下是一些用于启动增强功能的代码。此代码来自这一页。
\documentclass{article}
\usepackage{tikz}
\usepackage{xstring}
\tikzset{
raw sort entry/.style={rectangle, thick, draw, node distance=1.5em},
sort entry black/.style={raw sort entry, black, fill=white},
sort entry blackgray/.style={raw sort entry, black, fill=gray!25},
s1/.style={raw sort entry, red, fill=yellow!30},
s2/.style={raw sort entry, blue, fill=green!20},
s3/.style={raw sort entry, violet, fill=orange!25}
}
\newcommand*{\List}[2][sort entry black]{%
\par\noindent%
\edef\listtoprocess{#2}%
\def\ListToProcess{}%
\begin{tikzpicture}[inner sep=2pt, outer sep=0]
\foreach \content in \listtoprocess{
\IfSubStr{\content}{/}{% true
\xdef\ListToProcess{\ListToProcess,\content}
}{% false
\xdef\ListToProcess{\ListToProcess,#1/\content}
}
}
\StrGobbleLeft{\ListToProcess}{1}[\ListToProcess]% removes the first comma (\listToProcess is empty at the start)
\foreach [count=\i] \Style/\Value in \ListToProcess {
\ifnum\i=1\relax
\node [raw sort entry, \Style] (sortnode\i) {\Value};
\else
\node [raw sort entry, right of=sortnode\number\numexpr\i-1\relax, \Style] (sortnode\i) {\Value};
\fi
}
\end{tikzpicture}%
}
\begin{document}
\List{1, 1, 1, 1, 1, s1/2, s1/2, s1/2, 3, 3}
\end{document}
答案1
这可以给你一个开始。
当然,绘制垂直线的方法并不是很复杂。也许\setLines
类似于pgfgantt
我的另一个答案这可能是一种更好的解决方案。
此外,第一行之后所需的内容也\tikzset
可以得到更好的解决。这是一个元级别的问题,因为我们确实有一系列的链。TikZ 以更好的方式解决了实际链的问题。
代码
\documentclass[tikz,convert=false]{standalone}
\usetikzlibrary{chains}
\makeatletter
\tikzset{
on chain/.append code={
\ifnum\c@pgf@counta=1\relax
\tikzset{every first on chain/.try}%
\fi
},
start chain/.append code={%
\edef\pgf@marshal{\noexpand\tikzset{execute at end scope={\noexpand\xdef\noexpand\tikzpreviouschain{\tikz@lib@chain@name}}}}%
\pgf@marshal
}
}
\def\listset{\pgfqkeys{/List}}
\tikzset{
raw sort entry/.style={rectangle, thick, draw, font=\strut,text depth=+0pt,minimum width=+1.2em, inner xsep=+0pt},
sort entry black/.style={raw sort entry, black, fill=white},
sort entry blackgray/.style={raw sort entry, black, fill=gray!25},
s1/.style={raw sort entry, fill=yellow!30},
s2/.style={raw sort entry, fill=green!20},
s3/.style={raw sort entry, fill=orange!25},
g/.style={raw sort entry, fill=lightgray}
}
\listset{
chain name/.initial=,
do/.style={
/utils/exec={\begin{scope}[start chain=\pgfkeysvalueof{/List/chain name}]},
int/do/.list={#1},
/utils/exec=\end{scope},
},
int/do/.code={%
\pgfutil@ifnextchar[\tikz@List@int@@do{\tikz@List@int@@do[]}#1\pgf@stop
}
}
\def\tikz@List@int@@do[#1]#2\pgf@stop{%
\node[on chain={\pgfkeysvalueof{/List/chain name}},#1] {#2};
}
\begin{document}
\begin{tikzpicture}[
node distance=+1em and +0pt,
outer xsep=+0pt,
outer ysep=+0pt,
every node/.append style={raw sort entry}
]
\listset{chain name=r1,do={1, [g]z, 1, 1, 1, [s1]2, [s2]2, [s3]2, 3, 3, 1, 1, [g]y, 1, 1, [s3]2, [s1]2, [s2]2, 3, 3}}
\tikzset{every first on chain/.style={below=of \tikzpreviouschain-begin}}
\listset{chain name=r2, do={1, 1, [g]y, 1, 1, [s3]2, [s1]2, [s2]2, 3, 3, 1, 1, 1, [g]x, 1, [s2]2, [s3]2, [s1]2, 3, 3}}
\listset{chain name=r3, do={1, 1, 1, [g]x, 1, [s2]2, [s3]2, [s1]2, 3, 3, 1, [g]z, 1, 1, 1, [s1]2, [s2]2, [s3]2, 3, 3}}
\foreach \chain in {r1, r2, r3}{
\draw[thick, densely dotted] (\chain-begin.north west) -- ++ (left:10pt)
(\chain-begin.south west) -- ++ (left:10pt)
(\chain-end.north east) -- ++ (right:10pt)
(\chain-end.south east) -- ++ (right:10pt)
;
}
\foreach \column in {0,5,10,...,20}{
\ifnum\column=0
\draw[dash pattern=on \pgflinewidth off \pgflinewidth] ([yshift=1em]r1-begin.north west) -- ([yshift=-1em]r3-begin.south west);
\else\ifodd\column
\draw[densely dotted] ([yshift=.75em]r1-\column.north east) -- ([yshift=-.75em]r3-\column.south east);
\else
\draw[dash pattern=on \pgflinewidth off \pgflinewidth] ([yshift=1em]r1-\column.north east) -- ([yshift=-1em]r3-\column.south east);
\fi
\fi}
\end{tikzpicture}
\end{document}