我想在 LaTeX 文档中做一些计算;第一个计算的结果将用于下一个计算。我发现这个fp
包应该可以完成这个工作。下面是一个例子:
\FPeval{\result}{round(400*1.06,0)}%
$400+6\%=\result$\\
\FPeval{\result}{round(424*1.06,0)}%
$424+6\%=\result$
第一个\result
应该存储在某种变量中,并输入到下一个等式中。
答案1
包裹fp
自述提到了两个例子,然后还提到了一些计算中的宏名的警告/使用:
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.
)
以下是包装指南:
\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}