📢 编译器为
xelatex
,算法宏包为algorithm2e
。
▚ 00 算法介绍
0.1 控制
- \SetAlgoLined:增添end行
- \DontPrintSemicolon:不显示行末尾的分号
- \SetKwInput{KwInput}{Input}:设置输入
- \SetKwInOut{KwInput}{Input}:设置输入输出
- \label:设置标签
- \ref:引用标签
0.2 函数
- \If
- \ElseIf
- \Else
- \For
- \While
0.3 注释
- \tcp{注释内容} :// 注释内容
- \tcc{注释内容}:/* 注释内容 */
🍀 默认情况下,注释行左对齐;添加*时,表示注释行右对齐,如下所示:
- \tcp*{注释内容} :// 注释内容
- \tcc*{注释内容}:/* 注释内容 */
0.4 标题
- 标题:\caption{标题名称}
0.5 引用包参数
- ruled:标题左对齐,顶部和底部各添加一条线
- linesnumbered:显示行编号
▚ 01 算法 (含end行)
1.1 源代码
\documentclass{article}
\usepackage[linesnumbered,ruled,vlined,algo2e]{algorithm2e}
\begin{document}
\begin{algorithm2e}[H]
\SetAlgoLined % 增添end行
\DontPrintSemicolon % 不显示行末尾的分号
\SetKwInOut{Input}{\textbf{Input}} % Set the Input
\SetKwInOut{Output}{\textbf{Output}} % set the Output
\Input{a,b,c}
\Output{sub,n}
$sub \gets 0, n \gets 0, a \gets 6, b \gets 8, c \gets 3$ \tcp*{Initialization}
\tcc{ loop: if...else if...else}
\If{a > b}
{
sub $\gets$ a - b
}
\ElseIf{a < b}
{
sub $\gets$ b - a\;
\tcc{loop: for}
\For{sub > c}
{
sub $\gets$ sub - 1
}
}
\Else
{
sub $\gets$ 0
}
\tcc{loop: While }
\While{ c $\ge$ 0}
{
c $\gets$ c - 1\;
}
\caption{Mathematical calculations}
\end{algorithm2e}
\end{document}
1.2 结果展示
▚ 02 算法 (不含end行)
2.1 源代码
\documentclass{article}
\usepackage[linesnumbered,ruled,vlined,algo2e]{algorithm2e}
\begin{document}
\begin{algorithm2e}[H]
%\SetAlgoLined % 增添end行
\DontPrintSemicolon
\SetKwInOut{Input}{\textbf{Input}} % Set the Input
\SetKwInOut{Output}{\textbf{Output}} % set the Output
\Input{a,b,c}
\Output{sub,n}
$sub \gets 0, n \gets 0, a \gets 6, b \gets 8, c \gets 3$ \tcp*{Initialization}
\tcc{ loop: if...else if...else}
\If{a > b}
{
sub $\gets$ a - b
}
\ElseIf{a < b}
{
sub $\gets$ b - a\;
\tcc{loop: for}
\For{sub > c}
{
sub $\gets$ sub - 1
}
}
\Else
{
sub $\gets$ 0
}
\tcc{loop: While }
\While{ c $\ge$ 0}
{
c $\gets$ c - 1\;
}
\caption{Mathematical calculations}
\end{algorithm2e}
\end{document}
2.2 结果展示
▚ 03 算法 (Input,Output后冒号不对齐)
3.1 源代码
\documentclass{article}
\usepackage[linesnumbered,ruled,vlined,algo2e]{algorithm2e}
% \SetAlgoLined 会增加end行
\begin{document}
\begin{algorithm2e}[H]
\SetAlgoLined
\DontPrintSemicolon
\SetKwInput{KwInput}{Input} % Set the Input
\SetKwInput{KwOutput}{Output} % set the Output
\KwInput{a,b,c}
\KwOutput{sub,n}
$sub \gets 0, a \gets 6, b \gets 8$ \tcp*{Initialization}
\tcc{ loop: if...else}
\If{a > b}
{
sub $\gets$ a - b
}
\Else
{
sub $\gets$ b - 1
}
\caption{Mathematical calculations}
\end{algorithm2e}
\end{document}
3.2 结果展示
▚ 04 算法 (自定义函数)
🍁自定义函数名使用
小型大写形状 Small Caps Shape
,可参考博客 【LaTex常用命令集】数学比较符号,字体形状,各种箭头。
4.1 源代码
\documentclass{article}
\usepackage[linesnumbered,ruled,vlined,algo2e]{algorithm2e}
\begin{document}
\begin{algorithm2e}[H]
\SetAlgoLined
\DontPrintSemicolon
\SetKwInOut{Input}{\textbf{Input}}
\SetKwInOut{Output}{\textbf{Output}}
\Input{a,b,c}
\Output{sub,n}
$sub \gets 0, a \gets 6, b \gets 8$ \tcp*{Initialization}
\textsc{Subtraction}(sub, a, b) \\
\SetKwFunction{FMain}{\emph{\textsc{Subtraction}}}
\SetKwProg{Fn}{Function}{}{}
\Fn{\FMain{sub, a, b}}{
\If{a > b}
{
sub $\gets$ a - b
}
\Else
{
sub $\gets$ b - a
}
}
\textbf{end}
\caption{Mathematical calculations}
\end{algorithm2e}
\end{document}
4.2 结果展示

