LaTeX 写作: 算法代码排版 --latex2e范例总结

时间:2022-01-30 06:39:29

latex2e 宏包的使用范例:

\usepackage[ruled]{algorithm2e} %算法排版样式1
\usepackage[ruled,vlined]{algorithm2e} %算法排版样式2
\usepackage[linesnumbered,boxed]{algorithm2e} %算法排版样式3

使用上述代码中三种不同的宏包编译后的代码排版样式预览:

样式一:
LaTeX 写作: 算法代码排版 --latex2e范例总结

样式二:
LaTeX 写作: 算法代码排版 --latex2e范例总结

样式三:
LaTeX 写作: 算法代码排版 --latex2e范例总结

latex代码排版.tex文件:

\documentclass{article}

\usepackage[ruled]{algorithm2e} %算法排版样式1
%\usepackage[ruled,vlined]{algorithm2e} %算法排版样式2
%\usepackage[linesnumbered,boxed]{algorithm2e} %算法排版样式3

\begin{document}

How to use the algorithm2e in \LaTeX ~ file.

Examples:

% ------------------------------Example - 1---------------------------------------------
\begin{algorithm}[H]
\caption{How to write algorithms}
\KwIn{this text}
\KwOut{how to write algorithm with \LaTeX2e }

initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}
{
go to next section\;
current section becomes this one\;
}
{
go back to the beginning of current section\;
}
}
\end{algorithm}

% ---------------------------Example - 2------------------------------------------------
\begin{algorithm}
% \SetAlgoNoLine %去掉之前的竖线
\caption{identifyRowContext}
\KwIn{$r_i$, $Backgrd(T_i)$=${T_1,T_2,\ldots ,T_n}$ and similarity threshold $\theta_r$}
\KwOut{$con(r_i)$}
$con(r_i)= \Phi$\;
\For{$j=1;j \le n;j \ne i$}
{
float $maxSim=0$\;
$r^{maxSim}=null$\;
\While{not end of $T_j$}
{
compute Jaro($r_i,r_m$)($r_m\in T_j$)\;
\If{$(Jaro(r_i,r_m) \ge \theta_r)\wedge (Jaro(r_i,r_m)\ge r^{maxSim})$}
{
replace $r^{maxSim}$ with $r_m$\;
}
}
$con(r_i)=con(r_i)\cup {r^{maxSim}}$\;
}
return $con(r_i)$\;
\end{algorithm}
%--------------------------------------------------------------------------------------

some special information
The algorithm2e LaTeX package conflicts with several others over the use of the algorithm identifier. A common indicator is something like this message:
To resolve the issues, simply put the following just before the inclusion of the algorithm2e package:

%\makeatletter
%\newif\if@restonecol
%\makeatother
%\let\algorithm\relax
%\let\endalgorithm\relax

\end{document}

原文:

http://www.cnblogs.com/tsingke/p/5833221.html

\documentclass{article}
\usepackage{CJK}

\usepackage[noend]{algpseudocode}
\usepackage{algorithmicx,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}

LaTeX 写作: 算法代码排版 --latex2e范例总结