Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

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.

share|improve this answer
    
simply perfect, thank you! –  john Jul 18 '12 at 1:26
    
\getrefnumber is expandable, therefore it cannot notify LaTeX that the reference is used &mdash; LaTeX wants to warn because of undefined references. Therefore package refcount provides \refused that tells LaTeX about the reference. \refused{myline} is then used outside the expandable context of \FPeval. –  Heiko Oberdiek Aug 6 '12 at 18:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.