当前位置: 首页 > news >正文

latex|算法algorithm宏包和注意事项

LaTeX 中 algorithm 环境完整指南

在科研论文里,写清楚算法步骤通常需要用到 伪代码环境。最常见的选择有两个包:

  • algorithm2e —— 功能最强大,适合期刊/学位论文
  • algorithmicx + algpseudocode —— 更灵活、可定制,常用于会议模板

1️⃣ 常用包的选择

  1. algorithm2e

    \usepackage[ruled,vlined]{algorithm2e}
    
    • ruled:算法标题在上方,并有横线
    • vlined:每行后加竖线(折线效果),用来表示语句块作用域
    • linesnumbered :为每一行添加行号
    • 提供 \KwIn, \KwOut, \For, \If, \While 等高阶命令
    • 自带行号、自动缩进、美观
  2. algorithmicx + algpseudocode

    \usepackage{algorithm}
    \usepackage{algpseudocode}
    
    • algorithm 负责浮动体,类似 figure/table
    • algpseudocode 提供命令,比如 \State, \If, \For
    • 优点:高度可控,适合需要精细排版的场景

2️⃣ 基本结构

algorithm2e 示例

\begin{algorithm}[!t]\SetAlgoLined\caption{Power Allocation Algorithm}\KwIn{User set $\mathcal{U}$, channel gains $h_u$, total power $P$}\KwOut{Optimal power allocation $\{p_u\}$}Initialize $p_u \gets 0$ for all $u$\;\For{each user $u \in \mathcal{U}$}{Compute priority weight $w_u$\;\If{$w_u > \text{threshold}$}{Allocate power $p_u \gets f(h_u, w_u)$\;}}\Return $\{p_u\}$
\end{algorithm}

在这里插入图片描述

algorithmicx + algpseudocode 示例

\begin{algorithm}[!t]
\caption{Power Allocation Algorithm}
\begin{algorithmic}[1]
\Require User set $\mathcal{U}$, channel gains $h_u$, total power $P$
\Ensure Optimal power allocation $\{p_u\}$\State Initialize $p_u \gets 0$ for all $u$
\For{each user $u \in \mathcal{U}$}\State Compute priority weight $w_u$\If{$w_u > \text{threshold}$}\State Allocate power $p_u \gets f(h_u, w_u)$\EndIf
\EndFor
\State \Return $\{p_u\}$
\end{algorithmic}
\end{algorithm}

在这里插入图片描述


3️⃣ 注意事项

🔹 (1) 包冲突问题

  • 有些模板(如 IEEEtran)自带 algorithm 环境,可能会和 algorithm2e 冲突
  • 解决方法:如果用 algorithm2e,建议 \usepackage[linesnumbered,ruled,vlined]{algorithm2e} 并避免同时加载 algorithmicx

🔹 (2) 算法位置控制

  • \begin{algorithm}[!t] —— 强制浮动到页面顶部
  • [!h] —— 尽量放在当前位置
  • [!b] —— 底部

🔹 (3) 行号 & 标题规范

  • 行号:algorithm2e\SetAlgoNlRelativeSize{-1} 调整大小
  • 标题:最好简洁,避免超过一行
  • 如果论文模板要求“Algorithm 1: xxx”,需检查 期刊模板 是否自带样式

🔹 (4) 中英文混排

  • 建议输入输出统一用 \KwIn\KwOut,保持一致
  • 中文论文里标题可写中文,但正文尽量保持英文变量与符号

🔹 (5) 长算法分页

  • algorithm2e 自带分页支持
  • algorithmicx 需要加 \usepackage{algcompatible} 来避免分页问题

🔹 (6) 折线(竖线)控制

  • algorithm2e 里,折线由 vlined 选项决定
  • 如果不想要折线,有两种方法:

全局关闭:在导言区去掉 vlined

\usepackage[ruled]{algorithm2e}

局部关闭:在某个算法内部用命令

\SetVline{false}

需要恢复时再写

\SetVline{true}

👉 这样你可以灵活决定:有的算法直观用折线表示作用域,有的算法则更简洁无竖线。


4️⃣ 小技巧

  • 伪代码中写注释

    \tcp{This is a comment in algorithm2e}
    % or
    \Comment{This is a comment in algpseudocode}
    
  • 多行输入/输出

    \KwIn{$\mathcal{U}$: set of users, $P$: total power}
    \KwOut{Optimal allocation $\{p_u\}$}
    
  • 强制换行
    algorithm2e 里用 \;


http://www.lryc.cn/news/626214.html

相关文章:

  • 区块链 + 域名Web3时代域名投资的新风口(下)
  • RWA加密金融高峰论坛星链品牌全球发布 —— 稳定币与Web3的香港新篇章
  • CTFshow系列——命令执行web34-37
  • 厂区能源管理智能化改造物联网解决方案
  • 驱动开发系列65 - NVIDIA 开源GPU驱动open-gpu-kernel-modules 目录结构
  • week2-[一维数组]最大元素
  • 数据仓库OLTPOLAP维度讲解
  • 传统防火墙
  • LG P3710 方方方的数据结构 Solution
  • Windows/Centos 7下搭建Apache服务器
  • Android RxJava数据库操作:响应式改造实践
  • 006.Redis 哨兵(Sentinel)架构实战
  • C++入门自学Day14-- deque类型使用和介绍(初识)
  • 【运维进阶】Ansible 角色管理
  • 用poll改写select
  • RabbitMQ:SpringAMQP Direct Exchange(直连型交换机)
  • 在Excel和WPS表格中为多个数字同时加上相同的数值
  • 如何解析PDF中的复杂表格数据
  • UniApp 实现pdf上传和预览
  • Go语言快速入门指南(面向Java工程师)
  • 智慧校园中IPTV融合对讲:构建高效沟通新生态
  • DHCP详解
  • sqlite-gui:一款开源免费、功能强大的SQLite开发工具
  • Netty 集成 protobuf
  • 代码随想录刷题——字符串篇(七)
  • 机械原理的齿轮怎么学?
  • Transformer中的编码器和解码器是什么?
  • ubuntu安装kconfig-frontends提示报错
  • SpringAI——向量存储(vector store)
  • 【Netty4核心原理⑫】【异步处理双子星 Future 与 Promise】