Search code examples
latexpseudocode

Formatting problem in latex , pseudo code


I am writing a report for my thesis in latex, most of the report is complete, one of the things left is to write up the pseudo-code for the algorithm used. The inserted picture is the pseudo-code I am trying to write. Main Pseudo Code example

I want a little help in writing up the for loops and figuring out the formatting, This is what I have done:

\documentclass[12pt]{article}
\usepackage{array}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{blindtext}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[intoc]{nomencl}
\usepackage[nottoc]{tocbibind}


\title{test}
\begin{document}

\maketitle


\begin{algorithm}
\caption{pseudo code}\label{alg:Pseudo code}
\begin{algorithmic}[1]
\State Define the maximum number of iteration.
\For {i = 1: N flies} 
\State x_i.fitness = f(x_i)   
    \EndFor  
\State x_s = argmax[f(x_i)], i \in {1,.....,N}
\State \For {i = 1:N and i \neq s}
        \State \For{d = 1:D dimensions}
        \If{U(0,1) < \triangle}
\end{algorithmic}
\end{algorithm}
\end{document}

Solution

    • \EndIf and \EndFor are missing

    • If you want to use math symbols like subscripts, \in or \triangle (shouldn't this rather be a delta?), you need to place them in math mode


    \documentclass[12pt]{article}
    \usepackage{array}
    \usepackage{multirow}
    \usepackage{booktabs}
    \usepackage{blindtext}
    \usepackage{algorithm}
    \usepackage{algpseudocode}
    \usepackage[utf8]{inputenc}
    \usepackage[english]{babel}
    \usepackage[intoc]{nomencl}
    \usepackage[nottoc]{tocbibind}
    
    
    \title{test}
    \begin{document}
    
    \maketitle
    
    
    \begin{algorithm}
    \caption{pseudo code}\label{alg:Pseudo code}
    \begin{algorithmic}[1]
    \State Define the maximum number of iteration.
    \For {i = 1: N flies} 
    \State $x_i$.fitness = f($x_i$)   
        \EndFor{} i
    \State $x_s$ = argmax[f($x_i$)], i $\in$ {1,.....,N}
    \State \For {i = 1:N and i $\neq$ s}
            \State \For{d = 1:D dimensions}
            \If{U(0,1) $< \triangle$}
              Quack
            \Else
              Quack Quack
            \EndIf
            \EndFor{} d
    \EndFor{} i
    \end{algorithmic}
    \end{algorithm}
    \end{document}