以下操作有效,可以完成我想要的操作,但它可以更好地自动化,因为我想多次执行此操作。
\documentclass{article}
\usepackage{fp}
\usepackage{ifthen}
\usepackage{etoolbox}
\begin{document}
\begin{enumerate}
\item
\FPrandom{\rndA}
\FPrandom{\rndB}
\FPrandom{\rndC}
\FPeval{\uu}{trunc(11*\rndA, 0)}
\FPeval{\vv}{trunc(- 30 - 61*\rndB, 0)}
\FPeval{\ww}{trunc(101*\rndC + 100, 0)}
Some text involving \uu, \vv\ \& \ww.
\global\edef\uu{\uu}
\global\edef\vv{\vv}
\global\edef\ww{\ww}
\end{enumerate}
More text involving \uu, \vv\ \& \ww.
\end{document}
我多次尝试自动化都失败了。以下是我尝试执行的操作的格式。
\documentclass{article}
\usepackage{fp}
\usepackage{ifthen}
\usepackage{etoolbox}
\newcommand{\MakeGlobDefs}[1]{
This is the function input "unexpanded": \unexpanded{#1} %
% Loop over items in list #1 (possibly just one)
\global\edef<command name>{<command value>}
% end of loop
}
\begin{document}
\begin{enumerate}
\item
\FPrandom{\rndA}
\FPrandom{\rndB}
\FPrandom{\rndC}
\FPeval{\uu}{trunc(11*\rndA, 0)}
\FPeval{\vv}{trunc(- 30 - 61*\rndB, 0)}
\FPeval{\ww}{trunc(101*\rndC + 100, 0)}
Some text involving \uu, \vv\ \& \ww.
\def\Glist{\uu,\vv,\ww}
\MakeGlobDefs{\Glist}
\end{enumerate}
More text involving \uu, \vv\ \& \ww.
\end{document}
似乎当我尝试在函数\Glist
内使用命令列表 ( )时\MakeGlobDefs
,它们已经完全展开。所以我甚至无法使用循环。我尝试了各种应该控制扩展的命令,但毫无效果。
答案1
你是說這樣嗎?
\documentclass{article}
\usepackage{fp}
\usepackage{ifthen}
\usepackage{etoolbox}
\makeatletter
\newcommand{\MakeGlobDefs}[1]{%
This is the function input "unexpanded":
\texttt{\detokenize{#1}}$\rightarrow$
\texttt{\expandafter\detokenize\expandafter{#1}}%
% Loop over items in list #1 (possibly just one)
\@for\cs:={#1}\do{%
\expandafter\xdef\cs{\cs}%
}%
% end of loop
}
\makeatother
\begin{document}
\begin{enumerate}
\item
\FPrandom{\rndA}
\FPrandom{\rndB}
\FPrandom{\rndC}
\FPeval{\uu}{trunc(11*\rndA, 0)}
\FPeval{\vv}{trunc(- 30 - 61*\rndB, 0)}
\FPeval{\ww}{trunc(101*\rndC + 100, 0)}
Some text involving \uu, \vv\ \& \ww.
\def\Glist{\uu,\vv,\ww}
\MakeGlobDefs{\Glist}
\end{enumerate}
More text involving \uu, \vv\ \& \ww.
\end{document}
如果没有,\Glist
您可以使用以下命令:
\documentclass{article}
\usepackage{fp}
\usepackage{ifthen}
\usepackage{etoolbox}
\makeatletter
\newcommand{\MakeGlobDefs}[1]{%
\@for\cs:={\unexpanded{#1}}\do{%
\expandafter\xdef\cs{\cs}%
}%
}
\makeatother
\begin{document}
\begin{enumerate}
\item
\FPrandom{\rndA}
\FPrandom{\rndB}
\FPrandom{\rndC}
\FPeval{\uu}{trunc(11*\rndA, 0)}
\FPeval{\vv}{trunc(- 30 - 61*\rndB, 0)}
\FPeval{\ww}{trunc(101*\rndC + 100, 0)}
Some text involving \uu, \vv\ \& \ww.
\MakeGlobDefs{\uu,\vv,\ww}
\end{enumerate}
More text involving \uu, \vv\ \& \ww.
\end{document}
答案2
我可以建议一种更好的方法来获取随机整数并能够在任何地方使用它们。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\setrandominteger}{mO{0}m}
{
\int_const:cn { c_cwhctza_#1_int } { \fp_eval:n { randint(#2,#3) } }
}
\NewDocumentCommand{\userandominteger}{m}
{
\ensuremath{\int_use:c { c_cwhctza_#1_int }}
}
\ExplSyntaxOff
\begin{document}
\begin{enumerate}
\item
\setrandominteger{uu}{10}
\setrandominteger{vv}[-90]{-30}
\setrandominteger{ww}[100]{200}
Some text involving \userandominteger{uu}, \userandominteger{vv} \& \userandominteger{ww}.
\end{enumerate}
More text involving \userandominteger{uu}, \userandominteger{vv} \& \userandominteger{ww}.
\end{document}
该\setrandominteger{<label>}[<lower>]{<upper>}
宏定义范围为<lower>
- <upper>
(含)内的随机整数;如果缺少可选参数,则默认为 0。
您可以通过 恢复该值\userandominteger{<label>}
。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\setrandominteger}{mO{0}m}
{
\int_const:cn { c_cwhctza_#1_int } { \fp_eval:n { randint(#2,#3) } }
}
\NewDocumentCommand{\userandominteger}{m}
{
\ensuremath{\int_use:c { c_cwhctza_#1_int }}
}
\ExplSyntaxOff
\begin{document}
\begin{enumerate}
\item
\setrandominteger{uu}{10}
\setrandominteger{vv}[-90]{-30}
\setrandominteger{ww}[100]{200}
Some text involving \userandominteger{uu}, \userandominteger{vv} \& \userandominteger{ww}.
\end{enumerate}
More text involving \userandominteger{uu}, \userandominteger{vv} \& \userandominteger{ww}.
\end{document}
可以进行扩展以支持许多其他操作。
\documentclass{article}
\usepackage{xparse,xfp}
\ExplSyntaxOn
\NewDocumentCommand{\setrandominteger}{mO{0}m}
{
\int_const:cn { c_cwhctza_#1_int } { \fp_eval:n { randint(#2,#3) } }
}
\NewDocumentCommand{\printrandominteger}{m}
{
\ensuremath{\int_use:c { c_cwhctza_#1_int }}
}
\NewExpandableDocumentCommand{\userandominteger}{m}
{
\int_use:c { c_cwhctza_#1_int }
}
\NewDocumentCommand{\setfpvar}{mm}
{
\fp_const:cn { c_cwhctza_#1_fp } { #2 }
}
\NewDocumentCommand{\printfpvar}{m}
{
\ensuremath{\fp_use:c { c_cwhctza_#1_fp }}
}
\NewExpandableDocumentCommand{\usefpvar}{m}
{
\fp_use:c { c_cwhctza_#1_fp }
}
\ExplSyntaxOff
\begin{document}
\begin{enumerate}
\item
\setrandominteger{uu}{10}
\setrandominteger{vv}[-90]{-30}
\setrandominteger{ww}[100]{200}
\setfpvar{abc}{exp(rand()) + sqrt(\userandominteger{uu})}
Some text involving \printrandominteger{uu}, \printrandominteger{vv},
\printrandominteger{ww} and \printfpvar{abc}.
\end{enumerate}
More text involving \printrandominteger{uu}, \printrandominteger{vv},
\printrandominteger{ww} and \printfpvar{abc}.
We can also do $\fpeval{round(\usefpvar{abc}+\userandominteger{vv},2)}$
\end{document}