2

Using package algorithm2e.

Want to write the algorithm within an equation environment, so that it has an equation number on the right margin.

But I cannot just include it inside there, LaTeX complains (probably because the algorithm is not supposed to appear within an equation).

How can I do that?

2

Set the algorithm before the equation inside a box, and then use that box as your equation content. There is no official need for this, but it may make your code a little cleaner. Alternatively, set the algorithm inside an appropriately-sized minipage:

enter image description here

\documentclass{article}

\usepackage{algorithm2e}
\newsavebox{\algbox}

\begin{document}

\begin{lrbox}{\algbox}
\begin{minipage}{0.9\linewidth}
  \begin{algorithm}[H]
    \SetAlgoLined
    \KwData{this text}
    \KwResult{how to write algorithm with \LaTeX2e }
    initialization\;
    \While{not at end of this document}{
      read current\;
      \eIf{understand}{
        go to next section\;
        current section becomes this one\;
      }{
        go back to the beginning of current section\;
      }
    }
    \caption{How to write algorithms}
  \end{algorithm}
\end{minipage}
\end{lrbox}

\begin{equation}
  \usebox{\algbox}
\end{equation}

\end{document}

Note the use of the [H] float specifier. This removes any floating behaviour from the algorithm environment and is critical in terms of capturing the algorithm inside a box and/or making it stick where you want it to (like in a specific equation environment).

Also note that I've set the algorithm inside a minipage that is slightly narrower than the text block width - 0.9\linewidth. That way it ensures that the equation number will fit next to the algorithm. If you have a wider equation number, you may have to adjust this length.

|improve this answer|||||
  • It should be easy to build an algoequation environment – egreg Mar 1 '17 at 22:21
1

A variant of Werner's code. I think that one shouldn't try centering the algorithm on the page, but setting it at a fixed distance from the margin (here I set it to zero). Also the input is simplified.

A caption shouldn't be necessary, I believe. Instead, I provided support for a standard \label command.

\documentclass{article}
\usepackage{amsmath}
\usepackage{algorithm2e}

\usepackage{lipsum}

\newsavebox{\algbox}
\newenvironment{algoequation}
 {\begin{lrbox}{\algbox}
  \begin{minipage}{0.9\linewidth}
  \setlength{\algomargin}{0pt}% or the margin you want
  \begin{algorithm}[H]
  \let\label\algolabel\gdef\thisalgolabel{}}
 {\end{algorithm}
  \end{minipage}
  \end{lrbox}
  \begin{equation}
  \if\relax\detokenize\expandafter{\thisalgolabel}\relax
  \else
    \label{\thisalgolabel}
  \fi
  \hfilneg\usebox{\algbox}\hspace{10000pt minus 1fil}
  \end{equation}}
\newcommand{\algolabel}[1]{\gdef\thisalgolabel{#1}}

\begin{document}

Algorithm~\eqref{test} is important.

\lipsum*[2]
\begin{algoequation}\label{test}
  \SetAlgoLined
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }
  initialization\;
  \While{not at end of this document}{
    read current\;
    \eIf{understand}{
      go to next section\;
      current section becomes this one\;
    }{
      go back to the beginning of current section\;
    }
  }
\end{algoequation}
\lipsum[3]

\end{document}

Act on the line with \algoindent if you want to implement a different distance from the left margin.

The width 0.9\linewidth should be adjusted depending on the maximum width of the equation numbers.

enter image description here

|improve this answer|||||

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.