I have a problem with the algorithm2e
package. I want to reference the end of a block, e.g., a for-loop, in my text. Therefore, I place a label behind the closing bracket of the for statement. When I configure the package to produce an "end" line, the label correctly points to this line. But if I use the option "noend", the label points to the for statement instead of the last line of the for-loop.
Here is an example:
\documentclass[]{scrartcl}
\usepackage[algo2e, noline]{algorithm2e}
\begin{document}
\begin{algorithm2e}
\For {...} { a = b\; }\label{x}
\end{algorithm2e}
Reference: \ref{x}
\end{document}
The output would look somehow like this:
1 for … do
2 a = b
3 end
Reference: 3
When I reference label x in my text, I get a correct reference to line 3.
With the noend option for package algorithm2e
, I get the following output:
1 for … do
2 a = b
Reference: 1
Label x now creates a reference to line 1 instead of line 2.
I guess it depends on how latex handles labels. I can fix it by putting the label behind the last line of my for-loop. But then the label would point to line 2 instead of 3 when I want to show "end" lines again.
Is there a possibility that always generates the correct reference independent of the option that I am using?
Best Answer
You most certainly triggered a bug in
algorithm2e
. While the manual is not very clear on that, I guess the behaviour of theend
version is correct, and the label should refer to the last line of the block. The reason for this bug is that\label
picks up\@currentlabel
, butalgorithm2e
uses its own counterAlgoLine
, and this is not pushed to\@currentlabel
in all situations. The funny thing is thatalgorithm2e
has special code for label handling whenhyperref
is loaded, and then everything works correct. So your recipe is: 1. Inform the maintainer ofalgorithm2e
about this bug. 2a. Use thehyperref
package. 2b. If you don't want to usehyperref
, the following code defines a new labeling command,\elabel
, which works as expected: