在我的考试的某个问题中,我想为圆柱体的半径 (r) 和高度 (h) 生成两个整数随机数。我正在使用包来xfp
生成随机数。但是,我想根据这些随机值确定这个圆柱体的体积。我注意到\FPeval{}{}
当我有随机数时,这种方法不起作用。有没有办法使用包 xfp 将这些值存储在变量中?下面是我的代码:
\documentclass{article}
\usepackage{xfp}
\begin{document}
The value for the radius of a cilinder is equal to $r=\fpeval{randint(1,10)}$.
Supose that the height is equal to $h=\fpeval{randint(1,10)}$.
Then the volume of this solid is equal to $V=\pi r^2=.....$
(calculate the volume automatically)
\end{document}
'''
答案1
您可以将值存储在变量中并在计算中使用它们:
\documentclass{article}
\usepackage{xfp}
\begin{document}
\edef\cylinderradius{\fpeval{randint(1,10)}}% Cylinder radius
\edef\cylinderheight{\fpeval{randint(1,10)}}% Cylinder height
The value for the radius of a cylinder is equal to $r = \cylinderradius$m.
Suppose that the height is equal to $h = \cylinderheight$m.
Then the volume of this solid is equal to $V = \pi r^2 h = \fpeval{round(pi * \cylinderradius^2 * \cylinderheight, 2)}$m\textsuperscript{3}.
\end{document}
答案2
如果您选择使用 LuaLaTeX,那么以下解决方案可能会引起您的兴趣。
\documentclass{article}
\usepackage{luacode} % for '\luaexec' macro
\newcommand\myprint[1]{\luaexec{tex.sprint(#1)}}
\begin{document}
% First, generate two integer-valued random numbers "x", 1 \le x \le 10
\luaexec{ my_r = math.random(10) ; my_h = math.random (10) }
Suppose the radius and height of a cylinder are equal to
$r=\myprint{my_r}$ and $h=\myprint{my_h}$.
Then the volume of this solid is equal to
$V=\pi h r^2=\myprint{string.format("\%.3f", math.pi*my_h*my_r^2)}$.
\end{document}