使用LaTeX写伪代码

一、LaTeX写伪代码的需求

  想要向word格式的论文中添加伪代码,现有思路在LeTeX中书写伪代码,生成矢量图之后插入word中。

二、语法

https://zhuanlan.zhihu.com/p/166418214

https://zhuanlan.zhihu.com/p/468629675

三、示例

\documentclass[11pt]{article}
\usepackage{CJK}
\usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry}
\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
\usepackage{amsmath}

\floatname{algorithm}{算法}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}

\begin{document}
    \begin{CJK*}{UTF8}{gkai}
        \begin{algorithm}
            \caption{基于高斯混合的用户中心聚类算法}
            \begin{algorithmic}[1] %每行显示行号
                \Require 样本集合$C=c_1,c_2,\ldots,c_N,$
                \Ensure K个相似用户组$G=\left \{ G_1,G_2,...,G_k \}\right$
                
                \State 初始化高斯混合分布的模型参数
                \Repeat
                \For{$j=1,2,...,n$}
                \State 第一句
                \State 第二句
                \EndFor
                
                \For{$i=1,2,...,k$}
                \State 第一句
                \State 第二句
                \State 第三句
                \EndFor
                \State 一句话                
                \Until{condition}
                \State 一句话
                
                \State 初始化高斯混合分布的模型参数

                \For{$j=1,2,...,n$}
                \State 第一句
                \State 第二句
                \EndFor

            \end{algorithmic}
        \end{algorithm}
    \end{CJK*}
\end{document}

运行效果

 

 

https://www.jianshu.com/p/43c9c3f37baf

四、注意问题

1.latex伪代码中插入中文

\usepackage{CJK}

https://www.omegaxyz.com/2018/10/07/latex_-pseudocode/

2.将LaTeX写好的伪代码插入到word中,目前的方法:pdf转word,提取其中的图片。粘贴进word中

posted @ 2022-09-14 20:45  StarZhai  阅读(4158)  评论(0编辑  收藏  举报