3

I want to do some calculations in a LaTeX document; the result of the first is used in the next calculation. I figured out that the fp package should do the job. Here is an example:

\FPeval{\result}{round(400*1.06,0)}%
$400+6\%=\result$\\
\FPeval{\result}{round(424*1.06,0)}%
$424+6\%=\result$

The first \result should be stored in a varable of some sort an fed into the next equation.

  • Did you try the obvious \FPeval{\result}{round(\result*1.06,0)}: seems to work for me. – Joseph Wright Jan 15 '14 at 15:39
5

The fp package README mentions two examples and then also some warning/usage of macro names inside calculations:

 Example 1:
   The macro call
         \FPupn\result{17 2.5 + 17.5 - 2 1 + * 2 swap /} 
   is equivalent to
     \result := ((17.5 - (17 + 2.5)) * (2 + 1)) / 2
   and evaluates to
     \def\result{-3.000000000000000000}
   Afterwards the macro call
         \FPupn\result{\result{} -1 * 0.2 + sin 2 round}
                              ^^ the "{}" is necessary!
   is equivalent to
     \result := round_2(sin((\result * -1) + 0.2))
   and evaluates to
         \def\result{-0.06}
 Example 2:
   As "result" is an abbreviation of "\result{}" you may
   write
     \FPupn{result}{17 2.5 + 17.5 - 2 1 + * 2 swap /}
   and
     \FPupn{result}{result -1 * 0.2 + sin 2 round}
   instead leading to the same results.
   This is even true for other macro names using e.g. "x" for "\x{}"
   and so on. But be careful with it. We may introduce new constants
   in further versions overwriting these abbreviations.
 - fp-eval.sty:
     The following macros are public ones to be used in the document:
       \FPeval#1#2      % #1 := eval(#2) where eval evaluates the
                     expression #2
 ATTENTION: Do not use macro names with \. for its own
 Use only the name or the macro surrounded by (, and ) instead,
 i.e. do not write "\value{}" but "value" or "(\value)".
 This is needed to avoid problems with a prefix "-" of numbers.
 (I do not intend to write a more complex parsing routine in future.
  But if you do so, just send it to me.
 )

Here's following the package guidelines:

enter image description here

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

\FPeval{\result}{round(400*1.06,0)}%
$400+6\%=\result$

\FPeval{\result}{round((result)*1.06,0)}%
$424+6\%=\result$

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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