我想用 绘制一个算法图Tikz
。在这个图中,我想显示哪个子程序调用了哪个子程序。并且该图可以从左上角到右下角阅读。
我在网上寻找一种绘制这种算法图的好方法(我想要的),并找到了这:
我浏览了 pgf 文档并进行了一些测试:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs}
\begin{document}
\begin{tikzpicture}[every node/.style = draw]
\graph [grow right sep, left anchor=south, right anchor=west,
grow down=7mm,grow right=7mm]{
root -> {
child 1,
child 2 -> {
grand child 1,
grand child 2
},
child 3 -> {
grand child 3
}
}
};
\end{tikzpicture}
\end{document}
说实话,我对这个库和图形选项一点都不熟悉。但我确信必须对它们进行修改才能创建如我给出的示例那样的图表(请参阅上面的链接)。
有人知道怎么做吗?您能否就我需要修复的图表选项提供一些建议?
答案1
基本框架:http://www.texample.net/tikz/examples/filesystem-tree/
看看 21. 让树木生长PGF 手册。
这个解决方案是我第一次尝试,因此并不完美。
您可以创建nodes
和childs
。级别之间的距离可以用定义level distance=<length>
。节点和子节点之间的连接用设置edge from parent path={[->] ([xshift=.5em]\tikzparentnode.south west) |- (\tikzchildnode.west)}]
。(xshift
是可选的)。您还可以添加dashed
。
儿童位置的设置请grow via three points={...}
参见第 72.1 章“生长功能”PGF 手册。对于空格/空白子项,您可以添加child [missing] {}
。
可选地,您也可以将节点设置为dashed
和非solid
或其他内容。
梅威瑟:
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\tikzstyle{every node}=[draw=black,thick,anchor=west]
\begin{tikzpicture}[%
parent anchor=south west,
level distance=40mm,
grow via three points={one child at (0.1,-1) and
two children at (0.1,-1) and (0.1,-2)},
%edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}]
edge from parent path={[->] ([xshift=.5em]\tikzparentnode.south west) |- (\tikzchildnode.west)}]
\node { \_start}
child { node {main}
child[%
grow via three points={one child at (0.1,-1) and
two children at (0.1,-1) and (0.1,-2)},
edge from parent path={[->] ([xshift=.5em]\tikzparentnode.south west) |- (\tikzchildnode.west)}] { node {\_\_function}
child[%
grow via three points={one child at (-0.5,-1) and
two children at (-0.5,-1) and (-1.2,-1)},
edge from parent path={[->] ([xshift=.5em]\tikzparentnode.south west) |- (\tikzchildnode.west)}] { node {\_\_mt\_MasterFunction\_}
child[%
grow=right, edge from parent path={[->,dashed] (\tikzparentnode.east) |- (\tikzchildnode.west)}] { node {\_thread\_start}
child[%
grow via three points={one child at (-0.5,-1) and
two children at (-0.5,-1) and (-0.5,-2)},
edge from parent path={[->,solid] ([xshift=.5em]\tikzparentnode.south west) |- (\tikzchildnode.west)}] { node[solid] {\_\_mt\_SlaveFunction}
child { node {\_\_mt\_WaitForWork}}
child { node {\_\_mt\_run\_my\_job}
child { node {\_\_mt\_runLoop\_int\_}
child { node {loop body function}}
}
}
child [missing] {}
child [missing] {}
child { node {\_\_mt\_EndOfTaskBarrier\_}}
}
}
child { node {\_\_mt\_run\_my\_job\_}
child { node {\_\_mt\_runloop\_int\_}
child { node {loop body function}}
}
}
}
child [missing] {}
child [missing] {}
child [missing] {}
child { node {\_\_mt\_EndOfTaskBarrier\_}}
}
};
\end{tikzpicture}
\end{document}
答案2
我已经尝试过这个包快速图表提议者帕帕它对我来说很好用,并且非常容易使用。
具体来说,用户仅需定义:
- 例程和子例程的名称
- 以及各个子程序的继承。
此外,它允许定义您自己的盒子并修改现有盒子的颜色/尺寸/长度/等等。
分数维:
\documentclass[border=10pt]{standalone}
\usepackage[french]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{babel}
\usepackage{fast-diagram}
\begin{document}
\renewcommand{\fastLargeurBoite}{11em}
\renewcommand{\fastEspaceColonne}{14em}
\begin{fast}{\_start}
\fastFT{main}{%
\fastFT{function}{%
\fastFT{\_\_mt\_MasterFunction}{%
\fastFT{\_thread\_start}{%
\fastFT{\_\_mt\_SlaveFunction\_}{%
\fastFT{\_\_mt\_WaitForWork\_}{}
\fastFT{\_\_mt\_run\_my\_job\_}{%
\fastFT{\_\_mt\_runLoop\_int\_}{
\fastFT{\textit{loop body function}}{}}}
\fastFT{\_\_mt\_EndOfTaskBarrier\_}{}}}
\fastFT{\_\_mt\_run\_my\_job\_}{%
\fastFT{\_\_mt\_runLoop\_int\_}{%
\fastFT{\textit{loop body function}}}}
\fastFT{\_\_mt\_EndOfTaskBarrier\_}{}}}}
\end{fast}
\end{document}