使用 LaTeX 绘制流程图

在写作论文时,流程图是一种最为直观和直接的方法来帮助我们表示思想方法或者算法。下面介绍一种使用 LaTeX 宏包 TikZ 来绘制矢量流程图的方法,主要参考了这篇博客 Ethan Deng。

基本步骤

下面给出一个基本框图的代码,请注意注释:

% texlive2015, pdflatex
\documentclass{article}
\usepackage{palatino}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\begin{document}
\thispagestyle{empty}
% 流程图定义基本形状
\tikzstyle{startstop} = [rectangle, rounded corners, minimum width = 2cm, minimum height=1cm,text centered, draw = black]
\tikzstyle{io} = [trapezium, trapezium left angle=70, trapezium right angle=110, minimum width=2cm, minimum height=1cm, text centered, draw=black]
\tikzstyle{process} = [rectangle, minimum width=3cm, minimum height=1cm, text centered, draw=black]
\tikzstyle{decision} = [diamond, aspect = 3, text centered, draw=black]
% 箭头形式
\tikzstyle{arrow} = [->,>=stealth]
\begin{tikzpicture}[node distance=2cm]
%定义流程图具体形状
\node[startstop](start){Start};
\node[io, below of = start, yshift = -1cm](in1){Input};
\node[process, below of = in1, yshift = -1cm](pro1){Process 1};
\node[decision, below of = pro1, yshift = -1cm](dec1){Decision 1 ?};
\node[process, below of = dec1, yshift = -1cm](pro2){Process 2};
\node[io, below of = pro2, yshift = -1cm](out1){Output};
\node[startstop, below of = out1, yshift = -1cm](stop){Stop};
\coordinate (point1) at (-3cm, -6cm);
%连接具体形状
\draw [arrow] (start) -- (in1);
\draw [arrow] (in1) -- (pro1);
\draw [arrow] (pro1) -- (dec1);
\draw (dec1) -- node [above] {Y} (point1);
\draw [arrow] (point1) |- (pro1);
\draw [arrow] (dec1) -- node [right] {N} (pro2);
\draw [arrow] (pro2) -- (out1);
\draw [arrow] (out1) -- (stop);
\end{tikzpicture}
\end{document}

编译出来的效果:
使用 LaTeX 绘制流程图_第1张图片
当然你也可以做得 Fancy 一点,比如下面这个模拟退火算法的框图:
使用 LaTeX 绘制流程图_第2张图片
其代码如下:

% texlive2015, pdflatex
\documentclass{standalone}
\usepackage{newtxmath}
\usepackage{palatino}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\begin{document}
\thispagestyle{empty}
% 流程图定义基本形状
\tikzstyle{startstop} = [rectangle, rounded corners, minimum width = 2cm, minimum height=1cm,text centered, draw = black, fill = red!40]
\tikzstyle{io} = [trapezium, trapezium left angle=70, trapezium right angle=110, minimum width=2cm, minimum height=1cm, text centered, draw=black, fill = blue!40]
\tikzstyle{process} = [rectangle, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill = yellow!50]
\tikzstyle{decision} = [diamond, aspect = 3, text centered, draw=black, fill = green!30]
% 箭头形式
\tikzstyle{arrow} = [->,>=stealth]
\begin{tikzpicture}[node distance=2cm]
%定义流程图具体形状
\node (start) [startstop]
{Start};
\node (in1) [io, below of = start]
{Initial $x_0=(x_{01},x_{02},\cdots)$};
\node (pro1) [process, right of = in1, xshift = 5cm]
{Calculation $u_0=f(x_0)$};
\node (pro4) [process, below of = in1]
{New result $u^*=f(x_0^*)$};
\node (pro3) [process, below of=pro1]
{New solution $x_0^*=(\cdots,x_{0i},\cdots$)};
\node (pro2) [process, right of=pro3, xshift = 4cm]
{Randomly change $x_0$ into $x_0^*$};
\node (dec1) [decision, below of=pro4]
{Optimized?};
\node (pro5) [process, below of=pro3]
{Accept new solution probobly};
\node (pro6) [process, below of=dec1]
{Accept new solution};
\node (dec2) [decision, below of=pro5]
{Enough iterations?};
\node (pro7) [process, below of=dec2]
{Accept new solution as optimized solution};
\node (out1) [io, below of=pro6]
{Output $x_0^*$};
\node (stop) [startstop, below of=out1]
{stop};
%连接具体形状
\draw [arrow] (start) -- (in1);
\draw [arrow] (in1) -- (pro1);
\draw [arrow] (pro1) -| (pro2);
\draw [arrow] (pro2) -- (pro3);
\draw [arrow] (pro3) -- (pro4);
\draw [arrow] (pro4) -- (dec1);
\draw [arrow] (dec1) --node [above] {N} (pro5);
\draw [arrow] (dec1) --node [right] {Y} (pro6);
\draw [arrow] (pro6) -- (dec2);
\draw [arrow] (pro5) -- (dec2);
\draw [arrow] (dec2) -|node [right] {N} (pro2);
\draw [arrow] (dec2) --node [right] {Y} (pro7);
\draw [arrow] (pro7) -- (out1);
\draw [arrow] (out1) -- (stop);
\end{tikzpicture}

当然 LaTeX 是支持自定义颜色的,如果你有一套非常优秀的颜色配比,相信你可以画出非常好看的流程图。

总结

用 LaTeX 可以画出矢量的流程图,重点是在变成之前先安排好各个流程的位置。
如果你用 LaTeX 写论文的话会发现,这是一种使用其他软件生成的 jpg 格式图片所不能比拟的优势。另外,在PowerPoint 中使用“插入形状”并选择保存为 pdf 格式也可以得到矢量的 pdf 文件来插入到 LaTeX 的写作当中,不过这里不讨论这种所见即所得的方法。

彩蛋:使用 markdown 编辑器作流程图

第一次在 CSDN 上写博客时,我知道了可以使用 markdown 编辑器作流程图,效果也是不错的。代码如下:

```flow
st=>start: 开始
e=>end: 结束
op=>operation: 操作
cond=>condition: 确认?

st->op->cond
cond(yes)->e
cond(no)->op
Created with Raphaël 2.1.0 开始 操作 确认? 结束 yes no

可以看到,markdown 语法可以直接画出否认和操作的有两个直角的连线,比 LaTeX 要方便一些。
更多关于 markdown 流程图 的语法,可以参考这里。

你可能感兴趣的:(LaTeX,TikZ,流程图,LaTeX)