跳到主要内容
极客日志极客日志面向AI+效率的开发者社区
首页博客GitHub 精选镜像工具UI配色美学隐私政策关于联系
搜索内容 / 工具 / 仓库 / 镜像...⌘K搜索
注册
博客列表
编程语言算法

LaTeX 算法宏包 algorithm 与 algorithm2e 使用指南

综述由AI生成LaTeX 伪代码环境常用 algorithm2e 与 algorithmicx+algpseudocode 两种方案。前者功能强大适合学位论文,后者灵活可定制常用于会议模板。文章对比了两者基本结构、配置选项及冲突解决方法,涵盖行号控制、标题规范、中英文混排、长算法分页及竖线折线设置等注意事项,并提供强制换行与注释编写技巧,帮助科研论文中清晰呈现算法步骤。

开源信徒发布于 2026/2/9更新于 2026/5/303.3K 浏览
LaTeX 算法宏包 algorithm 与 algorithm2e 使用指南

LaTeX 中 algorithm 环境完整指南

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

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

1️⃣ 常用包的选择

  • ruled:算法标题在上方,并有横线
  • vlined:每行后加竖线(折线效果),用来表示语句块作用域
  • linesnumbered :为每一行添加行号
  • 提供 \KwIn, \KwOut, \For, \If, \While 等高阶命令
  • 自带行号、自动缩进、美观
  • algorithm 负责浮动体,类似 figure/table
  • algpseudocode 提供命令,比如 \State, \If, \For
  • 优点:高度可控,适合需要精细排版的场景

algorithmicx + algpseudocode

\usepackage{algorithm} \usepackage{algpseudocode}

algorithm2e

\usepackage[ruled,vlined]{algorithm2e}

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️⃣ 小技巧

    • 强制换行
      在 algorithm2e 里用 \;

    多行输入/输出

    \KwIn{$\mathcal{U}$: set of users, $P$: total power}
    \KwOut{Optimal allocation {$p_u$}}
    

    伪代码中写注释

    \tcp{This is a comment in algorithm2e} % or \Comment{This is a comment in algpseudocode}
    

    目录

    1. LaTeX 中 algorithm 环境完整指南
    2. 1️⃣ 常用包的选择
    3. 2️⃣ 基本结构
    4. algorithm2e 示例
    5. algorithmicx + algpseudocode 示例
    6. 3️⃣ 注意事项
    7. 🔹 (1) 包冲突问题
    8. 🔹 (2) 算法位置控制
    9. 🔹 (3) 行号 & 标题规范
    10. 🔹 (4) 中英文混排
    11. 🔹 (5) 长算法分页
    12. 🔹 (6) 折线(竖线)控制
    13. 4️⃣ 小技巧
    • 💰 8折买阿里云服务器限时8折了解详情
    • Magick API 一键接入全球大模型注册送1000万token查看
    • 🤖 一键搭建Deepseek满血版了解详情
    • 一键打造专属AI 智能体了解详情
    极客日志微信公众号二维码

    微信扫一扫,关注极客日志

    微信公众号「极客日志V2」,在微信中扫描左侧二维码关注。展示文案:极客日志V2 zeeklog

    更多推荐文章

    查看全部
    • 普通人如何利用 AI 大模型实现商业化变现
    • 华为 S5700 交换机 SSH/Telnet/Web 登录完整配置流程(V200R005C00SPC500)
    • C++ 多态详解:概念、实现与原理
    • 基于 Qwen3-VL 与 ComfyUI 的 AI 绘画图像反推指南
    • 15 个超级实用的 Python 技巧,提升开发效率
    • Vue Print Designer 前端可视化打印设计器详解
    • 服务器硬盘接口与形态:SATA、SAS、NVMe、U.2、M.2、U.3、EDSFF
    • openGauss 在向量数据库与 RAG 场景下的应用深度研究
    • Shell 脚本封装 Curl 请求及响应码校验
    • LangChain4j 集成 Spring Boot 完整教程
    • DeepSeek 使用指南与高阶提示词技巧
    • Webots 机器人模拟器:新手快速入门指南
    • Windows 系统下 PyCharm IDE 安装与配置指南
    • Python 环境下 Gurobi 安装与配置指南
    • 搜索二叉树与哈希表(HashMap/HashSet)原理及实现
    • 基于 Whisper 的本地语音识别与隐私保护方案
    • Git 合并冲突实战:高效解决与预防指南
    • ChatGPT 记忆功能揭秘:使用与管理指南
    • Cursor Chat 三大模式:Agent、Ask、Plan 解析
    • Coze ChatSDK 纯前端实现会话隔离

    相关免费在线工具

    • 加密/解密文本

      使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online

    • Gemini 图片去水印

      基于开源反向 Alpha 混合算法去除 Gemini/Nano Banana 图片水印,支持批量处理与下载。 在线工具,Gemini 图片去水印在线工具,online

    • Base64 字符串编码/解码

      将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online

    • Base64 文件转换器

      将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online

    • Markdown转HTML

      将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online

    • HTML转Markdown

      将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML转Markdown在线工具,online