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?

  • your code does not compile. please post a complete MWE. – Nasser Jun 4 '16 at 9:38
  • Seems to be related to tex.stackexchange.com/questions/245316/… – Zarko Jun 4 '16 at 9: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 4 '16 at 9: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 4 '16 at 17:46
3

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.

  • the \hline doesnot work for marking lines, can we do something for that. it will just make my code as i want. – Shahensha Khan Sep 26 '17 at 6:10
  • @ShahenshaKhan: You mean vertical lines? – Werner Sep 26 '17 at 6:13
  • i mean horizontal line after each row – Shahensha Khan Sep 26 '17 at 6:18
  • @ShahenshaKhan: Something like this? – Werner Sep 26 '17 at 6:20
  • exactly like this – Shahensha Khan Sep 26 '17 at 6:33

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.