Write nested if else statement inside function

algorithm2elyx

I am trying to write the following algorithm in LaTex using algorithm2e package.

enter image description here

\IncMargin{1em}
\begin{algorithm}
\caption{An example} 
\SetNoFillComment
\DontPrintSemicolon
\BlankLine
\SetKwInOut{Input}{Input}\SetKwInOut{Output}{Output}
\Input{set ($x$), $y$, $Q$, $x_t$, epsilon (\varepsilon)} 
\Output{$G_r$}
\BlankLine
$x$ = \{$x_1$, $x_2$,\dots\}; $x_t$ \in $x$\;
\\$y$ = \{$y_1$, $y_2$,\dots\}\;
\SetKwFunction{Fmain}{main}   
\SetKwProg{Fn}{Function}{:}{end}
\Fn{\Fmain{}}{
\eIf {condition 1}
{do this\;
 do this\;
\eIf {condition 11}
{$x_t$ \leftarrow $\max\limits_y Q(x_t,.)$\;}
{x_t \leftarrow  $rand$\quad$num$\;}
$G_u$ =$x_t$\;
}
{$G_u$ = 0\;}
return $G_u$\;}
\end{algorithm}

But the above code gave the following output. Would anyone please help to correct my code? Thank you.

enter image description here

Best Answer

You get many (really many) errors with that input, due to inconsistent usage of $ to delimit math formulas.

\documentclass{article}
\usepackage{algorithm2e}

\begin{document}

\IncMargin{1em}
\begin{algorithm}
\caption{An example} 
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\SetKwFunction{Fmain}{main}   
\SetKwProg{Fn}{Function}{:}{end}
\SetNoFillComment
\DontPrintSemicolon

\BlankLine
\Input{set ($x$), $y$, $Q$, $x_t$, epsilon ($\varepsilon$)} 
\Output{$G_r$}
\BlankLine
$x = \{x_1, x_2,\dots\}$; $x_t\in x$\;
$y = \{y_1, y_2,\dots\}$\;
\BlankLine
\Fn{\Fmain{}}{
  \eIf {condition 1}{
    do this\;
    do this\;
    \eIf {condition 11}
      {$x_t \leftarrow \max\limits_y Q(x_t,.)$\;}
      {$x_t \leftarrow  \mathrm{rand}\,\mathrm{num}$\;}
    $G_u = x_t$\;
  }
  {$G_u = 0$\;}
return $G_u$\;}
\end{algorithm}

\end{document}

enter image description here