4

Let us consider the following code and the algorithm corresponding to it

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

   \begin{document}

   \begin{center}
   \begin{algorithm}[H]
    \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{center}
   \end{document}

Which gives the following

enter image description here

I want to add running times for each step to my algorithm as shown below (for example)

enter image description here

So, I need to make my algorithm as a column in the table and one more column for running times of each step, how can I make my algorithm as a column inside a table?

hanugm
  • 396
  • your code does not compile. please post a complete MWE. – Nasser Jun 04 '16 at 09:38
  • Seems to be related to http://tex.stackexchange.com/questions/245316/writing-algorithm-in-a-table – Zarko Jun 04 '16 at 09:41
  • @Nasser I provided MWE. Now, for each step in my algorithm I want to add how many times each step is running, like in second diagram using table. – hanugm Jun 04 '16 at 09:54
  • I had my compiler set to use lualatex,. Your code did not compile with it. it gives errors. It compiled with pdflatex. It is always good idea to say which compiler one uses when posting an example. Try compiling your code with lualatex and see the errors. – Nasser Jun 04 '16 at 17:46

1 Answers1

4

I would set such an algorithm using a tabularx to ensure it fits the entire width of the text column, since you're adding information horizontally:

enter image description here

\documentclass{article}

\usepackage{tabularx,amsmath}

\newenvironment{algorithm}
  {\renewcommand{\caption}[1]{\multicolumn{1}{@{} X}{\scshape ##1}}%
   \noindent
   \tabularx{\linewidth}{@{} >{\quad} X l l @{}}  
  }
  {\endtabularx}

\begin{document}

\begin{algorithm}
  \caption{Insertion-Sort($A$)}                      & \textit{cost} & \textit{times}             \\
  \textbf{for} $j = 2$ \textbf{to} $A.\text{length}$ & $c_1$         & $n$                        \\
  $k = A[j]$                                         & $c_2$         & $n - 1$                    \\
  \itshape // Insert $A[j]$ into the sorted                                                       \\
  \quad \itshape   sequence $A[1, \dots, j - 1]$.    & $0$           & $n - 1$                    \\
  $i = j - 1$                                        & $c_4$         & $n - 1$                    \\
  \textbf{while} $i > 0$ \textbf{and} $A[i] > k$     & $c_5$         & $\sum_{j = 2}^n t_j$       \\
  \quad $A[i + 1] = A[i]$                            & $c_6$         & $\sum_{j = 2}^n (t_j - 1)$ \\
  \quad $i = i - 1$                                  & $c_7$         & $\sum_{j = 2}^n (t_j - 1)$ \\
  $A[i + 1] = k$                                     & $c_8$         & $n - 1$
\end{algorithm}

\end{document}

I've made a rough copy of the expected columnar output. There are a multitude of options one can add to the process; it all depends on how many of these you want to set and whether certain things require automation or not.

Werner
  • 603,163