以下代码非常适合将每次构建的新输入/输出值打印为 PDF;但是,我无法将其四舍五入到小数位。它总是四舍五入到最接近的整数。我尝试过 \numprint 和 \sisetup 方法,但没有成功。这似乎是一个简单的问题;但是,我在这个论坛上部署类似问题的答案却没有成功。最终,我希望能够四舍五入到正确的数字,甚至尝试过使用“round-places”和“round-figures”来做到这一点,无论它们属于哪个包……都没有成功。
\documentclass[12pt]{article}
\usepackage{geometry}
\geometry{margin=1in}
\usepackage{pgf,amsmath,enumitem,siunitx}
\pgfmathsetseed{\number\pdfrandomseed} % seed for pseudo random generator
% new command to initialize variables
\newcommand\initVariables{%
\pgfmathsetmacro{\A}{random(100,200)}%
\pgfmathsetmacro{\B}{random(5,29)}%
}
% define command to calculate result
%\newcommand\answer{\pgfmathprint{round(\A / \B)}}
\newcommand\answer{\pgfmathprint{round(\A / \B)}}
\begin{document}
\begin{enumerate}
\initVariables % initialize variables
\item \textbf{An object is displaced \A \ meters in \B \ seconds.}\\
\begin{equation*}
v = \dfrac{\Delta s}{\Delta t} = \dfrac{\A \ m}{\B \ s} =
\pgfmathparse{\A / \B}\pgfmathresult \ \frac{m}{s}
\approx \answer \frac{m}{s}\\
\end{equation*}
答案1
内核expl3
对于浮点数有更好的管理。
宏\defineVariables
(局部)设置变量应包含的内容。使用\initVariables
表达式进行求值(因此实际上会替换随机整数)。
为了使用变量A
,请使用\Var{A}
(这比使用等更好\A
,否则可能会重新定义重要的命令)。使用
\round(<expression>,<places>)
对表达式进行求值,并按规定的小数位数进行四舍五入。
\documentclass[12pt]{article}
\usepackage{geometry}
\geometry{margin=1in}
\usepackage{amsmath,enumitem,siunitx,xfp}
\ExplSyntaxOn
% no need to set the seed with expl3
% new command to initialize variables
\NewDocumentCommand{\defineVariables}{m}
{
\prop_set_from_keyval:Nn \l_clark_variables_def_prop { #1 }
}
\NewDocumentCommand{\initVariables}{}
{
\prop_map_inline:Nn \l_clark_variables_def_prop
{
\prop_put:Nnx \l_clark_variables_set_prop { ##1 } { \fp_eval:n { ##2 } }
}
}
\NewExpandableDocumentCommand{\Var}{m}
{
\prop_item:Nn \l_clark_variables_set_prop { #1 }
}
\NewExpandableDocumentCommand{\round}{mm}
{
\fp_eval:n { round(#1,#2) }
}
\prop_new:N \l_clark_variables_def_prop
\prop_new:N \l_clark_variables_set_prop
\ExplSyntaxOff
\begin{document}
\begin{enumerate}
\defineVariables{
A={randint(100,200)},
B={randint(5,29)},
}
\sisetup{per-mode=fraction}
% initialize variables
\initVariables
\item \textbf{An object is displaced \Var{A} meters in \Var{B} seconds.}
\begin{equation*}
v = \frac{\Delta s}{\Delta t}
= \dfrac{\SI{\Var{A}}{m}}{\SI{\Var{B}}{s}}
= \SI{\round{\Var{A}/\Var{B}}{5}}{\meter\per\second}
\approx \SI{\round{\Var{A}/\Var{B}}{1}}{\meter\per\second}
\end{equation*}
% initialize variables
\initVariables
\item \textbf{An object is displaced \Var{A} meters in \Var{B} seconds.}
\begin{equation*}
v = \frac{\Delta s}{\Delta t}
= \dfrac{\SI{\Var{A}}{m}}{\SI{\Var{B}}{s}}
= \SI{\round{\Var{A}/\Var{B}}{5}}{\meter\per\second}
\approx \SI{\round{\Var{A}/\Var{B}}{1}}{\meter\per\second}
\end{equation*}
% initialize variables
\initVariables
\item \textbf{An object is displaced \Var{A} meters in \Var{B} seconds.}
\begin{equation*}
v = \frac{\Delta s}{\Delta t}
= \dfrac{\SI{\Var{A}}{m}}{\SI{\Var{B}}{s}}
= \SI{\round{\Var{A}/\Var{B}}{5}}{\meter\per\second}
\approx \SI{\round{\Var{A}/\Var{B}}{1}}{\meter\per\second}
\end{equation*}
\end{enumerate}
\end{document}