[Tex/LaTex] Center line number for equation in algorithm

algorithmsequationsline-numbering

I would like to use a equation inside an algorithm. The following pseudo code generates a line number, which is not centered with the equation:

\documentclass{scrartcl}

\usepackage{amsmath}
\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}
\begin{algorithm}
\begin{algorithmic}[1]
    \State{}
    \begin{equation*}
        e=mc^2
    \end{equation*}
\end{algorithmic}
\end{algorithm}
\end{document}

resulting in:

Algorithm without centered line next to equation

What I would like to have is something like:

Algorithm with centered line next to equation

Is there a possibility to center the line number vertically at the hight of the equation?
Thanks for your help 🙂

Best Answer

You can enclose the math environment in a \vcenter which itself has to been between dollars

$\vcenter{....}$

Sample output

\documentclass{scrartcl}

\usepackage{amsmath}
\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}
\begin{algorithm}
\begin{algorithmic}[1]
  \State{} $\vcenter{
  \begin{equation*}
        e=mc^2
  \end{equation*}
  }$
  \State{} $\vcenter{
  \begin{align*}
    x &= y^2 \\
    &= z + t
  \end{align*}
  }$
\end{algorithmic}
\end{algorithm}
\end{document}
Related Question