How can I format the algorithm shown in the following image without using the algorithm environment nor tabular environment and how can I make the proper cross-reference so that \ref{Algorithm 2} may be turned into a hyperlink to the algorithm itself.
1 Answer
- To get multi-level indentation, I would suggest to use the
enumitempackage: Define a new type of list (saylevel) that has no item symbol, and thus only provides an indentation that can be nested. - To get user-defined references, you need to define a counter (say
algo) and call\refstepcounterat the point where you want your reference to point to.
I combined these two things (together with horizontal lines) in a definition of a new environment àlgorithm:
\documentclass{article}
\usepackage{hyperref}
\usepackage{enumitem}
\newlist{level}{itemize}{4}
\setlist[level]{label={},noitemsep,topsep=0pt}
\newcounter{algo}
\renewcommand{\thealgo}{\arabic{algo}.}
\newenvironment{algorithm}[1]{%
\refstepcounter{algo}%
\paragraph{Algorithm \thealgo}#1%
\vspace{2pt}\hrule\vspace{5pt}%
\begin{level}
}{%
\end{level}%
\vspace{5pt}\hrule\vspace{\baselineskip}%
}
\begin{document}
We summarize the PF-EKF in Algorithm~\ref{algo:ekf}
\begin{algorithm}{Particle filter with EKF proposal}\label{algo:ekf}
\item \textbf{Initialize:}
\item Draw the particles by using $\{x_0^f\}_{i=1}^M\!\sim\!p(x_0)$
\item \textbf{for} k=1 to N \textbf{do}
\begin{level}
\item \textbf{Prediction Step:}
\item Draw the particles by using the Equation 52
\item \textbf{Measurement Step:}
\item Update the weight by using the Equation 56
\item \textbf{Resample Step}
\end{level}
\item end for
\end{algorithm}
\end{document}
-
@ Tiuri, Is there a possibility to make the horizontal line to be as wide as it need in order to accommodate the content of the algorithm.user137684– user1376842017-08-28 21:42:39 +00:00Commented Aug 28, 2017 at 21:42
-
@user137684 No, I don't know a way of automatically achieving this with an itemize-type environment. But feel free to take the code of my answer and ask this as a new question.Tiuri– Tiuri2017-08-29 07:32:18 +00:00Commented Aug 29, 2017 at 7:32


algorithmlike environments here?