무작위 변수를 생성한 후 pdflatex의 계산에 사용하는 방법

무작위 변수를 생성한 후 pdflatex의 계산에 사용하는 방법

저는 기본적으로 각 질문의 변수 값이 서로 다른 여러 버전의 워크시트를 만들려고 합니다. 이를 위해 무작위 변수를 정의한 다음 값 변경을 컴파일할 때마다 워크시트의 "새" 버전을 인쇄합니다.

\pgfmathsetseed{\pdfrandomseed}예를 들어 원하는 범위에서 변수를 사용하고 정의하여 무작위 변수를 생성하는 방법을 이해합니다 .\def\A{pgfrandom{1,10}\pgfmathresult}

제가 직면한 문제는 워크시트 끝부분에서 각 문제에 대한 답을 생성하고 싶다는 것입니다. 하지만 원래 질문에서 생성된 값과 다른 임의 값을 사용하여 답변에 대한 계산을 수행하려고 합니다.

예를 들어 내가 말하면

\def\A{pgfrandom{1,10}\pgfmathresult}

\def\B{pgfrandom{10,20}\pgfmathresult}

\def\answer{\A + \B, \pgfmathresult}

그리고 저는 이렇게 질문합니다.

What is \A + \B?

\answer

그것은 다음과 같은 것을 생산할 것입니다....

What is 2 + 12?

5 + 19, 24

그러면 a) 계산 자체(예: 2 + 12)를 표시하지 않고 답을 표시하려면 어떻게 해야 할까요? b) 답을 계산할 때 새로운 무작위 변수를 다시 생성하지 않게 하려면 어떻게 해야 할까요?

죄송합니다. 질문이 명확해지기를 바랍니다. 나는 이 모든 것이 처음이다. 도움을 주시면 감사하겠습니다. 감사해요!

답변1

귀하의 질문에 따라 다음을 수행할 수 있습니다.

암호

\documentclass{article}

\usepackage{pgf}

\pgfmathsetseed{\number\pdfrandomseed} % provide seed for pseudo random generator
% (change to constant, to get the same random numbers on every time compiling)

% new command to init variables
\newcommand\initVariables{%
    \pgfmathsetmacro{\A}{random(1,10)}%
    \pgfmathsetmacro{\B}{random(10,20)}%
}

\newcommand\answer{\pgfmathprint{int(\A + \B)}} % define command to calculate result


\begin{document}
\initVariables % initialize variables (do this every time you need new numbers)
What is $\A + \B$?

Result is \answer
\end{document}

결과

2 + 12는 무엇입니까?
결과는 14

레이어를 활용하는 재미 ;)

솔루션 레이어가 활성화될 때까지 레이어를 사용하여 답변을 숨길 수 있습니다(요청하지 않았으므로 이는 단지 재미를 위한 것입니다).

\documentclass{article}

\usepackage{pgf}
\usepackage{ocg-p}

\pgfmathsetseed{\number\pdfrandomseed} % provide seed for pseudo random generator 

% new command to init variables
\newcommand\initVariables{%
    \pgfmathsetmacro{\A}{random(1,10)}%
    \pgfmathsetmacro{\B}{random(10,20)}%
}

\newcommand\answer{\pgfmathprint{int(\A + \B)}} %define command to calculate result
\begin{document}
\initVariables%initialize variables (do this every time you need new numbers)
What is $\A + \B$?

Result is 
\begin{ocg}{result layer}{1}{0}
\answer
\end{ocg}

\end{document}

결과

답변2

sagetex사용하는 솔루션세이지. 모드 에서 변수를 설정합니다 sagesilent. \sage조판하는 동안 명령을 통해 문서의 변수에 액세스합니다 .

\documentclass[12point]{article}%
\usepackage{sagetex}%  use Sage for it's math ability
\pagestyle{empty} % remove the page numbers
\begin{document}
\begin{sagesilent}
var('x')
a = Integer(randint(1,9))
b = Integer(randint(1,9))
\end{sagesilent}
% *********** END DEFINE VARIABLES ****************
\noindent What's $\sage{a}+\sage{b}$? $\sage{a}\times\sage{b}$?    $\sage{a}\div\sage{b}$?\\
Answer: $\sage{a}+\sage{b}=\sage{a+b}$, $\sage{a}\times\sage{b}=\sage{a*b}$ and $\sage{a}\div\sage{b}=\sage{a/b}$\\
You can get a decimal for the division $\sage{a}\div\sage{b}\approx\sage{(a/b).n(digits=5)}$
\end{document}

다음은 실행 중인 출력입니다.세이지매스 클라우드: 여기에 이미지 설명을 입력하세요

관련 정보