latex 如何在overleaf中插入算法伪代码

2023-05-16

问题描述

博主今天在用overleaf写论文,急需插入伪代码,将查阅结果记录在此以便后续参考。

解决方案

overleaf有专门的说明文档来教大家如何正确插入伪代码:

https://www.overleaf.com/learn/latex/Algorithms

 列举几种样式与其对应的latex源码。

样式1

 Latex源码:

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{An algorithm with caption}\label{alg:cap}
\begin{algorithmic}
\Require $n \geq 0$
\Ensure $y = x^n$
\State $y \gets 1$
\State $X \gets x$
\State $N \gets n$
\While{$N \neq 0$}
\If{$N$ is even}
    \State $X \gets X \times X$
    \State $N \gets \frac{N}{2}$  \Comment{This is a comment}
\ElsIf{$N$ is odd}
    \State $y \gets y \times X$
    \State $N \gets N - 1$
\EndIf
\EndWhile
\end{algorithmic}
\end{algorithm}

\end{document}

样式2

 Latex源码:

\documentclass{article}
\usepackage{algorithm2e}
\begin{document}
%% This declares a command \Comment
%% The argument will be surrounded by /* ... */
\SetKwComment{Comment}{/* }{ */}
\RestyleAlgo{ruled}
\begin{algorithm}
\caption{An algorithm with caption}\label{alg:two}
\KwData{$n \geq 0$}
\KwResult{$y = x^n$}
$y \gets 1$\;
$X \gets x$\;
$N \gets n$\;
\While{$N \neq 0$}{
  \eIf{$N$ is even}{
    $X \gets X \times X$\;
    $N \gets \frac{N}{2}$ \Comment*[r]{This is a comment}
  }{\If{$N$ is odd}{
      $y \gets y \times X$\;
      $N \gets N - 1$\;
    }
  }
}
\end{algorithm}

\end{document}

样式3

Latex源码:

\documentclass{article}
\usepackage{algorithm2e}
\begin{document}
%% This declares a command \Comment
%% The argument will be surrounded by /* ... */
\SetKwComment{Comment}{/* }{ */}

\begin{algorithm}
\caption{An algorithm with caption}\label{alg:two}
\KwData{$n \geq 0$}
\KwResult{$y = x^n$}
$y \gets 1$\;
$X \gets x$\;
$N \gets n$\;
\While{$N \neq 0$}{
  \eIf{$N$ is even}{
    $X \gets X \times X$\;
    $N \gets \frac{N}{2}$ \Comment*[r]{This is a comment}
  }{\If{$N$ is odd}{
      $y \gets y \times X$\;
      $N \gets N - 1$\;
    }
  }
}
\end{algorithm}

\end{document}

以下两种代码来自:

用LaTeX优雅地书写伪代码:Algorithm2e简明指南

样式4

 Latex源码:

\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}

样式5

Latex源码: 

\documentclass{article}
\usepackage[linesnumbered, ruled]{algorithm2e}
\SetKwRepeat{Do}{do}{while}%
\begin{document}

\begin{algorithm}[H]
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }
  initialization\;
  \While{not at end of this document}{
    read current\;
    \Repeat{this end condition}{
      do these things\;
    }
    \eIf{understand}{
      go to next section\;
      current section becomes this one\;
    }{
      go back to the beginning of current section\;
    }
    \Do{this end condition}{
      do these things\;
    }
  }
  \caption{How to write algorithms}
\end{algorithm}

\end{document}

注意:如果伪代码每行前面一直出现编号,可以检查是不是添加了额外的跟伪代码相关的包,一般来说删除之后便可解决问题。

更多解释和代码示例见上面列出的官方文档链接地址和微信公众号。

祝各位友友们写论文愉快~

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

latex 如何在overleaf中插入算法伪代码 的相关文章

