# Using reference labels in mathematical calculations

EDIT: This answer points to a good solution.

I wanted to use reference labels in mathematical calculations and was under the impression that if I use the calc package, it will be rather straightforward.

\documentclass{article}

\usepackage{calc}

\usepackage{lineno}

\begin{document}

\linenumbers
First line

Second line\label{ls}

As found in \arabic{\ref{ls}-1}

\end{document}


As we find, this does not work.

(My actual calculations will more complex than this. This is a toned down version.)

• \arabic{} needs the name of a counter. It is defined as \def\arabic#1{\expandafter\@arabic\csname c@#1\endcsname}. – cfr May 11 '15 at 1:53
• @cfr Then, how do I engage a label in calculation? – Masroor May 11 '15 at 1:54
• Tried \setcounter{x}{\ref{ls}-1} which did not work. – Masroor May 11 '15 at 2:06
• Pick your poison? But I'm far from good at this stuff. But I would think: (1) go lower level TeX; (2) use something like pgfmath or the TikZ math library; (3) use something like the fp package; (4) use expl3 (maybe). – cfr May 11 '15 at 2:09
• Or, in this case, just counters. But \ref{ls} doesn't return anything for me anyway. – cfr May 11 '15 at 2:30

You can use a combination of refcount and expl3:

\documentclass{article}

\usepackage{lineno,refcount}
\usepackage{expl3}

\ExplSyntaxOn
\cs_new_eq:NN \calc \fp_eval:n
\ExplSyntaxOff

\begin{document}

\linenumbers
First line

Second line\linelabel{ls}

As found in \calc{\getrefnumber{ls}-1}

\end{document}


\getrefnumber expands its argument (defaulting to 0 if the references haven't settled), making it useful in calculations.

While the above example uses lineno (and therefore a package-specific \linelabel for labelling some reference, the above usage should work with the standard \label-\ref system as well.

You could do something like this in a simple case. Obviously, I don't know what your more complex usage requires. This just uses LaTeX counters.

I've changed the reference to use an enumeration because \ref{ls} did not return a number, so obviously could not be used.

\documentclass{article}
\begin{document}
\begin{enumerate}
\item First line
\item Second line\label{ls}
\end{enumerate}
\newcounter{mycounter}
\setcounter{mycounter}{\ref{ls}}
As found in \arabic{mycounter}
\end{document}


Yet another solution:

Edit: The first version of this code contained a stupid error that prevented it from working when the number generated by the reference contained more than one digit; I have now corrected this.

\documentclass{article}

\makeatletter

\@ifdefinable\striphbox{\def\striphbox#1\hbox#2% #1 is the number, #2 is {}
{{#1}}
}
\newcommand*\calcref[2]{%
\ifcsname r@#1\endcsname
\begingroup
\edef\cs{\ref{#1}}%
\def\next##1{\numexpr #2\relax}%
\number\expandafter\expandafter\expandafter\next\expandafter\striphbox\cs
\endgroup
\fi
}

\makeatother

\begin{document}
\begin{enumerate}
\item First line
\setcounter{enumi}{10}
\item Second line\label{ls}
\end{enumerate}
As found in~\calcref{ls}{#1-1} and in~\calcref{ls}{7*#1+5}\ldots
\end{document}


In the first argument of \calcref you pass the symbolic label, in the second one you pass a numeric expression in which #1 marks the spot where the number from the reference should be included.