Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm basically trying to create multiple versions of a worksheet, each with different values for the variables in each question. To do this I'm defining random variables, then each time I compile the values change and I print a "new" version of the worksheet.

I understand how to create the random variables by using \pgfmathsetseed{\pdfrandomseed} and defining my variables in my desired ranges as e.g. \def\A{pgfrandom{1,10}\pgfmathresult}

The problem I run into is that I want to generate an answer for each problem at the end of the worksheet. But it wants to do the calculation for the answer with a different random value than the one generated in the original question.

e.g. If I say

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

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

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

and I ask the question:

What is \A + \B?

\answer

It will produce something like....

What is 2 + 12?

5 + 19, 24

So how do I get it to display the answer a) without showing the calculation itself (i.e. 2 + 12) and b) how do I get it to not re-generate a new random variable when computing the answer?

Sorry, I hope my questions are clear. I'm new to all this. Any help is appreciated. Thanks!

share|improve this question
    
Welcome to TeX.SX! Please make your code compilable, starting with \documentclass{...} and ending with \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to help you. Help them help you: remove that one hurdle between you and a solution to your problem. –  someonr Dec 26 '13 at 15:39
add comment

2 Answers

Based on your question you could do this:

Code

\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}

Result

What is 2 + 12?
Result is 14

Fun with layers ;)

You could use layers to hide the answer, until the solution layer is enabled (you didn't ask for it, so this is just for fun).

\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}

result

share|improve this answer
    
I actually do this a lot (elfsoft2000.com/worksheets/index.htm) but use C programs to generate the questions and LaTeX to typeset them. I create two separate files, one for the questions and one for the answers. –  John Kormylo Dec 26 '13 at 15:20
1  
@JohnKormylo: I didn't really try it, but it sounds that you want to use LuaTeX, right? –  Dror Dec 26 '13 at 15:29
    
Don't even know whtat LuaTeX is. No, I use Dev_C++. Anyway, you might consider putting the answers into floats which won't be printed until the last page. –  John Kormylo Dec 26 '13 at 15:32
    
exuse me, but is your discussion related to this answer or the question? (I just got a little bit confused) –  someonr Dec 26 '13 at 15:33
    
I should have put these comment on the original question. Sorry about that. –  John Kormylo Dec 26 '13 at 15:33
add comment

A sagetex solution using Sage. Set up variables in sagesilent mode. Access the variables in your document through \sage command while you typeset.

\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}

Here's the output running in Sagemath Cloud: enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.