如何在 Latex 编辑器的同一页面的两个地方复制一个随机数?

如何在 Latex 编辑器的同一页面的两个地方复制一个随机数?

我正在尝试在 Latex 编辑器中创建一个心算练习表生成器。对于预期的问题数量,我只需要一半 A4 纸。所以另一半我想为另一个学生打印同一组问题。

在 excel 中,我们可以简单地将一个单元格的值复制到另一个单元格中进行复制。如何复制所有包含随机生成的数字的问题。

我有这些问题的语法。只需要帮助复制部分。谢谢。

\tikzset{declare function={randomfixed(\a,\b) = int(random(0,int(\b-\a))+\a);}}

% get random integer
\newcommand\randomint[2]{\bgroup%
  \pgfmathsetmacro\myval{randomfixed(#1,#2)}%
  \pgfmathsetmacro\final{(\myval < 0)?"(\myval)":\myval}%
  \final\egroup%
}

\newcommand{\randnzi}{%random non-zero integer, change random (argument) to increase or decreases range
    \pgfmathsetmacro{\a}{int(ifthenelse(rand > 0, 1, -1)*random(1,9))}%
}

% get random integer for equation - the only diff from \randomint is that this one doesn't generate () for negative numbers
\newcommand\randomeqint[2]{\bgroup%
  \pgfmathsetmacro\myval{randomfixed(#1,#2)}%
  \pgfmathsetmacro\final{(\myval < 0)?"\myval":\myval}%
  \final\egroup%
}
% get random operator
\def\ops{{"+","-","\times","/"}}
\def\pmop{{"+","-"}}
\newcommand\randomop{\bgroup\pgfmathsetmacro\op{\ops[int(rnd*4)]}\op\egroup}

%get random + or - only
\newcommand\randompm{{\bgroup\pgfmathsetmacro\op{\pmop[int(rnd*2)]}\op\egroup}}
% choose random seed
\pgfmathsetseed{\pdfuniformdeviate 10000000}


\begin{document}
\foreach \n in {1,...,5}{
   $\randomint{-10}{10} \randomop{} \randomint{-10}{10} = $\par
   %\par is used in for loop to create a linebreak after each iteration
}

答案1

如果您提供了一个可用的测试文件,那会更容易,但如果我理解你的问题,你可以替换

\foreach \n in {1,...,5}{
   $\randomint{-10}{10} \randomop{} \randomint{-10}{10} = $\par
   %\par is used in for loop to create a linebreak after each iteration
}

经过

\sbox0{$\randomint{-10}{10} \randomop{} \randomint{-10}{10} = $}
\foreach \n in {1,...,5}{
   \usebox{0}\par
   %\par is used in for loop to create a linebreak after each iteration
}

获得5份相同的副本。


由于 jfbu 已提供测试文件,这里采用的是相同的技术,但这次将整个列表装箱并重新使用。这里我使用了一个宽度为 parbox 的\textwidth垂直重复,您可以使 parbox 变窄并布置水平副本。

在此处输入图片描述

\documentclass{article}

\usepackage{tikz}

\tikzset{declare function={randomfixed(\a,\b) = int(random(0,int(\b-\a))+\a);}}

% get random integer
\newcommand\randomint[2]{\bgroup%
  \pgfmathsetmacro\myval{randomfixed(#1,#2)}%
  \pgfmathsetmacro\final{(\myval < 0)?"(\myval)":\myval}%
  \final\egroup%
}

\newcommand{\randnzi}{%random non-zero integer, change random (argument) to increase or decreases range
    \pgfmathsetmacro{\a}{int(ifthenelse(rand > 0, 1, -1)*random(1,9))}%
}

% get random integer for equation - the only diff from \randomint is that this one doesn't generate () for negative numbers
\newcommand\randomeqint[2]{\bgroup%
  \pgfmathsetmacro\myval{randomfixed(#1,#2)}%
  \pgfmathsetmacro\final{(\myval < 0)?"\myval":\myval}%
  \final\egroup%
}
% get random operator
\def\ops{{"+","-","\times","/"}}
\def\pmop{{"+","-"}}
\newcommand\randomop{\bgroup\pgfmathsetmacro\op{\ops[int(rnd*4)]}\op\egroup}

%get random + or - only
\newcommand\randompm{{\bgroup\pgfmathsetmacro\op{\pmop[int(rnd*2)]}\op\egroup}}
% choose random seed
\pgfmathsetseed{\pdfuniformdeviate 10000000}


\begin{document}


\sbox0{\parbox{\textwidth}{%
\foreach \n in {1,...,5}{
   $\randomint{-10}{10} \randomop{} \randomint{-10}{10} = $\par
   %\par is used in for loop to create a linebreak after each iteration
}
}}

Answer this:

\noindent\usebox{0}

\bigskip

You fool, try again:

\noindent\usebox{0}


\end{document}

答案2

\documentclass{article}

\usepackage{tikz}

\tikzset{declare function={randomfixed(\a,\b) = int(random(0,int(\b-\a))+\a);}}

% get random integer
\newcommand\randomint[2]{\bgroup%
  \pgfmathsetmacro\myval{randomfixed(#1,#2)}%
  \pgfmathsetmacro\final{(\myval < 0)?"(\myval)":\myval}%
  \final\egroup%
}

\newcommand{\randnzi}{%random non-zero integer, change random (argument) to increase or decreases range
    \pgfmathsetmacro{\a}{int(ifthenelse(rand > 0, 1, -1)*random(1,9))}%
}

% get random integer for equation - the only diff from \randomint is that this one doesn't generate () for negative numbers
\newcommand\randomeqint[2]{\bgroup%
  \pgfmathsetmacro\myval{randomfixed(#1,#2)}%
  \pgfmathsetmacro\final{(\myval < 0)?"\myval":\myval}%
  \final\egroup%
}
% get random operator
\def\ops{{"+","-","\times","/"}}
\def\pmop{{"+","-"}}
\newcommand\randomop{\bgroup\pgfmathsetmacro\op{\ops[int(rnd*4)]}\op\egroup}

%get random + or - only
\newcommand\randompm{{\bgroup\pgfmathsetmacro\op{\pmop[int(rnd*2)]}\op\egroup}}
% choose random seed
\edef\myrandomseed{\pdfuniformdeviate 10000000}

\usepackage{multicol}
\begin{document}
\begin{multicols}2
\pgfmathsetseed{\myrandomseed}
Answer this:\par
  \foreach \n in {1,...,5}{
   $\randomint{-10}{10} \randomop{} \randomint{-10}{10} = $\par
   %\par is used in for loop to create a linebreak after each iteration
}
\columnbreak
\pgfmathsetseed{\myrandomseed}

You fool, try again:\par
\foreach \n in {1,...,5}{
   $\randomint{-10}{10} \randomop{} \randomint{-10}{10} = $\par
   %\par is used in for loop to create a linebreak after each iteration
}
\end{multicols}

\end{document}

在此处输入图片描述

相关内容