我正在尝试使用 TikZ 构建具有一些控制流的框图。图中的块应像六边形的顶点一样对齐。此外,宏\centering
对我来说不起作用。请给我一些建议,使整个图片居中并改善块之间的对齐。
\documentclass{article}
\usepackage{amsmath}
\usepackage{biblatex}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{tikz}
\usepackage{varwidth}
\usetikzlibrary{shapes.multipart, arrows}
\tikzstyle{nodest} = [draw, rounded corners, very thick]
\begin{document}
\centering
\begin{tikzpicture} [node distance=5cm, auto, >=latex']
\node (inputnode)[nodest, align=center, rectangle split, rectangle split draw splits=false] {
\textbf{Input}
\nodepart{two}
$(A, 506, 90)$\\
$(B, 590, 86)$\\
$(A, 590, 70)$
\nodepart{three}
$(C, 506, 23)$\\
$(C, 590, 45)$\\
$(B, 506, 67)$
};
\node [below of=inputnode] {\begin{varwidth}{15em}\Large{partitions of input tuples are sent to mappers}\end{varwidth}};
\draw[thick] (inputnode.text split west) -- (inputnode.text split east);
\draw[dashed] (inputnode.second split west) -- (inputnode.second split east);
\node (map1node)[nodest, rectangle split, rectangle split draw splits=false, right of=inputnode, yshift=2.5cm] {
\textbf{Mapper 1}
\nodepart{two} $key = 506, value = (A, 90)$
\nodepart{three} $key = 590, value = (B, 86)$
\nodepart{four} $key = 590, value = (A, 70)$
};
\draw[->, thick] (inputnode.two east) -- (map1node);
\node (map2node)[nodest, rectangle split, rectangle split draw splits=false, below of=map1node] {
\textbf{Mapper 2}
\nodepart{two} $key = 506, value = (C, 23)$
\nodepart{three} $key = 590, value = (C, 45)$
\nodepart{four} $key = 506, value = (B, 67)$
};
\draw[->, thick] (inputnode.three east) -- (map2node);
\node [below of=map2node] {\begin{varwidth}{15em}\Large{Mappers}\end{varwidth}};
\node (reduce1node) [nodest, rectangle split, rectangle split parts=5, rectangle split draw splits=false, right of=map1node, xshift=2cm] {
\textbf{Reducer 1}
\nodepart{two} $key = 506$
\nodepart{three} $(A, 90)$
\nodepart{four} $(C, 23)$
\nodepart{five} $(B, 67)$
};
\draw[->, dashed] (map1node.two east) -- (reduce1node.three west);
\draw[->, dashed] (map2node.two east) -- (reduce1node.four west);
\draw[->, dashed] (map2node.four east) -- (reduce1node.five west);
\node (reduce2node) [nodest, rectangle split, rectangle split parts=5, rectangle split draw splits=false, right of=map2node, xshift=2cm] {
\textbf{Reducer 2}
\nodepart{two} $key = 590$
\nodepart{three} $(B, 86)$
\nodepart{four} $(A, 70)$
\nodepart{five} $(C, 45)$
};
\draw[->, dashed] (map1node.three east) -- (reduce2node.three west);
\draw[->, dashed] (map1node.four east) -- (reduce2node.four west);
\draw[->, dashed] (map2node.three east) -- (reduce2node.five west);
\node [below of=reduce2node] {\begin{varwidth}{15em}\Large{Reducers}\end{varwidth}};
\end{tikzpicture}
\end{document}
答案1
您可以用六边形节点将它们精确地放在六边形的顶点上。我已经画好了,但您可以随时删除该draw
选项。
\documentclass{article}
\usepackage{amsmath}
\usepackage{biblatex}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{tikz}
\usepackage{varwidth}
\usetikzlibrary{shapes.multipart,shapes.geometric, arrows}
\tikzstyle{nodest} = [draw, rounded corners, very thick]
\begin{document}
{
\centering
\begin{tikzpicture} [node distance=5cm, auto, >=latex']
\node[regular polygon,regular polygon sides=6,minimum height=10 cm,draw] (hexag) {};
\node (inputnode)[nodest, align=center, rectangle split, rectangle split draw splits=false] at (hexag.corner 3){
\textbf{Input}
\nodepart{two}
$(A, 506, 90)$\\
$(B, 590, 86)$\\
$(A, 590, 70)$
\nodepart{three}
$(C, 506, 23)$\\
$(C, 590, 45)$\\
$(B, 506, 67)$
};
%\node at (hexag.corner 4) {\begin{varwidth}{15em}\Large{partitions of input tuples are sent to mappers}\end{varwidth}};
\draw[thick] (inputnode.text split west) -- (inputnode.text split east);
\draw[dashed] (inputnode.second split west) -- (inputnode.second split east);
\node (map1node)[nodest, rectangle split, rectangle split draw splits=false] at (hexag.corner 2){
\textbf{Mapper 1}
\nodepart{two} $key = 506, value = (A, 90)$
\nodepart{three} $key = 590, value = (B, 86)$
\nodepart{four} $key = 590, value = (A, 70)$
};
\draw[->, thick] (inputnode.two east) -- (map1node);
\node (map2node)[nodest, rectangle split, rectangle split draw splits=false] at (hexag.corner 4){
\textbf{Mapper 2}
\nodepart{two} $key = 506, value = (C, 23)$
\nodepart{three} $key = 590, value = (C, 45)$
\nodepart{four} $key = 506, value = (B, 67)$
};
\draw[->, thick] (inputnode.three east) -- (map2node);
\node [below of=map2node] {\begin{varwidth}{15em}\Large{Mappers}\end{varwidth}};
\node (reduce1node) [nodest, rectangle split, rectangle split parts=5, rectangle split draw splits=false] at (hexag.corner 1) {
\textbf{Reducer 1}
\nodepart{two} $key = 506$
\nodepart{three} $(A, 90)$
\nodepart{four} $(C, 23)$
\nodepart{five} $(B, 67)$
};
\draw[->, dashed] (map1node.two east) -- (reduce1node.three west);
\draw[->, dashed] (map2node.two east) -- (reduce1node.four west);
\draw[->, dashed] (map2node.four east) -- (reduce1node.five west);
\node (reduce2node) [nodest, rectangle split, rectangle split parts=5, rectangle split draw splits=false] at (hexag.corner 5){
\textbf{Reducer 2}
\nodepart{two} $key = 590$
\nodepart{three} $(B, 86)$
\nodepart{four} $(A, 70)$
\nodepart{five} $(C, 45)$
};
\draw[->, dashed] (map1node.three east) -- (reduce2node.three west);
\draw[->, dashed] (map1node.four east) -- (reduce2node.four west);
\draw[->, dashed] (map2node.three east) -- (reduce2node.five west);
\node [below of=reduce2node] {\begin{varwidth}{15em}\Large{Reducers}\end{varwidth}};
\end{tikzpicture}
}
\end{document}
我不知道如何处理其中一个节点,所以它丢失了。