4

My aim is to fit this long single-line algorithm into a double column page lay out, just as it is shown in the below, in 3 different lines, but I cannot use \State and I do not know why and how can I make this to happen.

  \documentclass{article}
    \usepackage[]{algorithm2e}

\begin{document}

     \begin{algorithm}[H]

     \caption{Computing Levenshtein edit distance }
     \label{editComputing}

    $D(i,j)=min{D(i-1,j-1)+\gamma(A<i> \to B<j>),
                D(i-1,j)+ \gamma(A<i> \to \Lambda),
                D(i,j-1) +\gamma(\Lambda \to B<j>)}$

    \end{algorithm}
\begin{document} 
2

Don't use the [H]ere specifier, since it's not allowed in twocolumn mode by-design. Also, I've used some \phantoms to provide the appropriate layout of the function across three lines.

Note that \State is used by algorithmicx's algpseudocde package, not algorithm2e. Instead, algorithm2e uses an end-of-line \; without a \State.

In order to make the math display fit, it's better to use \langle...\rangle rather than <...>.

enter image description here

\documentclass[twocolumn]{article}

\usepackage{algorithm2e,lipsum,amsmath}

\begin{document}

\lipsum[1]

\begin{algorithm}
  \DontPrintSemicolon
  \caption{Computing Levenshtein edit distance }
  $D(i,j) = \min\bigl\{D(i-1,j-1) + \gamma(A\langle i\rangle \to B\langle j\rangle),$\;
  $\phantom{D(i,j) = \min\bigl\{} D(i-1,j) + \gamma(A\langle i\rangle \to \Lambda),$\;
  $\phantom{D(i,j) = \min\bigl\{} D(i,j-1) + \gamma(\Lambda \to B\langle j\rangle)\bigr\}$\;
\end{algorithm}

\lipsum[2]

\end{document}
  • sorry for the late response, carried away with something else. When I use your code in my code, the caption is not aligned with the formula. Moreover, it is printed at the top part of next page, unlike what you have shown in this answer. Do you mind to let me know what could go wrong? – user3049183 Jun 20 '16 at 7:01
2

i don't know any reason not to use align* for this; requires amsmath.

\documentclass[twocolumn]{article}
\usepackage{algorithm2e}
\usepackage{amsmath}

\begin{document}
\noindent Some text preceding.
\begin{algorithm}
  \DontPrintSemicolon
  \caption{Computing Levenshtein edit distance }
  \abovedisplayskip=0pt
  \begin{align*}
  D(i,j) = \min\bigl\{& D(i-1,j-1) + \gamma(A\langle i\rangle \to B\langle j\rangle),\\
  & D(i-1,j) + \gamma(A\langle i\rangle \to \Lambda),\\
  & D(i,j-1) + \gamma(\Lambda \to B\langle j\rangle)\bigr\}
  \end{align*}
\end{algorithm}

\noindent Some text following.
  \begin{align*}
  D(i,j) = \min\bigl\{& D(i-1,j-1) + \gamma(A\langle i\rangle \to B\langle j\rangle),\\
  & D(i-1,j) + \gamma(A\langle i\rangle \to \Lambda),\\
  & D(i,j-1) + \gamma(\Lambda \to B\langle j\rangle)\bigr\}
  \end{align*}
\end{document}

output of example code

the line \abovedisplayskip=0pt is included to eliminate excessive space at the top of the algorithm block; this does not happen automatically in a "restricted" environment. (the second instance of the code is included to demonstrate that this adjustment does not persist.)

edit: the space above and below the algorithm defaults to \smallskip. the command provided to change this is \SetAlgoSkip{...} and suggested values are medskip or bigskip. notice the absence of the backslash; these are not control sequences, and cannot be substituted by actual dimension values.

  • Maybe this is just me, but I cant see any difference the two formulas. – user3049183 Jun 20 '16 at 7:07
  • @user3049183 -- there's no difference in the formulas. the difference is in the amount of space between the formulas and the preceding text. – barbara beeton Jun 20 '16 at 11:59
  • Which of these two spacing is recommended? I mean, since it seems the formula belongs to an academic article, which of these two spacing scheme looks more appropriate? – lonesome Jun 20 '16 at 13:13
  • the goal is to have the same spacing above the algorithm that begins with the align as it would be if the algorithm begins with text. the space above a "text" algorithm is slightly larger than the space between text and ordinary display math, and is "correct" from that point of view in this example. if the space from the align hadn't been removed for the algorithm, the space would have been as large as those two spaces added together -- much too large. – barbara beeton Jun 20 '16 at 13:26
  • I see. I realized that the caption has very thin space with the text in the next paragraph. How to fix this? – user3049183 Jun 22 '16 at 5:41

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.