Skip to content

Commit

Permalink
Fixes sjtug#145 replace algorithmic with algorithmicx
Browse files Browse the repository at this point in the history
- remove old algorithmic package
- remove extension for SWITCH CASE and its example
- add algorithmicx package
- add new example
- add how to use fixed position
  • Loading branch information
at15 authored and weijianwen committed Aug 25, 2016
1 parent c4cbb6a commit 6fb56b8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 58 deletions.
25 changes: 4 additions & 21 deletions sjtuthesis.cls
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
\RequirePackage[inline]{enumitem}
\RequirePackage{pdfpages}
\RequirePackage{calc}
\RequirePackage{algorithm, algorithmic}
\RequirePackage{algorithm, algorithmicx, algpseudocode}
\RequirePackage{siunitx}
\RequirePackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
Expand Down Expand Up @@ -128,8 +128,11 @@
\ctexset{listfigurename={\sjtu@listfigurename}}
\ctexset{listtablename={\sjtu@listtablename}}
\floatname{algorithm}{\sjtu@label@algo}
\renewcommand{\algorithmicrequire}{\textbf{输入:}}
\renewcommand{\algorithmicensure}{\textbf{输出:}}
\renewcommand{\listalgorithmname}{\sjtu@listalgorithmname}
\renewcommand{\lstlistingname}{\sjtu@value@listingname}

% Title Settings at the chapter Level
\ctexset{chapter={
nameformat={\Large\bfseries},
Expand Down Expand Up @@ -258,26 +261,6 @@
\newtheorem{bcor}[thm]{\sjtu@label@cor~}
\renewcommand{\proofname}{\bf\sjtu@label@proof}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The following definitions are to extend the LaTeX algorithmic
% package with SWITCH statements and one-line structures.
% The extension is by
% Prof. Farn Wang
% Dept. of Electrical Engineering,
% National Taiwan University.
%
\newcommand{\SWITCH}[1]{\STATE \textbf{switch} (#1)}
\newcommand{\ENDSWITCH}{\STATE \textbf{end switch}}
\newcommand{\CASE}[1]{\STATE \textbf{case} #1\textbf{:} \begin{ALC@g}}
\newcommand{\ENDCASE}{\end{ALC@g}}
\newcommand{\CASELINE}[1]{\STATE \textbf{case} #1\textbf{:} }
\newcommand{\DEFAULT}{\STATE \textbf{default:} \begin{ALC@g}}
\newcommand{\ENDDEFAULT}{\end{ALC@g}}
\newcommand{\DEFAULTLINE}[1]{\STATE \textbf{default:} }
%
% End of the LaTeX algorithmic package extension.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%==========
% Segment 4. Draw the sjtuthesis
%==========
Expand Down
87 changes: 52 additions & 35 deletions tex/example.tex
Original file line number Diff line number Diff line change
Expand Up @@ -370,45 +370,62 @@ \section{用listings插入源代码}
}
\end{lstlisting}

\section{用algorithm和algorithmic宏包插入算法描述}
\section{用algorithm和algorithmicx宏包插入算法描述}

如算法\ref{algo:sum_I_want}所示。
algorithmicx 比 algorithmic 增加了一些命令。
示例如\ref{algo:merge_sort},示例代码来自\href{http://hustsxh.is-programmer.com/posts/38801.html}{xhSong的博客}。
详细的使用方法见\href{http://mirror.hust.edu.cn/CTAN/macros/latex/contrib/algorithmicx/algorithmicx.pdf}{官方README}。

\begin{algorithm}
\caption{求和算法}
\label{algo:sum_I_want}
\begin{algorithmic}
\REQUIRE $n \geq 1$ %输入条件
\ENSURE $Sum = 1 + \cdots + n$ %输出
\STATE $Sum \leftarrow 0$ %\STATE 命名演示
\IF {$n < 1$} %条件语句
\PRINT {Input Error} %打印语句
\ENDIF
\RETURN Sum
\end{algorithmic}
\end{algorithm}
总结一些注意事项:

王凡教授\footnote{\url{http://cc.ee.ntu.edu.tw/~farn/}}对algorithmic做了扩展,支持\verb+STATE+、\verb+SWITCH+和\verb+CASE+等语句。
\begin{itemize}
\item
部分情况下,浮动时会出现图片出现在算法中间的情况。
参考
\href{http://tex.stackexchange.com/questions/165021/fixing-the-location-of-the-appearance-in-algorithmicx-environment}{这个问题}
使用H来强制定位,不允许浮动。
\end{itemize}

\begin{algorithm}
\caption{fibonacci}
\label{algo: Fibonacci Sequence}
\begin{algorithmic}
\REQUIRE $num$

\STATE $res \leftarrow 0$
\SWITCH {$num$}
\CASE {$0$}
\STATE $res \leftarrow 1$
\ENDCASE
\CASE {$1$}
\STATE $res \leftarrow 1$
\ENDCASE
\DEFAULT
\STATE $res \leftarrow fibonacci(num-1) + fibonacci(num-2)$
\ENDDEFAULT
\ENDSWITCH

\RETURN $res$
\label{algo:merge_sort}
\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}
4 changes: 2 additions & 2 deletions thesis.tex
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
\addcontentsline{toc}{chapter}{\listfigurename} %将插图目录加入全文目录
\listoftables
\addcontentsline{toc}{chapter}{\listtablename} %将表格目录加入全文目录
% \listofalgorithms
% \addcontentsline{toc}{chapter}{算法索引} %将算法目录加入全文目录
\listofalgorithms
\addcontentsline{toc}{chapter}{算法索引} %将算法目录加入全文目录

\include{tex/symbol} % 主要符号、缩略词对照表

Expand Down

0 comments on commit 6fb56b8

Please sign in to comment.