300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 【paper】latex使用algorithm工具包实现伪代码排版

【paper】latex使用algorithm工具包实现伪代码排版

时间:2019-11-03 03:42:06

相关推荐

【paper】latex使用algorithm工具包实现伪代码排版

伪代码的latex代码

前言algorithm+algorithmicalgorithm2e拓展需求参考资料

前言

由于实现伪代码排版的需求比较急,以下内容仅为个人观点,如有错误的地方,欢迎评论区留言~

公式部分推荐使用MathType,可直接转为latex代码。

algorithm+algorithmic

如果你的伪代码比较长,需要分页显示,个人体验就是不推荐使用algorithm2e工具包,因为这个工具包比较新,在使用手册中没有找到相关方法实现分页。(有实现的小伙伴欢迎评论区留言~

①基本用法

\caption{Have a good time}\label{algorithm}#算法名称\renewcommand{\algorithmicrequire}{\textbf{Input:}}#修改Require为Input\renewcommand{\algorithmicensure}{\textbf{Output:}}#修改Ensure为Output\Require#Require\Ensure#Ensure\textbf{text}#加粗文本\bm{equation}#加粗公式$/*$ Comment $*/$#/*加注释*/for..to..do#for...to...dowhile..do#while..thenif..then#if..then

②宏包(写在导言区,即\begin{document}之前)

\usepackage{algorithm}#工具包\usepackage[noend]{algpseudocode}#工具包\usepackage{bm}#加粗公式

③例子

\documentclass{article}% 算法部分包\usepackage{algorithm}\usepackage[noend]{algpseudocode}\usepackage{bm}\begin{document}\begin{algorithm} \renewcommand{\algorithmicrequire}{\textbf{Input:}}\renewcommand{\algorithmicensure}{\textbf{Output:}}\caption{work for yourself}\label{algorithm}\begin{algorithmic}\Require\\ %换行The point cloud example \bm{$x,x \in {R^{n{\rm{\times }}4}}$};\\$/*$ i don not know $*/$\\Optimizer \bm{$opt$};\Ensure\\The minimal 3D Point Clouds Adversarial Example \bm{$best\_adv$};\end{algorithmic} \begin{algorithmic}[1] %显示行号\State {\textbf{Initialization:}}\State{\indent $best\_norm = + \inf $};\State{\textbf{for} $m \leftarrow 0$ \textbf{to} $M$ \textbf{do}}\State{\indent playplaydancedance};\State{\indent Focus on};\State {\textbf{end}}\end{algorithmic}\end{algorithm}\end{document}

algorithm2e

如果伪代码的排版比较基础,不需要分页、只使用常见的语句,个人推荐使用该工具包。该工具包与第一种相比,已将for循环等排版封装好,无需手动添加部分语句需要的then、end等。

①基本用法

补充用法

\tcc{Comment}#添加注释\For{.. \KwTo..}{..}#for..to..then..end\If{}{}#if..then..end\;#末尾加分号,换行

②宏包(写在导言区,即\begin{document}之前)

\usepackage[ruled,linesnumbered]{algorithm2e}#工具包\usepackage{bm}#加粗公式

ruled

在顶部和底部都有一条线。 标题不再位于算法下方,而是设置在算法开始的位置。

linesnumbered

除注释和输入/输出(KwInput和KwInOut)外,算法的所有行均已编号。

显示行号的问题

如果在input或者output添加注释时,行号会在注释后的第一行开始显示。

解决办法

将宏包中的linesnumbered删除,再将**\LinesNumberd**添加到正文内的指定位置,但是我发现显示的行号还是跟预期有偏差。由于最后我没有选择该工具包实现,就没有深究这个问题。

③例子

\documentclass{article}\usepackage[ruled,linesnumbered]{algorithm2e}\usepackage{bm}\begin{document}\begin{algorithm}\caption{Thank you my darling}\label{algorithm}\KwIn{\\The point \bm{$x$}\;The good thing \bm{${x_a}$}\;The label \bm{$y$}\;}\KwOut{\\The prediction \bm{${L_{pred}}$}\;}\textbf{Initialization}:\\\tcc{Calculate the loss by using the adversarial loss function $adv\_loss\left( {{\rm{\cdot}},{\rm{\cdot}}} \right)$}$loss \leftarrow adv\_loss\left( {f\left( {x_{adv}} \right),L} \right)$\;\For{$n \leftarrow 0$ \KwTo $N$}{$y \leftarrow {x_a}$\;\If{$x = = {x_a}$}{$alright!$\;$break;$}}\end{algorithm}\end{document}

拓展需求

①加横线

\rule[0.25\baselineskip]{\textwidth}{0.5pt} %加横线,写在指定的地方

②隐藏当前页页码

\usepackage{fancyhdr}%支持隐藏页码的宏包,写在导言区\thispagestyle{empty}%隐藏当前页页码,写在正文区

③分页显示

原本计划需要实现,由于第一种工具包词与词之间的间隔比较小,该功能可不实现。有需求的自行查看参考资料[分页显示]。

参考资料

Latex-算法伪代码

algorithm2e使用手册

分页显示

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。