LaTex论文排版 | (16)绘制程序流程图(框图)

在写作论文时,流程图是一种最为直观和直接的方法来帮助我们表示思想方法或者算法。下面介绍一种使用 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}
 
 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

编译出来的效果:
这里写图片描述
当然你也可以做得 Fancy 一点,比如下面这个模拟退火算法的框图:
这里写图片描述
其代码如下:

% 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}
 
 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

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

总结

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

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

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

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

st->op->cond
cond(yes)->e
cond(no)->op
 
 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
Created with Raphaël 2.1.0 开始 操作 确认? 结束 yes no

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

  • 2
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用提供了一个在线的latex表格编辑器,你可以直接在网页上将表格画好并填好,然后点击生成即可转换成latex格式。你可以访问链接https://www.tablesgenerator.com/来使用这个工具。 当在latex中进行图片排版时,可以使用figure环境。首先,你需要在导言区添加以下代码,用于调整公式字体大小并恢复编号样式: \makeatletter \renewcommand{\maketag@@@}[1]{\hbox{\m@th\normalsize\normalfont#1}}% \makeatother 接下来,你可以使用\begin{figure}[htbp和\end{figure}来包围你的图片和表格。在figure环境中,你可以使用\centering命令将图片或表格居中显示。例如,如果你要插入一个表格,可以使用以下代码: \begin{figure}[htbp] \centering \begin{tabular}{ccc} 表头1 & 表头2 & 表头3 \\ 数据1 & 数据2 & 数据3 \\ 数据4 & 数据5 & 数据6 \\ \end{tabular} \caption{这是一个表格} \label{fig:table} \end{figure} 在上面的代码中,\begin{tabular}{ccc}用于定义表格的列数和每列的对齐方式,表格内容则通过多行多列的方式填入。\caption和\label分别用于设置表格的标题和引用标签。 请注意,htbp是figure环境中的可选参数,用于指定图片或表格的放置位置。h表示尽可能在当前位置放置,t表示在页面顶部放置,b表示在页面底部放置,p表示单独放置在一页上。 希望这些信息对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Overleaf使用技巧 (latex公式,latex表格,latex图片排版)](https://blog.csdn.net/qq_33684682/article/details/125048367)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [【Latex论文排版】表格、图片和公式](https://blog.csdn.net/zfhsfdhdfajhsr/article/details/122521309)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值