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?

10
  • You're probably interested in Print programs with its proper syntax.
    – Werner
    Oct 6, 2014 at 22:57
  • could you please provide a working solution to the pseudocode above? its urgent :(
    – learner
    Oct 6, 2014 at 23:00
  • Please use proper tags for your questions ;-) none of the ones you used had something to do with the question …
    – Tobi
    Oct 6, 2014 at 23:03
  • 1
    … Your question leaves all the effort to our community, even typing the essentials of a TeX document such as \documentclass{}...\begin{document} etc. As it is, most of our users will be very reluctant to touch your question, and you are left to the mercy of our procrastination team who are very few in number and very picky about selecting questions. You can improve your question by adding a minimal working example (MWE) that more users can copy/paste onto their systems to work on. If no hero takes the challenge we might have to close your question.
    – Tobi
    Oct 6, 2014 at 23:11
  • 1
    Most of us would say that egreg did...
    – alexis
    Oct 6, 2014 at 23:54

4 Answers 4

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

1
  • THANKS!!! Just a small editing I had to do: placing \label{step:convert} at the end of the line :)
    – learner
    Oct 6, 2014 at 23:33
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}
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 ;-)

2
  • Not enough reputation to vote it up :( But my solution works smoothly as well :) Thanks!!!
    – learner
    Oct 6, 2014 at 23:14
  • This however globally modifies the placement of math displays.
    – egreg
    Oct 6, 2014 at 23:23
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}  
4
  • 1
    Instead of saying remember to include blank, you could instead complete your MWE.
    – dustin
    Oct 6, 2014 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 6, 2014 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 6, 2014 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.) Oct 7, 2014 at 1:07

Not the answer you're looking for? Browse other questions tagged .