用 Latex 生成英文论文中的 算法步骤/伪代码 并插入 word 中

2023-05-16

准备工作:

1、先安装 CTEX https://mirrors.tuna.tsinghua.edu.cn/ctex/legacy/2.9/

2、下载 algorithm2e 包(里面有详细的使用说明文档) https://www.ctan.org/tex-archive/macros/latex/contrib/algorithm2e

      这个其实 CTEX里已经包含了,不过不是最新的。想要最新版和一些官方提供的例子可以在 CTAN 下载 algorithm2e 包 (也就是上面的链接),下载完之后要解压到 "C:\CTEX\MiKTeX\doc\latex\algorithm2e" 和 "C:\CTEX\MiKTeX\tex\latex\algorithm2e",然后要 运行 "开始菜单 -> Ctex -> Settings(Admin) -> Refresh FNDB"

3、安装 Inkspace (将生成的 算法步骤图/伪代码.pdf 转成能导入word的格式)https://inkscape.org/release/inkscape-0.92.4/

好吧,开始操作~~~

装好 CTEX 后,打开 "开始菜单 -> Ctex -> WinEdt",新建一个 file

总的来说创建一个 latex 伪代码文件 需遵循格式:

\def\SetClass{article}
\documentclass{\SetClass}

\usepackage[格式]{algorithm2e}

\begin{document}
算法内容
\end{document}

我们直接贴几个例子:

注意编译的时候 选 "PDFTeX",然后点 Accessories 里的 Compile

例子1:

这个例子来源于官方说明文档

\def\SetClass{article}
\documentclass{\SetClass}
\usepackage[lined,boxed,commentsnumbered]{algorithm2e}
\begin{document}
\begin{algorithm}[H]
  \SetAlgoLined
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }

  initialization\;
  \While{not at end of this document}{
    read current\;
    \eIf{understand}{
      go to next section\;
      current section becomes this one\;
      }{
      go back to the beginning of current section\;
      }
    }
  \caption{How to write algorithms}
\end{algorithm}
\end{document} 

例子2:

这个例子也是官方说明文档里的

\def\SetClass{article}
\documentclass{\SetClass}
\usepackage[linesnumbered,lined,boxed,commentsnumbered]{algorithm2e}
\begin{document}
\IncMargin{1em}
\begin{algorithm}
  \SetKwData{Left}{left}\SetKwData{This}{this}\SetKwData{Up}{up}
  \SetKwFunction{Union}{Union}\SetKwFunction{FindCompress}{FindCompress}
  \SetKwInOut{Input}{input}\SetKwInOut{Output}{output}

  \Input{A bitmap $Im$ of size $w\times l$}
  \Output{A partition of the bitmap}
  \BlankLine
  \emph{special treatment of the first line}\;
  \For{$i\leftarrow 2$ \KwTo $l$}{
    \emph{special treatment of the first element of line $i$}\;
    \For{$j\leftarrow 2$ \KwTo $w$}{\label{forins}
      \Left$\leftarrow$ \FindCompress{$Im[i,j-1]$}\;
      \Up$\leftarrow$ \FindCompress{$Im[i-1,]$}\;
      \This$\leftarrow$ \FindCompress{$Im[i,j]$}\;
      \If(\tcp*[h]{O(\Left,\This)==1}){\Left compatible with \This}{\label{lt}
        \lIf{\Left $<$ \This}{\Union{\Left,\This}}
        \lElse{\Union{\This,\Left}}
      }
      \If(\tcp*[f]{O(\Up,\This)==1}){\Up compatible with \This}{\label{ut}
        \lIf{\Up $<$ \This}{\Union{\Up,\This}}
        \tcp{\This is put under \Up to keep tree as flat as possible}\label{cmt}
        \lElse{\Union{\This,\Up}}\tcp*[h]{\This linked to \Up}\label{lelse}
      }
    }
    \lForEach{element $e$ of the line $i$}{\FindCompress{p}}
  }
  \caption{disjoint decomposition}\label{algo_disjdecomp}
\end{algorithm}\DecMargin{1em}
\end{document} 

例子3:

这个例子来源于博客,而且博主对其中的一些用法给出了一些说明,详见 https://blog.csdn.net/robert_chen1988/article/details/71512914

\def\SetClass{article}
\documentclass{\SetClass}
\usepackage[ruled,linesnumbered]{algorithm2e}
\begin{document}
\begin{algorithm}
\caption{Simulation-optimization heuristic}\label{algorithm}
\KwData{current period $t$, initial inventory $I_{t-1}$, initial capital $B_{t-1}$, demand samples}
\KwResult{Optimal order quantity $Q^{\ast}_{t}$}
$r\leftarrow t$\;
$\Delta B^{\ast}\leftarrow -\infty$\;
\While{$\Delta B\leq \Delta B^{\ast}$ and $r\leq T$}{$Q\leftarrow\arg\max_{Q\geq 0}\Delta B^{Q}_{t,r}(I_{t-1},B_{t-1})$\;
$\Delta B\leftarrow \Delta B^{Q}_{t,r}(I_{t-1},B_{t-1})/(r-t+1)$\;
\If{$\Delta B\geq \Delta B^{\ast}$}{$Q^{\ast}\leftarrow Q$\;
$\Delta B^{\ast}\leftarrow \Delta B$\;}
$r\leftarrow r+1$\;}
\end{algorithm}
\end{document}

转格式

好了,最后一步就是将生成的 PDF 转成我们需要的格式了,打开 Inkspace 转就完事了,这里不详细说了~

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

用 Latex 生成英文论文中的 算法步骤/伪代码 并插入 word 中 的相关文章

随机推荐