[Tex/LaTex] convert outcome of reference to regular number

calculationscross-referencingfp

Is it possible to transform the outcome of a reference (e.g., a line number) to a regular number that I can use in a calculation?
I have this (and works fine):

\FPeval{\result}{clip(5+6)} $\result$

Now I want to do a calculation involving \lineref{myline}. However,

\FPeval{\result}{clip(5+\lineref{myline})} $\result$

doesn't work because \lineref{myline} is not recognized as a number, I guess.

Any ideas?

Best Answer

You easiest is to use the refcount package which allows for expandable versions of functions to retrieve counter numbers. Here's a small example where line number is extracted from an algorithm:

enter image description here

\documentclass{article}
\usepackage{refcount}% http://ctan.org/pkg/refcount
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage{fp}% http://ctan.org/pkg/fp
\begin{document}

\begin{algorithmic}[1]
  \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
  \State $r\gets a\bmod b$
  \While{$r\not=0$}\Comment{We have the answer if r is 0}
    \State $a\gets b$
    \State $b\gets r$
    \State $r\gets a\bmod b$\label{myline}% This is line 6 in the algorithm
  \EndWhile\label{euclidendwhile}
  \State \textbf{return} $b$\Comment{The gcd is b}
  \EndProcedure
\end{algorithmic}

Result: \FPeval{\result}{clip(5+\getrefnumber{myline})} $\result$
\end{document}​

\getrefnumber{<label>} retrieves the counter associated with <label>. For calculation of page references, an analogous \getpagerefnumber is provided.

Related Question