0

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.

enter image description here

  • 2
    What is reason of not using algorithm like environments here? – user31729 Aug 28 '17 at 10:27
  • 3
    While you wait for an answer to this question, can you go back to your previous ones and look if the answers solve your problems and accept them, if they do? – samcarter_is_at_topanswers.xyz Aug 28 '17 at 10:38
  • @christian Hupfer, I am writing a one column format of a paper to be send to an elsevier journal and my algorithm is too long (3 pages) and using the algorithm environment causes problem when I try to split the algorithm. – user137684 Aug 28 '17 at 10:59
  • 2
    @user137684: That's an explanation that should have been done in the post ;-) – user31729 Aug 28 '17 at 11:14
3
  • To get multi-level indentation, I would suggest to use the enumitem package: Define a new type of list (say level) 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 \refstepcounter at 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}

enter image description here

| improve this answer | |
  • @ 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 Aug 28 '17 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 Aug 29 '17 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.