如何在算法的顶部和底部编写开始和结束语句?

如何在算法的顶部和底部编写开始和结束语句?

我怎样才能像这样在以下代码的顶部和底部写下“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}

输出

在此处输入图片描述

相关内容