22

How can I add line number to my algorithm? I want to write line number in the front of the algorithm line. How can I get that with the \usepackage[ruled]{algorithm2e}?

Code:

\documentclass{article}
\usepackage[ruled]{algorithm2e}
\renewcommand{\baselinestretch}{1.5}

\usepackage{listings}
\usepackage{blindtext}
\begin{document}
\blindtext

\vspace{1cm}

\begin{algorithm}[H]
   \SetAlgoLined
   \KwData{$next\_id, speed,stop\_distance, mac$}
   \KwResult{Find the initializer. }
   int $next\_id1$ = find a  record with $speed > 30$ \\ for this $mac$ in the next table;\\
   int $next\_id2$ = find record with $speed <7$ and \\ $stop\_distance < 60$ for this mac in the next table;\\
   \If{$next\_id1 < next\_id2$}{
      find the related $stop\_name$ for this mac;\\
      return  $stop\_name$;
   }
   \caption{Algorithm to find intializer.}
\end{algorithm}
\vspace{1cm}
\begin{lstlisting}[caption = {MYSQL query to find the initializer.}, label={lst: initializer}]
SELECT speed, stop_name
from next
where stop_distance < 60 and speed < 7  
and mac = ? and next_id > ?  LIMIT  1
\end{lstlisting}

\clearpage
\end{document} 
Mr Asker
  • 837
  • 6
    Isn't this the same question like http://tex.stackexchange.com/questions/270778/add-lines-and-line-number-to-the-pseudocode???? Adding \nl in front of a algorithm line will provide the line number, works out of the box –  Oct 03 '15 at 16:55
  • @ChristianHupfer: I have tried to add \nl in the front of the line after adding \SetNlSty{texttt}{(}{)} to my code but it does not work and I am getting this error ! LaTeX Error: \begin{lrbox} on input line 4 ended by \end{document}. Please can you try it with my code? – Mr Asker Oct 03 '15 at 17:03
  • You've accepted Werner's answer in your other question, apparently it seemed to have worked there –  Oct 03 '15 at 17:05
  • 2
    Am i stuck in the matrix? http://golatex.de/viewtopic,p,76914.html#76914 – Johannes_B Oct 03 '15 at 17:06
  • @Johannes_B: We all are ;-) –  Oct 03 '15 at 17:07

2 Answers2

33

To have all lines numbered, add the package option linesnumbered at loading time or execute \LinesNumbered somewhere in your preamble/document.

enter image description here

\documentclass{article}
\usepackage[ruled,linesnumbered]{algorithm2e}
\newcommand{\var}{\texttt}

\begin{document}

\vspace{1cm}

\begin{algorithm}[H]
  \SetAlgoLined
  \KwData{$\var{next\_id}$, $\var{speed}$, $\var{stop\_distance}$, $\var{mac}$}
  \KwResult{Find the initializer. }
  int $\var{next\_id1}$ = find a  record with $\var{speed} > 30$ 
   for this $\var{mac}$ in the next table \;
  int $\var{next\_id2}$ = find record with $\var{speed} < 7$ and 
   $\var{stop\_distance} < 60$ for this $\var{mac}$ in the next table \;
  \If{$\var{next\_id1} < \var{next\_id2}$}{
    find the related $\var{stop\_name}$ for this mac \;
    return $\var{stop\_name}$ \;
  }
  \caption{Algorithm to find intializer.}
\end{algorithm}

\end{document}

For indentation of lengthy lines, you can manually break them and insert an indent using the technique described in Linebreak in algorithm2e.

Werner
  • 603,163
  • 3
    Your answer is accepted within a minute after posting. To be honest, i cannot process your post and test the solution that fast. This is amazing. – Johannes_B Oct 03 '15 at 17:14
  • @Johannes_B: I have compile my example with the linesnumbered option and it worked for me. Therefore I accepted your answer so fast. – Mr Asker Oct 03 '15 at 17:20
  • 3
    @MrAsker It is not my answer. You got the same answer one hour ago by rainer. Now at least four people have wasted time. This is really annoying me right now! Argh – Johannes_B Oct 03 '15 at 17:23
  • @MrAsker: But in your post there was no indication that you want to have any line numbered –  Oct 03 '15 at 17:24
6

This works out of the box (with an up-to-date) TeX distribution)

\documentclass{article}
\usepackage[ruled]{algorithm2e}
\renewcommand{\baselinestretch}{1.5}

\usepackage{listings}
\usepackage{blindtext}
\begin{document}
\blindtext

 %\vspace{1cm}

\SetNlSty{texttt}{(}{)}
\begin{algorithm}[H]
\SetAlgoLined
\nl   \KwData{$next\_id, speed,stop\_distance, mac$}
\nl   \KwResult{Find the initializer. }
\nl   int $next\_id1$ = find a  record with $speed > 30$ \\ for this $mac$ in the next table;\\
\nl   int $next\_id2$ = find record with $speed <7$ and \\ $stop\_distance < 60$ for this mac in the next table;\\
\nl   \If{$next\_id1 < next\_id2$}{
\nl      find the related $stop\_name$ for this mac;\\
\nl      return  $stop\_name$;
   }
   \caption{Algorithm to find intializer.}
\end{algorithm}
\vspace{1cm}
\begin{lstlisting}[caption = {MYSQL query to find the initializer.}, label={lst: initializer}]
SELECT speed, stop_name
from next
where stop_distance < 60 and speed < 7  
and mac = ? and next_id > ?  LIMIT  1
\end{lstlisting}

\clearpage
\end{document} 

enter image description here