0

I am trying to create a kind of pseudocode in Latex resembling the one shown below image.

What I tried is:

1. Convert R to a binary region using the threshold $\theta_{0}$\\

2. Assume N is the sum of the number of non-zero pixels within $F_{l}$ and $F_{r}$.\\

3. If N is larger than a predefined threshold $\beta$, then\\

            \begin{center}
            $\theta_{0} = \theta_{0} + \Delta\theta$\\
            Repeat the procedure from step 1
            \end{center}
   Else

            \begin{center}
            $\theta = \theta_{0}$
            \end{center}
   End

Its not turning out like I want though. Can anyone please suggest how to fix this?

Tobi
  • 56,353
learner
  • 251

4 Answers4

3

Here is an algorithm2e implementation:

enter image description here

\documentclass{article}
\usepackage{algorithm2e}
\LinesNumbered% Number algorithm lines
\usepackage{amsmath}
\begin{document}

\begin{algorithm}[H]
  \SetAlgoLined
  \KwData{$R$, $\beta$}
  \KwResult{$\theta_0$}
  Convert~$R$ to a binary region using the threshold $\theta_{0}$\; \label{first_step}
  Assume~$N$ is the sum of the number of non-zero pixels within~$F_l$ and~$F_r$\;
  \eIf{\text{$N$ is larger than a predefined threshold~$\beta$}}{
    $\theta_{0} = \theta_{0} + \Delta\theta$\;
    Repeat the procedure from Step~\ref{first_step}\;
  }{
    $\theta = \theta_{0}$\;
  }  
  \caption{How to write algorithms}
\end{algorithm}

\end{document}
Werner
  • 603,163
3

A “hand made” solution reproducing the layout you showed

\documentclass{article}
\usepackage{enumitem}
\newlist{pseudocode}{enumerate}{2}
\setlist[pseudocode,1]{label=\arabic*.,ref=\arabic*}
\setlist[pseudocode,2]{label={},ref=\arabic*,nosep}

\begin{document}

\begin{pseudocode}
\item\label{step:convert} Convert $R$ to a binary region using the threshold $\theta_{0}$.

\item Assume $N$ is the sum of the number of non-zero pixels within $F_{l}$ and $F_{r}$.

\item If $N$ is larger than a predefined threshold $\beta$, then
    \begin{pseudocode}
    \item $\theta_{0} = \theta_{0} + \Delta\theta$
    \item Repeat the procedure from step \ref{step:convert}
    \end{pseudocode}
  Else
    \begin{pseudocode}
    \item $\theta = \theta_{0}$
    \end{pseudocode}
  End
\end{pseudocode}
where $\Delta\theta$, $\beta$ are two constants.

\end{document}

enter image description here

egreg
  • 1,121,712
  • THANKS!!! Just a small editing I had to do: placing \label{step:convert} at the end of the line :) – learner Oct 06 '14 at 23:33
1

The easiest way is to use {enumerate} and the normal equation environments ({equation} and {gather} in this case – the * prevents numbering). The fleqn document class option sets the equations left aligned; omit this options to center them.

easy pseudo code

\documentclass[fleqn]{article}

\usepackage{mathtools}

\begin{document}
\begin{enumerate}
\item Convert R to a binary region using the threshold $\theta_{0}$
\item Assume N is the sum of the number of non-zero pixels within $F_{l}$ and $F_{r}$.
\item If N is larger than a predefined threshold $\beta$, then
    \begin{gather*}
    \theta_{0} = \theta_{0} + \Delta\theta\\
    \text{Repeat the procedure from step 1}
    \end{gather*}
    Else
    \begin{equation*}
    \theta = \theta_{0}
    \end{equation*}
    End
\end{enumerate}
\end{document}

More information can be found in the package manuals and LaTeX beginners books ;-)

Tobi
  • 56,353
1

BINGO!!! I created a solution! Works like a charm :) Remember to use: \usepackage{algpseudocode}

\begin{algorithmic}
\State Convert R to a binary region using the threshold $\theta_{0}$\\

\State Assume N is the sum of the number of non-zero pixels within $F_{l}$ and $F_{r}$\\

\If {$N \textgreater \beta$}
    \State $\theta_{0} = \theta_{0} + \Delta\theta$
    \State Repeat the procedure from step 1
\Else
    \State $\theta = \theta_{0}$
\EndIf

\end {algorithmic}  
dustin
  • 18,617
  • 23
  • 99
  • 204
learner
  • 251
  • 1
    Instead of saying remember to include blank, you could instead complete your MWE. – dustin Oct 06 '14 at 23:14
  • MWE? I am not sure I understood what u mean...i just posted my solution...its working fine. Might help somebody else :) – learner Oct 06 '14 at 23:16
  • 3
    Here is the canned message for MWE click the links to read up on it: Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – dustin Oct 06 '14 at 23:17
  • 1
    +1 for a newcomer answering his/her own question. Edit it now so that someone can cut out your code, paste it and have it compile. (It's wonderful sometimes how urgency can beget creativity.) – Ethan Bolker Oct 07 '14 at 01:07