随机推荐

  • 点云PCL——移动立方体(Marching Cubes,MC)算法

    MC算法的优点 xff1a 生成网格的质量好 xff0c 具有很高的可并行性 移动立方体算法的主要思想是在三维离散数据场中通过线性差值来逼近等值面 体元是在三维图像中由相邻的八个体素点组成的正方体方格 xff0c 体元中顶点值有三种情况 x
  • CCF: 2021 12-1 序列查询

    题目背景 西西艾弗岛的购物中心里店铺林立 xff0c 商品琳琅满目 为了帮助游客根据自己的预算快速选择心仪的商品 xff0c IT 部门决定研发一套商品检索系统 xff0c 支持对任意给定的预算 x xff0c 查询在该预算范围内 xff0
  • Python字符串排序的两种方法详解

    感谢 转载自 xff1a https blog csdn net space walk article details 102738583 Python中没有直接对字符串排序的方法 xff0c 下面介绍两种方法对字符串进行排序 第一种方法
  • mongodb nginx代理问题

    在代理mongodb的端口的时候出现以下error xff1a recv message len 1347703880 is invalid Min 16 Max 48000000 这是因为mongodb接口走的是tcp xff0c 但是之
  • CSDN代码片高亮显示,让你的博客更加靓丽~~

    最近在更新博客 xff0c 发现csdn的代码片有高亮显示的功能 xff0c 就想试试 xff0c 尝试了几种方法 xff0c 可是都没有效果 xff0c 今天无意中又将之前的 方法想了一遍 xff0c 在博客中这么一试 xff0c 居然成
  • Ubuntu 16.04 LTS 中通过grub修改root用户密码

    开机时按住shift不要松开 选择Advanced选项 xff0c 回车 选择恢复模式 xff08 只是选择 xff0c 不需要按下回车 xff09 xff0c 按下e键 找到 ro recovery nomodeset 并删掉 xff0c
  • Opencv2.4.9源码分析——Stitching(三)

    3 相机参数评估 3 1 原理 相机参数的评估也称为相机定标 要想理解这部分内容 xff0c 首先应该从成像原理开始讲起 图6 小孔成像原理 从图6可以看出 xff0c 真实物体通过小孔映射到成像平面上 xff0c 小孔到成像平面的距离称为
  • computed 和 watch 的区别

    computed 注意data 里面不需要声明fullname lt template gt lt div class 61 34 hello 34 gt lt h1 gt fullName lt h1 gt lt div gt lt te
  • Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file

    Springboot2 3 4 RELEASE报错 xff0c 解决办法 升级jdk到11版本
  • excel 用一列数据生成图表/折线图

    问题描述 该教程适用于该列的长度很长 xff0c 数据量较大不适合鼠标拖动选中的情况 如果有更简单的方法也欢迎小伙伴们在评论区交流补充 操作步骤 1 点一下列上面的标号 即图中的B 2 按住ctrl xff0c 再点一下列的名称 即图中的a
  • ubuntu NVIDIA CC sanity check failed:

    ubuntu NVIDIA CC sanity check failed The C compiler does not appear to be able to create object files Please make sure y
  • Zeal -- 一款面向软件开发人员的离线文档浏览器

    简介 Zeal官网 就我而言 xff0c 经常忘记一些不常用的方法什么的 xff0c 难免需要查询API文档 xff0c 这时候有一个离线的文档浏览器还是很方便的 xff0c 可以集成多种语言文档 xff0c 便于查阅学习 对于 对于MAC
  • markdown-it 重定义渲染规则

    使用 markdown it 解析 markdown 代码 从这篇文章中了解到 markdown it 解析 markdown 代码的过程 xff0c 了解 token 和渲染规则 add target 61 34 blank 34 to
  • 马走日算法-回溯

    马走日算法 回溯 1 马走日走到目标节点最少的步数 include lt iostream gt include lt queue gt include lt functional gt include lt stack gt includ
  • MYSQL如何设置大小写敏感

    linux下mysql设置大小写不敏感 1 linux下mysql安装完后是默认 xff1a 区分表名的大小写 xff0c 不区分列名的大小写 xff1b 2 用root帐号登录后 xff0c 在 etc my cnf 中的 mysqld
  • Linux下几种图形化远程工具推荐

    好久没写文章了 xff0c 和大家聊聊最近在机器人调试中用到的几款远程桌面工具吧 在机器人调试过程中经常需要使用rviz之类的工具来查看机器人的运行状态 xff0c 图形化的远程工具必不可少 当然如果大家习惯用ssh或者没有图形化方面的需求
  • Win10x64-WDM设备驱动开发入门 - 第六章 VMware-Win10x64+WDK10+VS2015/2019环境搭建与双机调试

    VMware Win10x64 43 WDK10 43 VS2015 2019环境搭建与双机调试 1 驱动程序的开发语言选择调用约定运行时函数的调用 2 配置驱动开发环境1 vs2015 43 SDK 43 WDK10查看 win10 的版
  • 找出4*5矩阵中值最小和最大元素,并分别输出其值及所在的行号和列号 java

    package 矩阵 import java util Scanner public class 矩阵 public static void main String args TODO Auto generated method stub
  • 将python程序打包成exe可执行文件

    方法一 xff1a xff1a 打包完成后可以直接被他人使用 xff0c 他人不用安装python环境的 可以使用pyinstaller模块实现将python项目打包成exe执行文件 span class token triple quot
  • latex 如何在overleaf中插入算法伪代码

    问题描述 博主今天在用overleaf写论文 xff0c 急需插入伪代码 xff0c 将查阅结果记录在此以便后续参考 解决方案 overleaf有专门的说明文档来教大家如何正确插入伪代码 xff1a https www overleaf c