5

I am currently working on runtime in my algorithm. I have currently set the algorithm up, and it looks like the following:

My Algorithm

I need to insert 2 new columns beside the current text. Each column should be able to hold some text, so that it would look like the following:

enter image description here

How would I go around doing this?

My current code can be seen below:

\begin{algorithm}
\caption{Insertion-Sort køretid}\label{AlgRun}
\begin{algorithmic}[1]
    \Procedure{insertion-sort}{$A,n$}
        \State $inversions = 0$
        \State \textit{L}[0..\textit{n} - 1] be a new array.
        \For{q=0}{n-1}
            \State L[q] = A[q]
        \EndFor
        \For{j=1}{n-1}
            \State $key = L[j]$
            \State $i = j - 1$
            \While{$ i \geq 0$ \textbf{and} $L[i] > key$}
                \State L[ i + 1] = L[ i ]
                \State $j = i - 1$
                \State $inversions = inversions + 1$
            \EndWhile
            $L[i + 1] = key$
        \EndFor
        \Return{inversions}
    \EndProcedure
\end{algorithmic}
\end{algorithm}
  • Can you turn your code fragment into a complete, compilable document. It's much more helpful for the people who may want to try to help you. – Alan Munn Sep 19 '16 at 3:16
2

The algorithmicx package provides a \Comment command which does what you need, and is customizable. In this example I've created a macro with two arguments, one for each column of the annotation. Then each line gets inserted with a \Comment command. I've also redefined the standard \Comment format macro (\algorithmiccomment) with the \algrenewcommand macro to remove the arrow that shows up by default.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{algpseudocode}
\usepackage{algorithm}
\usepackage{array}
\newcolumntype{L}{>{$}l<{$}}
\newcommand\cpair[2]{%
\begin{tabular}{LL}#1 & #2\end{tabular}}
\algrenewcommand{\algorithmiccomment}[1]{\hfill#1}
\begin{document}
\begin{algorithm}
\caption{Insertion-Sort køretid}\label{AlgRun}
\begin{algorithmic}[1]
    \Procedure{insertion-sort}{$A,n$}   \Comment \cpair{c_1}{n=0}
        \State $inversions = 0$         \Comment \cpair{c_1}{n=1}
        \State \textit{L}[0..\textit{n} - 1] be a new array.
        \For{q=0}{n-1}
            \State L[q] = A[q]
        \EndFor
        \For{j=1}{n-1}
            \State $key = L[j]$
            \State $i = j - 1$
            \While{$ i \geq 0$ \textbf{and} $L[i] > key$}
                \State L[ i + 1] = L[ i ]
                \State $j = i - 1$
                \State $inversions = inversions + 1$
            \EndWhile
            $L[i + 1] = key$
        \EndFor
        \Return{inversions}
    \EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}

output of code

  • what's the package name you refer to in the first line? (not the same as what's in the code. confusing.) – barbara beeton Sep 19 '16 at 19:46
  • @barbarabeeton The naming conventions of these packages are somewhat confusing (and another reason for asking the OP to post a MWE). The core package is algorithmicx, which provides a set of macros for formatting algorithmic commands. The package comes with some predefined sets of commands, one of which is the algopseudocode package, which the OP is using. But if you do texdoc algopseudocode you will reach the documentation for algorithmicx. And this package has nothing to do with the algorithm package, which is used to provide floating algorithms. :) – Alan Munn Sep 19 '16 at 20:16
  • thanks for the explication (but the package named in the first line is spelled wrong -- missing an "m"). – barbara beeton Sep 19 '16 at 20:58

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.