# What is the structure of this algorithm?

I want to type this:

I started to do it by

\begin{subequations}

0. \text{la la la } 0 + 1 = 1
\end{equation

1. he he he

\end{subequations


but I think it is not the good way.

What is the structure of the algorithm?

• I think you should look into the algorithmicx package (see tag algorithmicx) which can do these kind of things quite nicely with references etc. – zeroth Nov 27 '13 at 12:44
• It seems a simple enumerate to me. – karlkoeller Nov 27 '13 at 16:07

A job for good old tabbing:

\documentclass{article}

\usepackage{amsmath,amsthm}

\newtheoremstyle{mystyle}%  <name>
{\topsep}%                <space above>
{\topsep}%                <space below>
{\normalfont}%            <body font>
{}%                       <indent amount>

\theoremstyle{mystyle}
\newtheorem{algorithm}{Algorithm}[section]

\newcommand{\algoqed}{%
\makebox[0pt][l]{%
\makebox[\textwidth][r]{\rule{0.4em}{0.7em}}%
}%
}

\newcounter{algostep}
\newlength{\stepwidth}
\newcommand{\step}{%
\refstepcounter{algostep}%
\makebox[\stepwidth][r]{\thealgostep. }%
}
\newcommand{\initialstep}[2][1]{%
\setcounter{algostep}{#2}%
\settowidth{\stepwidth}{#1. }%
}

\begin{document}

\setcounter{section}{6}   %just for the example
\setcounter{algorithm}{1} %just for the example

\begin{algorithm}
\initialstep[10]{0}
The Preconditioned Conjugate Gradient Method for Solving the Symmetric
Positive Definite System $Ax = b$, Starting with $x^{(0)}$
\begin{tabbing}
\step
\= Input stopping criteria, $\epsilon$ and $k_{\text{max}}$.\\
\> Set $k=0$, $r^{(k)}=b-Ax^{(k)}$, $s=Ar^{(k)}$,
$p=M^{-1}s^{(k)}$, $y^{(k)}=M^{-1}r^{(k)}$\\
\> and $\gamma^{(k)}=y^{(k)\mathrm{T}}s^{(k)}$.\\
\step\label{step1}
\> If $\gamma^{(k)}\le\epsilon$, set $x=x^{(k)}$ and terminate.\\
\step
\> Set $q^{(k)}=Ap^{(k)}$.\\
\step
\> Set $\alpha^{(k)}=\frac{\gamma^{(k)}}{\|q^{(k)}\|^2}$.\\
\step
\> Set $x^{(k+1)}=x^{(k)}+\alpha^{(k)}p^{(k)}$.\\
\step
\> Set $r^{(k+1)}=r^{(k)}-\alpha^{(k)}q^{(k)}$.\\
\step
\> Set $s^{(k+1)}=A^{(k+1)}$.\\
\step
\> Set $y^{(k+1)}=M^{-1}r^{(k+1)}$.\\
\step
\> Set $\gamma^{(k+1)}=y^{(k+1)\mathrm{T}}s^{(k+1)}$.\\
\step
\> Set $p^{(k+1)}=M^{-1}s^{(k+1)}+\frac{\gamma^{(k+1)}}{\gamma^{(k)}}p^{(k)}$.\\
\step
\> If \= $k<k_{\text{max}}$,\\
\>    \> set $k\gets k + 1$ and go to step \ref{step1};\\
\> otherwise\\
\>    \> issue message that\\
\algoqed
\>    \> algorithm did not converge in $k_{\text{max}}$ iterations''.
\end{tabbing}
\end{algorithm}

\end{document}


With \initialstep[<final>]{<initial>} one sets the initial and final number of steps (actually <final> is just used for the number of digits, default 1). Steps in the algorithm are introduced by \step that can be followed by a \label. The first \step is followed by \= to set the tab stop.

The final line can be prefixed by \algoqed for the black box.

I would avoid all that text in boldface; the header is sufficient to give it prominence. I maintained the original capitalization, but it's really ugly.

• Thank you for your answer! I think this is the best one, since then you clearly see what is inside the algorithm. – Léo Léopold Hertz 준영 Nov 28 '13 at 7:36

IMHO, it is done with something like this

\documentclass{article}

\usepackage{amsmath,amsthm,enumitem}

\newtheoremstyle{mystyle}%  <name>
{\topsep}%                <space above>
{\topsep}%                <space below>
{\bfseries}%              <body font>
{}%                       <indent amount>

\theoremstyle{mystyle}
\newtheorem{algorithm}{Algorithm}[section]
\renewcommand{\qedsymbol}{\rule{0.4em}{0.7em}}

\setlist[enumerate]{%
parsep=0pt,
leftmargin=1.25\parindent,
listparindent=\parindent
}

\begin{document}

\setcounter{section}{6}   %just for the example
\setcounter{algorithm}{1} %just for the example

\begin{algorithm}
The Preconditioned Conjugate Gradient Method for Solving the Symmetric
Positive Definite System $Ax = b$, Starting with $x^{(0)}$
\end{algorithm}
\begin{enumerate}
\setcounter{enumi}{-1}
\item Input stopping criteria, $\epsilon$ and $k_{\text{max}}$.\\
Set...
\item ...
\setcounter{enumi}{9}   %just for the example
\item If $k<k_{\text{max}}$,\\
\indent set k = k + 1 and go to step 1;\\
otherwise\\
\indent issue message that\\
\indent algorithm did not converge in $k_{\text{max}}$ iterations''.\qed
\end{enumerate}

\end{document}