I see a few papers such as this
and this
that uses an indentation based display. Note that they do not have an endif or an endwhile. What algorithm package is being used here? Or how do I get a similar effect?
Asked
Active
Viewed 4,352 times
1
Werner
- 603,163
Rahul Gopinath
- 123
-
Related (and possibly duplicate): https://tex.stackexchange.com/q/51295/254823 – kcoskun Oct 25 '23 at 10:09
2 Answers
3
The following is a starting point, using algorithm2e:
\documentclass{article}
\usepackage{xcolor,amsmath}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\DontPrintSemicolon
% Define pseudocode formatting
\renewcommand{\KwSty}[1]{\textnormal{\textcolor{blue!90!black}{\ttfamily\bfseries #1}}\unskip}
\renewcommand{\ArgSty}[1]{\textnormal{\ttfamily #1}\unskip}
\SetKwComment{Comment}{\color{green!50!black}// }{}
\renewcommand{\CommentSty}[1]{\textnormal{\ttfamily\color{green!50!black}#1}\unskip}
\newcommand{\assign}{\leftarrow}
\newcommand{\var}{\texttt}
\newcommand{\FuncCall}[2]{\texttt{\bfseries #1(#2)}}
\SetKwProg{Function}{function}{}{}
\renewcommand{\ProgSty}[1]{\texttt{\bfseries #1}}
\begin{document}
\begin{algorithm}
\caption{Dynamic PCA}
\Comment{$\theta_{\mathrm{exp}}$ is globally given, and initially set to $\infty$.}
\Function{ExpandBasisIfInteresting($B$, $\Sigma$, $\vec{x}$)}{
\If{$\var{loss} > \theta_{\mathrm{exp}}$}{
$\var{loss} \assign \sqrt{\lVert \vec{x} \rVert^2 - \lVert \vec{x}^T B \rVert^2}$
\Comment{By Pythagoras}
$B, \Sigma \assign \FuncCall{Append}{$B$, $\Sigma$, $\vec{x}$}$\;
$B \assign \FuncCall{GramSchmidt}{$B$}$\;
}
$\theta_{\mathrm{exp}} \assign \FuncCall{UpdateLoss}{$\theta_{\mathrm{exp}}$, \var{loss}}$\;
\Return{$B$, $\Sigma$}\;
}
\Function{PeriodicDecompose($B$, $\Sigma$)}{
\If{\FuncCall{IsOneMinutePassed}{}}{
$B, \Sigma \assign \FuncCall{PCA}{$B$, $\Sigma$}$\;
}
\Return{$B$, $\Sigma$}\;
}
\Comment{The main function}
\Function{DynPCA($B$, $\Sigma$, $\vec{x}$, $s$)}{
$B, \Sigma \assign \FuncCall{ExpandBasisIfInteresting}{$B$, $\Sigma$, $\vec{x}$}$\;
$\Sigma \assign \FuncCall{UpdateCovMatrix}{$B$, $\Sigma$, $\vec{x}$, $s$}$\;
$B', \Sigma' \assign \FuncCall{PeriodicDecompose}{$B$, $\Sigma$}$\;
\Return{$B'$, $\Sigma'$}
}
\end{algorithm}
\end{document}
Werner
- 603,163
0
The algorithm2e package offers the above (not by default, but it is quite customizable).
vonbrand
- 5,473
-
4Maybe you could add a link to the documentation and a minimal example to make this answer more useful. – dexteritas Feb 24 '20 at 16:28
-
