我正在排版一些表示类似于以下算法的排序算法的图表Miller 和 Ranum Python 书部分是作为学习 TikZ 的练习。我正在尝试排版类似于合并排序图的内容:
分裂阶段:
合并阶段
我从一个多部分矩形开始,但阴影覆盖了填充的节点(除非是白色) - 在第二行,我尝试了单独的节点,但这会导致字体(例如 Lucida)出现问题,其中并非所有数字都具有相同的高度。
以下是这些部分的代码:
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{shapes.multipart}
\usetikzlibrary{shadows}
\usetikzlibrary{positioning}
\newlength{\OUmatrixSepWidth}
\newcommand{\OUsetMatrixSepWidth}[1]{%
\settowidth{\OUmatrixSepWidth}{#1}%
}
\renewcommand{\familydefault}{\sfdefault}
\thispagestyle{empty}
\begin{document}
\Large
\OUsetMatrixSepWidth{3em}
\begin{tikzpicture}
[myInnerBlock/.style={rectangle,draw,align=center}
,myInnerBlockL/.style={rectangle,draw,align=center,fill=red!20}
,myInnerBlockR/.style={rectangle,draw,align=center,fill=blue!20}
,myRectangleSplit/.style={rectangle split,rectangle split horizontal,draw,rectangle split part align={center,base}} % requires library shapes.multipart
,line/.style={draw,thick,red,-triangle 60}
]
\matrix[column sep=0.3\OUmatrixSepWidth, row sep=0.5\OUmatrixSepWidth]
{
% row 1
\node[myRectangleSplit,rectangle split allocate boxes=10,rectangle split parts=10,drop shadow,rectangle split part fill={red!20,red!20,red!20,red!20,red!20,blue!20}] (unsorted)
{ 3\nodepart{two}0\nodepart{three}1\nodepart{four}8\nodepart{five}7\nodepart{six}2\nodepart{seven}5\nodepart{eight}4\nodepart{nine}9\nodepart{ten}6}; \\
% row 2
\node (r2ac) {};
\node[myInnerBlockR,left=0.3\OUmatrixSepWidth of r2ac] (r2a5) {7};
\node[myInnerBlockR,left=0pt of r2a5] (r2a4) {8};
\node[myInnerBlockR,left=0pt of r2a4] (r2a3) {1};
\node[myInnerBlockL,left=0pt of r2a3] (r2a2) {0};
\node[myInnerBlockL,left=0pt of r2a2] (r2a1) {3};
\node[myInnerBlockL,right=0.3\OUmatrixSepWidth of r2ac] (r2a6) {2};
\node[myInnerBlockL,right=0pt of r2a6] (r2a7) {5};
\node[myInnerBlockR,right=0pt of r2a7] (r2a8) {4};
\node[myInnerBlockR,right=0pt of r2a8] (r2a9) {9};
\node[myInnerBlockR,right=0pt of r2a9] (r2a10) {6};
\\
% row 3
% row 4
% row 5
};
\end{tikzpicture}
\end{document}
看起来像:
多部分矩形看起来会更好,但如何让阴影不覆盖矩形?有没有我找不到的更好的方法?树形结构会更简单吗?更不用说得到直角的线条了。
答案1
使用您的代码和此处的答案:为重叠的物体绘制适当的阴影,并调整shadow yshift
键,您就可以正确绘制阴影。有关阴影的更多信息,请参阅 PGF 手册第 415 页。
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{shapes.multipart}
\usetikzlibrary{shadows}
\usetikzlibrary{positioning}
\newlength{\OUmatrixSepWidth}
\newcommand{\OUsetMatrixSepWidth}[1]{%
\settowidth{\OUmatrixSepWidth}{#1}%
}
\renewcommand{\familydefault}{\sfdefault}
\thispagestyle{empty}
\pgfdeclarelayer{back}
\pgfsetlayers{back,main}
\makeatletter
\pgfkeys{%
/tikz/on layer/.code={
\pgfonlayer{#1}\begingroup
\aftergroup\endpgfonlayer
\aftergroup\endgroup
},
/tikz/node on layer/.code={
\pgfonlayer{#1}\begingroup
\expandafter\def\expandafter\tikz@node@finish\expandafter{\expandafter\endgroup\expandafter\endpgfonlayer\tikz@node@finish}%
},
}
\makeatother
\begin{document}
\Large
\OUsetMatrixSepWidth{3em}
\begin{tikzpicture}
[myInnerBlock/.style={rectangle,draw,align=center}
,myInnerBlockL/.style={rectangle,draw,align=center,fill=red!20}
,myInnerBlockR/.style={rectangle,draw,align=center,fill=blue!20}
,myRectangleSplit/.style={rectangle split,rectangle split horizontal,draw,rectangle split part align={center,base}} % requires library shapes.multipart
,line/.style={draw,thick,red,-triangle 60}
]
\matrix[column sep=0.3\OUmatrixSepWidth, row sep=0.5\OUmatrixSepWidth]
{
% row 1
\node[myRectangleSplit,rectangle split allocate boxes=10,rectangle split parts=10,drop shadow={on layer=back,shadow yshift
=3ex},rectangle split part fill={red!20,red!20,red!20,red!20,red!20,blue!20}] (unsorted)
{ 3\nodepart{two}0\nodepart{three}1\nodepart{four}8\nodepart{five}7\nodepart{six}2\nodepart{seven}5\nodepart{eight}4\nodepart{nine}9\nodepart{ten}6}; \\[2ex]
% row 2
\node (r2ac) {};
\node[myInnerBlockR,left=0.3\OUmatrixSepWidth of r2ac] (r2a5) {7};
\node[myInnerBlockR,left=0pt of r2a5] (r2a4) {8};
\node[myInnerBlockR,left=0pt of r2a4] (r2a3) {1};
\node[myInnerBlockL,left=0pt of r2a3] (r2a2) {0};
\node[myInnerBlockL,left=0pt of r2a2] (r2a1) {3};
\node[myInnerBlockL,right=0.3\OUmatrixSepWidth of r2ac] (r2a6) {2};
\node[myInnerBlockL,right=0pt of r2a6] (r2a7) {5};
\node[myInnerBlockR,right=0pt of r2a7] (r2a8) {4};
\node[myInnerBlockR,right=0pt of r2a8] (r2a9) {9};
\node[myInnerBlockR,right=0pt of r2a9] (r2a10) {6};
\\
% row 3
% row 4
% row 5
};
\end{tikzpicture}
\end{document}
结果: