4

How can I write 'begin' and 'end' statement at the top and bottom the following code like this?A sample pseuodcode with begin and end statement

Besides using algorithm and algopseudocode package I can write Require and Ensure statement. Those are not properly aligned. Is there any way to align like the attached pic?

    % 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}

Here is the online code

4
  • Welcome to TeX.SX! Please add a minimal working example (MWE) starting with \documentclass{...} and ending with \end{document} so that users willing to help will have a starting point without the need of additional setup.
    – Pier Paolo
    Commented Jan 19, 2015 at 17:41
  • I think this will just be a matter of making your own block, as in algorithmicx section 4.2. Something like \algblock[name]{begin}{end} with \begin and \end statements as needed. Commented Jan 19, 2015 at 18:01
  • @MikeRenfro \begin and \end are already defined... You can not use them. Commented Jan 19, 2015 at 21:26
  • Correct. My mistake. Commented Jan 19, 2015 at 21:27

1 Answer 1

5

Just define (in algpseudocode style)

\algdef{SE}{Begin}{End}{\textbf{begin}}{\textbf{end}}

and then use \Begin and \End as in the following example:

    % 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} 

Output

enter image description here

Just a remark. You should not write

\EndWhile\\
\Return L

but

\EndWhile
\State \Return L

as in my example.


If you want a different indentation for \Return you can do the following.

Add

\def\BState{\State\hskip-.5em}

in the preamble and use \BState instead of \State in front of \Return.

MWE

    % 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}

Output

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .