19

Say I have algorithm appear as followed

enter image description here

Is it possible to reference the line number using ~\ref or any other possiblities?

2
  • 8
    You have accepted 50% of your answered questions. All your questions are excellent answered. We don't put any thanks in our question but you should thank users by upvoting [his/her] answer (with the upward pointing arrow to the left of it). Some users are ignoring questions where the OP has such an accepting rate. I am one of them. Aug 7, 2012 at 19:03
  • Is it possible to reference the line of a while loop (in your example, line 2)?
    – bios
    Jan 29, 2020 at 11:00

1 Answer 1

30

Yes, it's possible. :)Use the normal \label command in the line you want to refer (page 4 of the algorithm2e manual), then use \ref to display the line number.

\documentclass{article}

\usepackage[linesnumbered]{algorithm2e}

\begin{document}

Line \ref{alg:goto} is important.

\vspace{2em}

\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\;\label{alg:goto}
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\end{algorithm}

\end{document}

The output:

Line 5

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .