# Central line number for large function with algorithm2e

I have an algorithm (using algorithm2e) where I need a big function, so I want to enter it as an equation. But the line numbering seems a bit weird, as it numbers an empty line. I want the number to be central (technically on the level of the fraction bar).

I have

\documentclass[12pt]{report}
\usepackage[ruled,linesnumbered]{algorithm2e} % nice and easy (?) pseudocode

\begin{document}

\begin{algorithm}
\caption{A cool algorithm}
\ForEach {item $\in$ set}{
$\frac {numerator} {denominator}$
}
\end{algorithm}

\end{document}


but this results in:

I found this as a similar question, but it did not work properly. (It added a wrong number at the correct spot). Numbering works properly for single-line equations, but then the fraction is too small to read.

Thanks for the help

• Uhm, algorithm2e has an environment algomathdisplay for this but it seems to be faulty. But if the problem of the inline math fraction is only the size, would a simple \ForEach{item $\in$ set}{$\displaystyle\frac{numerator}{denominator}$} be enough? Jun 24 '20 at 15:47
• I have to be honest, I did not know of algomathdisplay. Tried it, as you said it is faulty. I did not think on displaystyle. That might do, thank you Jun 24 '20 at 16:01

## 1 Answer

I propose here two solutions: one uses simply inline math preceded by \displaystyle; the other uses a newly defined environment which just adds some space around the math expression (also typeset in \displaystyle)

\documentclass[12pt]{report}
\usepackage[ruled,linesnumbered]{algorithm2e}

\newenvironment{varalgomathdisplay}%
{\qquad$\vcenter\bgroup\kern2ex\hbox\bgroup$\displaystyle}%
{$\egroup\kern1ex\egroup$}

\begin{document}

\begin{algorithm}
\caption{A cool algorithm}
\ForEach {item $\in$ set}{$\displaystyle\frac{num}{den}$}
\end{algorithm}

\begin{algorithm}
\caption{Another cool algorithm}
\ForEach {item $\in$ set}{\begin{varalgomathdisplay}\frac{num}{den}\end{varalgomathdisplay}}
\end{algorithm}

\end{document}