0

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

1 Answer 1

1

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

6
  • Thanks a lot. Can I ask you one more question? How to make the rand num font italic? With {$x_t \leftarrow \mathrm{rand}\,\mathrm{num}$\;}, it is showing normal font. Thanks again.
    – 11187162
    Commented Apr 1, 2022 at 9:08
  • 1
    @11187162 You can do \mathit{rand\,num}
    – egreg
    Commented Apr 1, 2022 at 9:30
  • many thanks. Is there any way to make max in {$x_t \leftarrow \max\limits_y Q(x_t,.)$\;} italic?
    – 11187162
    Commented Apr 1, 2022 at 9:59
  • @11187162 Why would you? Look in every book and you'll see that's spelled in upright letters.
    – egreg
    Commented Apr 1, 2022 at 10:09
  • this is because in the document whenever I write this equation using the math formula, it appears like italic.
    – 11187162
    Commented Apr 1, 2022 at 23:23

You must log in to answer this question.

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