LateX 中的变量求值
我有以下一段乳胶代码:
\def\a{1}
\def\b{2}
\def\c{\a+\b}
\def\d{\c/2}
我期望 \d 的值为 1.5。但事实并非如此。然而,像这样在 \c 的定义中添加括号
\def\c{\a+\b}
也不起作用,因为如果我在某个地方使用 \c ,它会抱怨括号。在 \d 的定义中,有没有办法在将 \c 除以 2 之前对其进行评估?就像:(
\def\d{\eval{\c}/2}
我做了 \eval 来表明我的意思)
I have the following piece of latex code:
\def\a{1}
\def\b{2}
\def\c{\a+\b}
\def\d{\c/2}
I expected \d to have the value 1.5. But it did not. However, adding parenthesis to the definition of \c like
\def\c{\a+\b}
Doesn't work either, because if I use \c somewhere, it complains about the parenthesis. Is there a way to evaluate \c before dividing it by 2 in the definition of \d? Like:
\def\d{\eval{\c}/2}
(I made that \eval up to show what I mean)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 calc 包进行算术运算。包 fp 适用于实数。
如需讨论 LaTeX 问题,请访问 tex.stackexchange.com。
You could use the calc package for arithmetic operations. The package fp works with real numbers.
For discussing LaTeX problems you're kindly invited to visit tex.stackexchange.com.
您需要记住
\def
用于创建替换文本。它总是会返回你所输入的内容,除了对数学一无所知之外。如果我们假设您正在使用 e-TeX(可能),那么对于整数表达式,您可能会这样做。这使用 e-TeX 原语
\intexpr
,它执行整数数学运算。对于实数,Stefan 是正确的,fp 包是最好的方法。You need to remember that
\def
is about creating replacement text. It will always give you back what you put in, quite apart from not knowing anything about maths. If we assume you are using e-TeX (likely), then for integer expressions you might doThis uses the e-TeX primitive
\intexpr
, which does integer mathematics. For real numbers, Stefan is right that the fp package is the best approach.