有时候我们需要在文档中加入源代码,这里给出一个代码示例,供大家参考。
案例1:algorithm宏包
1 源代码
\documentclass{article}
\setlength\textwidth{245.0pt}
\usepackage[noend]{algpseudocode}
\usepackage{algorithm}
\begin{document}
\begin{algorithm}[t]
\caption{algorithm caption} %算法的名字
\hspace*{0.02in} {\bf Input:} %算法的输入, \hspace*{0.02in}用来控制位置,同时利用 \\ 进行换行
input parameters A, B, C\\
\hspace*{0.02in} {\bf Output:} %算法的结果输出
output result
\begin{algorithmic}[1]
\State some description % \State 后写一般语句
\For{condition} % For 语句,需要和EndFor对应
\State ...
\If{condition} % If 语句,需要和EndIf对应
\State ...
\Else
\State ...
\EndIf
\EndFor
\While{condition} % While语句,需要和EndWhile对应
\State ...
\EndWhile
\State \Return result
\end{algorithmic}
\end{algorithm}
\end{document}
2 代码效果
案例2:带公式的算法伪代码
1 源代码
\documentclass[11pt]{ctexart}
\usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry}
\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
\usepackage{amsmath}
\floatname{algorithm}{算法}
\renewcommand{\algorithmicrequire}{\textbf{输入:}}
\renewcommand{\algorithmicensure}{\textbf{输出:}}
\begin{document}
\begin{algorithm}
\caption{用归并排序求逆序数}
\begin{algorithmic}[1] %每行显示行号
\Require $Array$数组,$n$数组大小
\Ensure 逆序数
\Function {MergerSort}{$Array, left, right$}
\State $result \gets 0$
\If {$left < right$}
\State $middle \gets (left + right) / 2$
\State $result \gets result +$ \Call{MergerSort}{$Array, left, middle$}
\State $result \gets result +$ \Call{MergerSort}{$Array, middle, right$}
\State $result \gets result +$ \Call{Merger}{$Array,left,middle,right$}
\EndIf
\State \Return{$result$}
\EndFunction
\State
\Function{Merger}{$Array, left, middle, right$}
\State $i\gets left$
\State $j\gets middle$
\State $k\gets 0$
\State $result \gets 0$
\While{$i<middle$ \textbf{and} $j<right$}
\If{$Array[i]<Array[j]$}
\State $B[k++]\gets Array[i++]$
\Else
\State $B[k++] \gets Array[j++]$
\State $result \gets result + (middle - i)$
\EndIf
\EndWhile
\While{$i<middle$}
\State $B[k++] \gets Array[i++]$
\EndWhile
\While{$j<right$}
\State $B[k++] \gets Array[j++]$
\EndWhile
\For{$i = 0 \to k-1$}
\State $Array[left + i] \gets B[i]$
\EndFor
\State \Return{$result$}
\EndFunction
\end{algorithmic}
\end{algorithm}
\end{document}
代码运行效果
案例3:带范围线版本
源代码
\documentclass[8pt,twocolumn]{ctexart}
\usepackage{amssymb}
\usepackage{bm}
\usepackage{textcomp} %命令\textacutedbl的包,二阶导符号
% Page length commands go here in the preamble
\setlength{\oddsidemargin}{-0.25in} % Left margin of 1 in + 0 in = 1 in
\setlength{\textwidth}{9in} % 纸张宽度Right margin of 8.5 in - 1 in - 6.5 in = 1 in
\setlength{\topmargin}{-.75in} % Top margin of 2 in -0.75 in = 1 in
\setlength{\textheight}{9.2in} % Lower margin of 11 in - 9 in - 1 in = 1 in
\setlength{\parindent}{0in}
\makeatletter
\newif\if@restonecol
\makeatother
\let\algorithm\relax
\let\endalgorithm\relax
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}%[ruled,vlined]{
\usepackage{algpseudocode}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
\begin{document}
\begin{algorithm}
\caption{component matrices computing}
\LinesNumbered
\KwIn{$\mathcal{X}\in\mathbb{R}^{l_1\times l_2\times\cdots\times l_N},\varepsilon,\lambda,\delta,R$}
\KwOut{$A^{(j)}s$ for $j=1$ to $N$}
\textbf{Initialize} all $A^{(j)}s$ //which can be seen as the $0^{th}$ round iterations\;
{$l$\hspace*{-1pt}\textacutedbl}$=L$ //if we need to judge whether $(11)$ is true then {$l$\hspace*{-1pt}\textacutedbl} denotes $L|_{t-1}$\;
\For{ each $A_{i_jr}^{<!-- -->{j}}(1\le j\le N,1\le i_j\le I_j,1\le r\le R)$ }
{//$1^{st}$ round iterations\;
$g_{i_jr}^{(j)'}=g_{i_jr}^{(j)}$\;
$A_{i_jr}^{(j)'}=A_{i_jr}^{(j)}$//if the rollback shown as $(12)$ is needed,$A_{i_jr}^{(j)'}$ denotes $A_{i_jr}^{(j)}|_{t-1}$\;
$A_{i_jr}^{(j)}=A_{i_jr}^{(j)}-\mathrm{<!-- -->{\bf sign}}\left(g_{i_jr}^{(j)}\right)\cdot\delta_{i_jr}^{(j)}$\;
}
\Repeat(//other rounds of iterations for computing component matrices){$\bm{L\le \varepsilon}$ or maximum iterations exhausted}
{
$l'=L$ //if we need to judge whether $(11)$ is true then $l'$ denotes $L|_t$\;
\For{ each $A_{i_jr}^{<!-- -->{j}}(1\le j\le N,1\le i_j\le I_j,1\le r\le R)$}
{
\If{$g_{i_jr}^{(j)}\cdot g_{i_jr}^{(j)'}>0$}
{
$A_{i_jr}^{(j)'}=A_{i_jr}^{(j)} $\;
$g_{i_jr}^{(j)'}=g_{i_jr}^{(j)} $\;
$\delta_{i_jr}^{(j)}=\bm{\min}\left(\delta_{i_jr}^{(j)}\cdot\eta^{+},Max\_Step\_Size\right)$\;
$A_{i_jr}^{(j)}=A_{i_jr}^{(j)}-\mathrm{<!-- -->{\bf sign}}\left(g_{i_jr}^{(j)}\right)\cdot\delta_{i_jr}^{(j)}$\;
}
\ElseIf{$g_{i_jr}^{(j)}\cdot g_{i_jr}^{(j)'}<0$}
{
\If{$l'>l$\hspace*{-1pt}\textacutedbl}
{
$g_{i_jr}^{(j)'}=g_{i_jr}^{(j)}$\;
$A_{i_jr}^{(j)}=A_{i_jr}^{(j)'}$// if $(11)$ is true then rollback as $(12)$\;
$\delta_{i_jr}^{(j)}=\bm{\max}\left(\delta_{i_jr}^{(j)}\times\eta^{-},Min\_Step\_Size\right)$\;
}
\Else
{
$A_{i_jr}^{(j)'}=A_{i_jr}^{(j)} $\;
$g_{i_jr}^{(j)'}=g_{i_jr}^{(j)} $\;
$\delta_{i_jr}^{(j)}=\bm{\max}\left(\delta_{i_jr}^{(j)}\cdot\eta^{-},Min\_Step\_Size\right)$\;
$A_{i_jr}^{(j)}=A_{i_jr}^{(j)}-\mathrm{<!-- -->{\bf sign}}\left(g_{i_jr}^{(j)}\right)\cdot\delta_{i_jr}^{(j)}$\;
}
}
\Else
{
$A_{i_jr}^{(j)'}=A_{i_jr}^{(j)} $\;
$g_{i_jr}^{(j)'}=g_{i_jr}^{(j)} $\;
$A_{i_jr}^{(j)}=A_{i_jr}^{(j)}-\mathrm{<!-- -->{\bf sign}}\left(g_{i_jr}^{(j)}\right)\cdot\delta_{i_jr}^{(j)}$\;
}
}
$l$\hspace*{-1pt}\textacutedbl$=l'$\;
}
\end{algorithm}
\end{document}
代码效果:
参考资料:
LaTeX写伪代码_金良山庄-CSDN博客_latex写代码