4

I want to use the number of an equation as an integer value. Consider the following MWE:

\documentclass{article}
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp
\begin{document}

    \begin{equation}\label{b1}
    a+b
    \end{equation}

    \begin{equation}\label{b2}
    c+d
    \end{equation}

\FPeval{\result}{clip(\ref{b1}+\ref{b2})}
\ref{b1} + \ref{b2} = $\result$\par
\end{document}

I expect the result as the following:

1 + 2 = 3

How can I achieve this?

  • 1
    With the xfp package you don't need the intermediate step (neither clip): $\ref{b1} + \ref{b2} = \fpeval{\getrefnumber{b1}+\getrefnumber{b2}}$ – Phelype Oleinik Dec 02 '19 at 14:27

1 Answers1

4

The problem is that \ref is not expandable. You can use \getrefnumber from the refcount package.

\documentclass{article}
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp
\usepackage{refcount}
\begin{document}

    \begin{equation}\label{b1}
    a+b
    \end{equation}

    \begin{equation}\label{b2}
    c+d
    \end{equation}

\FPeval{\result}{clip(\getrefnumber{b1}+\getrefnumber{b2})}
\ref{b1} + \ref{b2} = $\result$\par
\end{document}

enter image description here

EDITED to use \ref in final line, per suggestion of Phelype.

In fact, as long as you are working with integer arithmetic, you can forego the use of fp altogether:

\documentclass{article}
\usepackage{refcount}
\begin{document}

    \begin{equation}\label{b1}
    a+b
    \end{equation}

    \begin{equation}\label{b2}
    c+d
    \end{equation}

\ref{b1} + \ref{b2} = $\the\numexpr\getrefnumber{b1}+\getrefnumber{b2}\relax$\par
\end{document}