▚ 05 算法 (中文支持)
5.1 源代码
\documentclass{article}
\usepackage[linesnumbered,ruled,vlined,algo2e]{algorithm2e}
\usepackage{xeCJK} %支持中文
\begin{document}
\begin{algorithm2e}[H]
\SetAlgoLined
\DontPrintSemicolon
\SetKwFunction{Subtraction}{\textsc{Subtraction}}
\SetKwInOut{Input}{\textbf{Input}}
\SetKwInOut{Output}{\textbf{Output}}
\Input{a,b,c}
\Output{sub,n}
$sub \gets 0, a \gets 6, b \gets 8$ \tcp*{初始化}
\textsc{Subtraction}(sub, a, b) \\
\SetKwFunction{FMain}{\emph{\textsc{Subtraction}}}
\SetKwProg{Fn}{Function}{}{}
\Fn{\FMain{sub, a, b}}{
\If{a > b}
{
sub $\gets$ a - b
}
\Else
{
sub $\gets$ b - 1
}
}\textbf{end}
\caption{数学计算}
\end{algorithm2e}
\end{document}
5.2 结果展示
▚ 06 算法 (智能标签)
🍂智能标签的优势:不会随着行号的编号而改变,只需将智能标签放在合适的语句处!
6.1 源代码
\begin{algorithm2e}[H]
\SetAlgoLined
\DontPrintSemicolon
\SetKwFunction{Subtraction}{\textsc{Subtraction}}
\SetKwInOut{Input}{\textbf{Input}}
\SetKwInOut{Output}{\textbf{Output}}
\Input{a,b,c}
\Output{sub,n}
$sub \gets 0, a \gets 6, b \gets 8$ \tcp*{初始化}
\textsc{Subtraction}(sub, a, b) \\
\SetKwFunction{FMain}{\textsc{Subtraction}}
\SetKwProg{Fn}{Function}{}{}
\Fn{\FMain{sub, a, b}}{ \label{alg:func:sub:start}
\If{a > b}
{
sub $\gets$ a - b
}
\Else
{
sub $\gets$ b - a
}
}\textbf{end} \label{alg:func:sub:end}
\caption{数学计算} \label{alg}
\end{algorithm2e}
算法\textsc{Subtraction}位于lines \ref{alg:func:sub:start}-\ref{alg:func:sub:end} of Algorithm \ref{alg}.
\end{document}
6.2 结果展示
▚ 07 算法 (字体颜色背景高亮)
- 添加
字体颜色
语法:{\color{颜色名} 代码内容 }
- 添加
背景颜色
语法:{\colorbox{颜色名} 代码内容 }
🎨 常用颜色的英文名:红(red)、绿(green)、蓝(blue)
7.1 源代码
\documentclass{article}
\usepackage[linesnumbered,ruled,vlined,algo2e]{algorithm2e}
\usepackage{color} %%%添加颜色所需的包
\begin{document}
\begin{algorithm2e}[H]
\SetAlgoLined
\DontPrintSemicolon
\SetKwInOut{Input}{\textbf{Input}}
\SetKwInOut{Output}{\textbf{Output}}
\Input{a,b}
\Output{sub,n}
$sub \gets 0, a \gets 6, b \gets 8$ \tcp*{Initialization}
\If{a > b} {
{\color{red}sub $\gets$ a - b}
}
\Else {
{\colorbox{green}{sub $\gets$ b - a}}
}
\caption{Mathematical calculations}
\end{algorithm2e}
\end{document}
7.2 结果展示

▚ 08 算法 (高级注释)
8.1 源代码
\documentclass{article}
\usepackage[linesnumbered,ruled,vlined,algo2e]{algorithm2e}
\usepackage{color} %%%添加颜色所需的包
\begin{document}
\begin{algorithm2e}[H]
\SetAlgoLined
\DontPrintSemicolon
\SetKwInOut{Input}{\textbf{Input}}
\SetKwInOut{Output}{\textbf{Output}}
\Input{a,b}
\Output{sub,n}
$sub \gets 0, a \gets 6, b \gets 8$ \tcp*{{\color{green} $\triangleleft$ Initialization}}
\centerline{{\underline{\color{blue} $\diamondsuit \quad \textbf{\emph{If condition}}$}}}
\If{a > b} {
{\color{red}sub $\gets$ a - b}
}
\centerline{{\underline{\color{blue} $\triangleleft \quad \textsc{Else condition}$}}}
\Else {
% sub $\gets$ b - a
{\colorbox{green}{sub $\gets$ b - a}}
}
\caption{Mathematical calculations}
\end{algorithm2e}
\end{document}
8.2 结果展示

参考材料
- https://en.wikibooks.org/wiki/LaTeX/Algorithms