![如何在算法的顶部和底部编写开始和结束语句?](https://linux22.com/image/304043/%E5%A6%82%E4%BD%95%E5%9C%A8%E7%AE%97%E6%B3%95%E7%9A%84%E9%A1%B6%E9%83%A8%E5%92%8C%E5%BA%95%E9%83%A8%E7%BC%96%E5%86%99%E5%BC%80%E5%A7%8B%E5%92%8C%E7%BB%93%E6%9D%9F%E8%AF%AD%E5%8F%A5%EF%BC%9F.png)
我怎样才能像这样在以下代码的顶部和底部写下“begin”和“end”语句?带有 begin 和 end 语句的伪代码示例
除了使用算法和 algopseudocode 包之外,我还可以编写 Require 和 Ensure 语句。这些语句没有正确对齐。有没有办法像附图一样对齐?
% book example for classicthesis.sty
\documentclass[
% Replace twoside with oneside if you are printing your thesis on a single side
% of the paper, or for viewing on screen.
%oneside,
twoside,
11pt, a4paper,
footinclude=true,
headinclude=true,
cleardoublepage=empty
]{scrbook}
\usepackage{lipsum}
\usepackage[linedheaders,parts,pdfspacing]{classicthesis}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{acronym}
\usepackage{algpseudocode}
\usepackage{algorithm}
\begin{document}
\chapter{A Chapter}
\begin{algorithm}
\caption{Apriori algorithm}
\begin{algorithmic}[1]
\State $L_{1} \leftarrow Frequent 1-itemset $
\State $k \leftarrow 2$
\While{$L_{k-1} \neq \phi$}
\State $Temp \leftarrow candidateItemSet (L_{k-1})$
\State $C_{k} \leftarrow frequencyOfItemSet (Temp)$
\State $L_{k} \leftarrow compareItemSetWithMinimumSupport (C_{k}, minsup) $
\State $k \leftarrow k + 1$
\EndWhile\\
\Return L
\end{algorithmic}
\end{algorithm}
\end{document}
答案1
只需定义(algpseudocode
风格)
\algdef{SE}{Begin}{End}{\textbf{begin}}{\textbf{end}}
然后使用\Begin
和 ,\End
如下例所示:
% book example for classicthesis.sty
\documentclass[
% Replace twoside with oneside if you are printing your thesis on a single side
% of the paper, or for viewing on screen.
%oneside,
twoside,
11pt, a4paper,
footinclude=true,
headinclude=true,
cleardoublepage=empty
]{scrbook}
\usepackage{lipsum}
\usepackage[linedheaders,parts,pdfspacing]{classicthesis}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{acronym}
\usepackage{algpseudocode}
\usepackage{algorithm}
\algdef{SE}{Begin}{End}{\textbf{begin}}{\textbf{end}}
\begin{document}
\chapter{A Chapter}
\begin{algorithm}
\caption{Apriori algorithm}
\begin{algorithmic}[1]
\Begin
\State $L_{1} \leftarrow Frequent 1-itemset $
\State $k \leftarrow 2$
\While {$L_{k-1} \neq \phi$}
\State $Temp \leftarrow candidateItemSet (L_{k-1})$
\State $C_{k} \leftarrow frequencyOfItemSet (Temp)$
\State $L_{k} \leftarrow compareItemSetWithMinimumSupport (C_{k}, minsup) $
\State $k \leftarrow k + 1$
\EndWhile
\State \Return L
\End
\end{algorithmic}
\end{algorithm}
\end{document}
输出
只是一句评论。你不应该写
\EndWhile\\
\Return L
但
\EndWhile
\State \Return L
正如我的例子一样。
如果您想要不同的缩进,\Return
可以执行以下操作。
添加
\def\BState{\State\hskip-.5em}
在序言中,并在 前面使用\BState
代替。\State
\Return
平均能量损失
% book example for classicthesis.sty
\documentclass[
% Replace twoside with oneside if you are printing your thesis on a single side
% of the paper, or for viewing on screen.
%oneside,
twoside,
11pt, a4paper,
footinclude=true,
headinclude=true,
cleardoublepage=empty
]{scrbook}
\usepackage{lipsum}
\usepackage[linedheaders,parts,pdfspacing]{classicthesis}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{acronym}
\usepackage{algpseudocode}
\usepackage{algorithm}
\algdef{SE}{Begin}{End}{\textbf{begin}}{\textbf{end}}
\def\BState{\State\hskip-.5em}
\begin{document}
\chapter{A Chapter}
\begin{algorithm}
\caption{Apriori algorithm}
\begin{algorithmic}[1]
\Begin
\State $L_{1} \leftarrow Frequent 1-itemset $
\State $k \leftarrow 2$
\While {$L_{k-1} \neq \phi$}
\State $Temp \leftarrow candidateItemSet (L_{k-1})$
\State $C_{k} \leftarrow frequencyOfItemSet (Temp)$
\State $L_{k} \leftarrow compareItemSetWithMinimumSupport (C_{k}, minsup) $
\State $k \leftarrow k + 1$
\EndWhile
\BState \Return L
\End
\end{algorithmic}
\end{algorithm}
\end{document}
输出