当前位置: 首页 > 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.dtcms.com/a/339148.html

相关文章:

  • 检测设备为手机或电脑来跳转不同网页
  • GaussDB 八种常规锁介绍
  • Redis的使用(初阶)
  • 命令行如何更改参数?
  • 化学反应中的贝叶斯优化
  • Spring RestTemplate的postForObject()方法详解与实践指南
  • 从一个ctf题中学到的多种php disable_functions bypass 姿势
  • 网络聚合链路与软件网桥配置指南
  • 效果图只是起点:深挖3D可视化在家装建筑中的隐藏金矿
  • Unity进阶--C#补充知识点--【C#各版本的新功能新语法】C#1~4与C#5
  • mycat分库分表实验
  • 安全设计-防止非法移机
  • Java多线程:线程创建、安全、同步与线程池
  • C++语法学习笔记之六 --- 生命周期、作用域
  • 私有化部署全攻略:开源大模型本地化改造的性能与安全深度评测
  • https如何保证传递参数的安全
  • 从零开始的云计算生活——第四十六天,铁杵成针,kubernetes模块之Configmap资源与Secret资源对象
  • 【Java】Spring Boot 3.0 微服务架构:高效开发与部署的最佳实践指南
  • CETOL 6σ 解决一次性支架输送器精度与法规验证难题
  • 算法——快速幂
  • 教程:用XiaothinkT6语言模型快速实现文本情感分类,附轻量模型推荐
  • 全链路自主构建智慧科研写作系统——融合LLM语义理解、多智能体任务协同与n8n自动化工作流构建
  • TDengine IDMP 运维指南(3. 使用 Ansible 部署)
  • 决策树算法全景解析:从 ID3、C4.5 到 CART
  • 响应式爬虫系统设计:Scala异步任务编排与弹性容错机制
  • 工业级蓝光三维扫描仪:助力汽车零部件高精度检测与质量提升
  • C++11 lambda表达式使用讲解
  • string 题目练习 过程分析 具体代码
  • leetcode_239 滑动窗口最大值
  • 本地部署消息中间件 Weblogic 并实现外部访问