5

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 :)

1

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}
0

One possibility is

\documentclass{scrartcl}

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

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

but this does not solve the problem, when wanting more than inline math mode..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.