How can I duplicate a random number in two places on the same page in a latex editor?
I am trying to create a mental math practice sheet generator in a latex editor. For the number of questions intended, I require only half of A4 sheet. So the other half I want to print the same set of questions for another student.
In excel we can simply copy the values of a cell into another to duplicate. How do I duplicate all the questions containing randomly generated numbers.
I have the syntax for the questions. Just need help with duplicating part. Thanks.
tikzset{declare function={randomfixed(a,b) = int(random(0,int(b-a))+a);}}
% get random integer
newcommandrandomint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"(myval)":myval}%
finalegroup%
}
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
newcommandrandomeqint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"myval":myval}%
finalegroup%
}
% get random operator
defops{{"+","-","times","/"}}
defpmop{{"+","-"}}
newcommandrandomop{bgrouppgfmathsetmacroop{ops[int(rnd*4)]}opegroup}
%get random + or - only
newcommandrandompm{{bgrouppgfmathsetmacroop{pmop[int(rnd*2)]}opegroup}}
% 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
}
random-numbers
add a comment |
I am trying to create a mental math practice sheet generator in a latex editor. For the number of questions intended, I require only half of A4 sheet. So the other half I want to print the same set of questions for another student.
In excel we can simply copy the values of a cell into another to duplicate. How do I duplicate all the questions containing randomly generated numbers.
I have the syntax for the questions. Just need help with duplicating part. Thanks.
tikzset{declare function={randomfixed(a,b) = int(random(0,int(b-a))+a);}}
% get random integer
newcommandrandomint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"(myval)":myval}%
finalegroup%
}
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
newcommandrandomeqint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"myval":myval}%
finalegroup%
}
% get random operator
defops{{"+","-","times","/"}}
defpmop{{"+","-"}}
newcommandrandomop{bgrouppgfmathsetmacroop{ops[int(rnd*4)]}opegroup}
%get random + or - only
newcommandrandompm{{bgrouppgfmathsetmacroop{pmop[int(rnd*2)]}opegroup}}
% 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
}
random-numbers
2
save the random number in a macro, or rather set the random seed identically (now that I see it is a whole series you want replicated)
– user4686
Jan 14 at 10:34
Thanks for the answer @jfbu but I am fairly new to latex. Don't know macro too well. If you could provide me with easy resource to read on macro, I would be very grateful. Thanks.
– rhlchd
Jan 14 at 16:59
"macro" is a generic name, LaTeX "commands" are macros. But what you are lacking is knowledge oflet
oredef
which are not described usually in LaTeX books. But this is what is needed here simply to store once and for all a given random seed in a "macro" and use it after, see my answer.
– user4686
Jan 14 at 17:04
add a comment |
I am trying to create a mental math practice sheet generator in a latex editor. For the number of questions intended, I require only half of A4 sheet. So the other half I want to print the same set of questions for another student.
In excel we can simply copy the values of a cell into another to duplicate. How do I duplicate all the questions containing randomly generated numbers.
I have the syntax for the questions. Just need help with duplicating part. Thanks.
tikzset{declare function={randomfixed(a,b) = int(random(0,int(b-a))+a);}}
% get random integer
newcommandrandomint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"(myval)":myval}%
finalegroup%
}
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
newcommandrandomeqint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"myval":myval}%
finalegroup%
}
% get random operator
defops{{"+","-","times","/"}}
defpmop{{"+","-"}}
newcommandrandomop{bgrouppgfmathsetmacroop{ops[int(rnd*4)]}opegroup}
%get random + or - only
newcommandrandompm{{bgrouppgfmathsetmacroop{pmop[int(rnd*2)]}opegroup}}
% 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
}
random-numbers
I am trying to create a mental math practice sheet generator in a latex editor. For the number of questions intended, I require only half of A4 sheet. So the other half I want to print the same set of questions for another student.
In excel we can simply copy the values of a cell into another to duplicate. How do I duplicate all the questions containing randomly generated numbers.
I have the syntax for the questions. Just need help with duplicating part. Thanks.
tikzset{declare function={randomfixed(a,b) = int(random(0,int(b-a))+a);}}
% get random integer
newcommandrandomint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"(myval)":myval}%
finalegroup%
}
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
newcommandrandomeqint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"myval":myval}%
finalegroup%
}
% get random operator
defops{{"+","-","times","/"}}
defpmop{{"+","-"}}
newcommandrandomop{bgrouppgfmathsetmacroop{ops[int(rnd*4)]}opegroup}
%get random + or - only
newcommandrandompm{{bgrouppgfmathsetmacroop{pmop[int(rnd*2)]}opegroup}}
% 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
}
random-numbers
random-numbers
asked Jan 14 at 10:27
rhlchdrhlchd
111
111
2
save the random number in a macro, or rather set the random seed identically (now that I see it is a whole series you want replicated)
– user4686
Jan 14 at 10:34
Thanks for the answer @jfbu but I am fairly new to latex. Don't know macro too well. If you could provide me with easy resource to read on macro, I would be very grateful. Thanks.
– rhlchd
Jan 14 at 16:59
"macro" is a generic name, LaTeX "commands" are macros. But what you are lacking is knowledge oflet
oredef
which are not described usually in LaTeX books. But this is what is needed here simply to store once and for all a given random seed in a "macro" and use it after, see my answer.
– user4686
Jan 14 at 17:04
add a comment |
2
save the random number in a macro, or rather set the random seed identically (now that I see it is a whole series you want replicated)
– user4686
Jan 14 at 10:34
Thanks for the answer @jfbu but I am fairly new to latex. Don't know macro too well. If you could provide me with easy resource to read on macro, I would be very grateful. Thanks.
– rhlchd
Jan 14 at 16:59
"macro" is a generic name, LaTeX "commands" are macros. But what you are lacking is knowledge oflet
oredef
which are not described usually in LaTeX books. But this is what is needed here simply to store once and for all a given random seed in a "macro" and use it after, see my answer.
– user4686
Jan 14 at 17:04
2
2
save the random number in a macro, or rather set the random seed identically (now that I see it is a whole series you want replicated)
– user4686
Jan 14 at 10:34
save the random number in a macro, or rather set the random seed identically (now that I see it is a whole series you want replicated)
– user4686
Jan 14 at 10:34
Thanks for the answer @jfbu but I am fairly new to latex. Don't know macro too well. If you could provide me with easy resource to read on macro, I would be very grateful. Thanks.
– rhlchd
Jan 14 at 16:59
Thanks for the answer @jfbu but I am fairly new to latex. Don't know macro too well. If you could provide me with easy resource to read on macro, I would be very grateful. Thanks.
– rhlchd
Jan 14 at 16:59
"macro" is a generic name, LaTeX "commands" are macros. But what you are lacking is knowledge of
let
or edef
which are not described usually in LaTeX books. But this is what is needed here simply to store once and for all a given random seed in a "macro" and use it after, see my answer.– user4686
Jan 14 at 17:04
"macro" is a generic name, LaTeX "commands" are macros. But what you are lacking is knowledge of
let
or edef
which are not described usually in LaTeX books. But this is what is needed here simply to store once and for all a given random seed in a "macro" and use it after, see my answer.– user4686
Jan 14 at 17:04
add a comment |
2 Answers
2
active
oldest
votes
It would be easier if you had provided a usable test file but if I understand your question you could replace
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
}
by
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
}
To get 5 identical copies.
As jfbu has provided a test file here is the same technique but this time boxing the entire list and re-using. here I used a parbox of width textwidth
for a vertical repetition, you could make the parbox narrower and lay out horizontal copies.
documentclass{article}
usepackage{tikz}
tikzset{declare function={randomfixed(a,b) = int(random(0,int(b-a))+a);}}
% get random integer
newcommandrandomint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"(myval)":myval}%
finalegroup%
}
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
newcommandrandomeqint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"myval":myval}%
finalegroup%
}
% get random operator
defops{{"+","-","times","/"}}
defpmop{{"+","-"}}
newcommandrandomop{bgrouppgfmathsetmacroop{ops[int(rnd*4)]}opegroup}
%get random + or - only
newcommandrandompm{{bgrouppgfmathsetmacroop{pmop[int(rnd*2)]}opegroup}}
% 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:
noindentusebox{0}
bigskip
You fool, try again:
noindentusebox{0}
end{document}
I don't think this is what was asked by OP.
– user4686
Jan 14 at 16:02
@jfbu I had trouble understanding the question, but anyway the basic technique of putting the random text in a box and re-using the box answers the question even if I duplicated the wrong text. But I'll delete if it's definitely the wrong answer, OP to confirm...
– David Carlisle
Jan 14 at 16:06
In such cases don't moderators automatically hand over a share of your reps to @jfbu ?
– user4686
Jan 14 at 16:07
Thanks @DavidCarlisle for the effort but you are duplicating a random number 5 times one after another. That solves the problem in strict technical sense. But it does not help in obtaining the duplicate of a random number(s) many lines down the page. Think like this: I need to store the random number in a variable & print the variable in any two different places on the page. And many different variables containing different random numbers are to be printed in different places like a linear equation or algebraic expressions. But two of each question in a single A4 page on either half.
– rhlchd
Jan 14 at 16:58
@rhlchd you can use as many boxes as you like and display the content as often as you need, so you can for example typeset your whole question sheet into a box and then re-use that box multiple times, but as you did not provide a test file just a large but unusable code fragment it wasn't clear to me exactly what you wanted to duplicate.
– David Carlisle
Jan 14 at 17:25
|
show 3 more comments
documentclass{article}
usepackage{tikz}
tikzset{declare function={randomfixed(a,b) = int(random(0,int(b-a))+a);}}
% get random integer
newcommandrandomint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"(myval)":myval}%
finalegroup%
}
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
newcommandrandomeqint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"myval":myval}%
finalegroup%
}
% get random operator
defops{{"+","-","times","/"}}
defpmop{{"+","-"}}
newcommandrandomop{bgrouppgfmathsetmacroop{ops[int(rnd*4)]}opegroup}
%get random + or - only
newcommandrandompm{{bgrouppgfmathsetmacroop{pmop[int(rnd*2)]}opegroup}}
% choose random seed
edefmyrandomseed{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}
this looks promising will keep you updated :) Thanks
– rhlchd
Jan 14 at 17:02
@rhlchd the only addition to your code isedefmyrandomseed{pdfuniformdeviate 10000000}
which definesmyrandomseed
and uses it afterwards.
– user4686
Jan 14 at 17:04
Thanks I borrowed your test file:-) (@rhlchd)
– David Carlisle
Jan 14 at 19:47
Thanks to you too @jfbu for your solution. Will try it too. Multicol looks good to duplicate as a textbook layout. This is promising too :)
– rhlchd
Jan 15 at 12:46
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f470038%2fhow-can-i-duplicate-a-random-number-in-two-places-on-the-same-page-in-a-latex-ed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It would be easier if you had provided a usable test file but if I understand your question you could replace
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
}
by
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
}
To get 5 identical copies.
As jfbu has provided a test file here is the same technique but this time boxing the entire list and re-using. here I used a parbox of width textwidth
for a vertical repetition, you could make the parbox narrower and lay out horizontal copies.
documentclass{article}
usepackage{tikz}
tikzset{declare function={randomfixed(a,b) = int(random(0,int(b-a))+a);}}
% get random integer
newcommandrandomint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"(myval)":myval}%
finalegroup%
}
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
newcommandrandomeqint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"myval":myval}%
finalegroup%
}
% get random operator
defops{{"+","-","times","/"}}
defpmop{{"+","-"}}
newcommandrandomop{bgrouppgfmathsetmacroop{ops[int(rnd*4)]}opegroup}
%get random + or - only
newcommandrandompm{{bgrouppgfmathsetmacroop{pmop[int(rnd*2)]}opegroup}}
% 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:
noindentusebox{0}
bigskip
You fool, try again:
noindentusebox{0}
end{document}
I don't think this is what was asked by OP.
– user4686
Jan 14 at 16:02
@jfbu I had trouble understanding the question, but anyway the basic technique of putting the random text in a box and re-using the box answers the question even if I duplicated the wrong text. But I'll delete if it's definitely the wrong answer, OP to confirm...
– David Carlisle
Jan 14 at 16:06
In such cases don't moderators automatically hand over a share of your reps to @jfbu ?
– user4686
Jan 14 at 16:07
Thanks @DavidCarlisle for the effort but you are duplicating a random number 5 times one after another. That solves the problem in strict technical sense. But it does not help in obtaining the duplicate of a random number(s) many lines down the page. Think like this: I need to store the random number in a variable & print the variable in any two different places on the page. And many different variables containing different random numbers are to be printed in different places like a linear equation or algebraic expressions. But two of each question in a single A4 page on either half.
– rhlchd
Jan 14 at 16:58
@rhlchd you can use as many boxes as you like and display the content as often as you need, so you can for example typeset your whole question sheet into a box and then re-use that box multiple times, but as you did not provide a test file just a large but unusable code fragment it wasn't clear to me exactly what you wanted to duplicate.
– David Carlisle
Jan 14 at 17:25
|
show 3 more comments
It would be easier if you had provided a usable test file but if I understand your question you could replace
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
}
by
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
}
To get 5 identical copies.
As jfbu has provided a test file here is the same technique but this time boxing the entire list and re-using. here I used a parbox of width textwidth
for a vertical repetition, you could make the parbox narrower and lay out horizontal copies.
documentclass{article}
usepackage{tikz}
tikzset{declare function={randomfixed(a,b) = int(random(0,int(b-a))+a);}}
% get random integer
newcommandrandomint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"(myval)":myval}%
finalegroup%
}
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
newcommandrandomeqint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"myval":myval}%
finalegroup%
}
% get random operator
defops{{"+","-","times","/"}}
defpmop{{"+","-"}}
newcommandrandomop{bgrouppgfmathsetmacroop{ops[int(rnd*4)]}opegroup}
%get random + or - only
newcommandrandompm{{bgrouppgfmathsetmacroop{pmop[int(rnd*2)]}opegroup}}
% 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:
noindentusebox{0}
bigskip
You fool, try again:
noindentusebox{0}
end{document}
I don't think this is what was asked by OP.
– user4686
Jan 14 at 16:02
@jfbu I had trouble understanding the question, but anyway the basic technique of putting the random text in a box and re-using the box answers the question even if I duplicated the wrong text. But I'll delete if it's definitely the wrong answer, OP to confirm...
– David Carlisle
Jan 14 at 16:06
In such cases don't moderators automatically hand over a share of your reps to @jfbu ?
– user4686
Jan 14 at 16:07
Thanks @DavidCarlisle for the effort but you are duplicating a random number 5 times one after another. That solves the problem in strict technical sense. But it does not help in obtaining the duplicate of a random number(s) many lines down the page. Think like this: I need to store the random number in a variable & print the variable in any two different places on the page. And many different variables containing different random numbers are to be printed in different places like a linear equation or algebraic expressions. But two of each question in a single A4 page on either half.
– rhlchd
Jan 14 at 16:58
@rhlchd you can use as many boxes as you like and display the content as often as you need, so you can for example typeset your whole question sheet into a box and then re-use that box multiple times, but as you did not provide a test file just a large but unusable code fragment it wasn't clear to me exactly what you wanted to duplicate.
– David Carlisle
Jan 14 at 17:25
|
show 3 more comments
It would be easier if you had provided a usable test file but if I understand your question you could replace
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
}
by
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
}
To get 5 identical copies.
As jfbu has provided a test file here is the same technique but this time boxing the entire list and re-using. here I used a parbox of width textwidth
for a vertical repetition, you could make the parbox narrower and lay out horizontal copies.
documentclass{article}
usepackage{tikz}
tikzset{declare function={randomfixed(a,b) = int(random(0,int(b-a))+a);}}
% get random integer
newcommandrandomint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"(myval)":myval}%
finalegroup%
}
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
newcommandrandomeqint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"myval":myval}%
finalegroup%
}
% get random operator
defops{{"+","-","times","/"}}
defpmop{{"+","-"}}
newcommandrandomop{bgrouppgfmathsetmacroop{ops[int(rnd*4)]}opegroup}
%get random + or - only
newcommandrandompm{{bgrouppgfmathsetmacroop{pmop[int(rnd*2)]}opegroup}}
% 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:
noindentusebox{0}
bigskip
You fool, try again:
noindentusebox{0}
end{document}
It would be easier if you had provided a usable test file but if I understand your question you could replace
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
}
by
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
}
To get 5 identical copies.
As jfbu has provided a test file here is the same technique but this time boxing the entire list and re-using. here I used a parbox of width textwidth
for a vertical repetition, you could make the parbox narrower and lay out horizontal copies.
documentclass{article}
usepackage{tikz}
tikzset{declare function={randomfixed(a,b) = int(random(0,int(b-a))+a);}}
% get random integer
newcommandrandomint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"(myval)":myval}%
finalegroup%
}
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
newcommandrandomeqint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"myval":myval}%
finalegroup%
}
% get random operator
defops{{"+","-","times","/"}}
defpmop{{"+","-"}}
newcommandrandomop{bgrouppgfmathsetmacroop{ops[int(rnd*4)]}opegroup}
%get random + or - only
newcommandrandompm{{bgrouppgfmathsetmacroop{pmop[int(rnd*2)]}opegroup}}
% 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:
noindentusebox{0}
bigskip
You fool, try again:
noindentusebox{0}
end{document}
edited Jan 14 at 19:47
answered Jan 14 at 12:06
David CarlisleDavid Carlisle
490k4111341883
490k4111341883
I don't think this is what was asked by OP.
– user4686
Jan 14 at 16:02
@jfbu I had trouble understanding the question, but anyway the basic technique of putting the random text in a box and re-using the box answers the question even if I duplicated the wrong text. But I'll delete if it's definitely the wrong answer, OP to confirm...
– David Carlisle
Jan 14 at 16:06
In such cases don't moderators automatically hand over a share of your reps to @jfbu ?
– user4686
Jan 14 at 16:07
Thanks @DavidCarlisle for the effort but you are duplicating a random number 5 times one after another. That solves the problem in strict technical sense. But it does not help in obtaining the duplicate of a random number(s) many lines down the page. Think like this: I need to store the random number in a variable & print the variable in any two different places on the page. And many different variables containing different random numbers are to be printed in different places like a linear equation or algebraic expressions. But two of each question in a single A4 page on either half.
– rhlchd
Jan 14 at 16:58
@rhlchd you can use as many boxes as you like and display the content as often as you need, so you can for example typeset your whole question sheet into a box and then re-use that box multiple times, but as you did not provide a test file just a large but unusable code fragment it wasn't clear to me exactly what you wanted to duplicate.
– David Carlisle
Jan 14 at 17:25
|
show 3 more comments
I don't think this is what was asked by OP.
– user4686
Jan 14 at 16:02
@jfbu I had trouble understanding the question, but anyway the basic technique of putting the random text in a box and re-using the box answers the question even if I duplicated the wrong text. But I'll delete if it's definitely the wrong answer, OP to confirm...
– David Carlisle
Jan 14 at 16:06
In such cases don't moderators automatically hand over a share of your reps to @jfbu ?
– user4686
Jan 14 at 16:07
Thanks @DavidCarlisle for the effort but you are duplicating a random number 5 times one after another. That solves the problem in strict technical sense. But it does not help in obtaining the duplicate of a random number(s) many lines down the page. Think like this: I need to store the random number in a variable & print the variable in any two different places on the page. And many different variables containing different random numbers are to be printed in different places like a linear equation or algebraic expressions. But two of each question in a single A4 page on either half.
– rhlchd
Jan 14 at 16:58
@rhlchd you can use as many boxes as you like and display the content as often as you need, so you can for example typeset your whole question sheet into a box and then re-use that box multiple times, but as you did not provide a test file just a large but unusable code fragment it wasn't clear to me exactly what you wanted to duplicate.
– David Carlisle
Jan 14 at 17:25
I don't think this is what was asked by OP.
– user4686
Jan 14 at 16:02
I don't think this is what was asked by OP.
– user4686
Jan 14 at 16:02
@jfbu I had trouble understanding the question, but anyway the basic technique of putting the random text in a box and re-using the box answers the question even if I duplicated the wrong text. But I'll delete if it's definitely the wrong answer, OP to confirm...
– David Carlisle
Jan 14 at 16:06
@jfbu I had trouble understanding the question, but anyway the basic technique of putting the random text in a box and re-using the box answers the question even if I duplicated the wrong text. But I'll delete if it's definitely the wrong answer, OP to confirm...
– David Carlisle
Jan 14 at 16:06
In such cases don't moderators automatically hand over a share of your reps to @jfbu ?
– user4686
Jan 14 at 16:07
In such cases don't moderators automatically hand over a share of your reps to @jfbu ?
– user4686
Jan 14 at 16:07
Thanks @DavidCarlisle for the effort but you are duplicating a random number 5 times one after another. That solves the problem in strict technical sense. But it does not help in obtaining the duplicate of a random number(s) many lines down the page. Think like this: I need to store the random number in a variable & print the variable in any two different places on the page. And many different variables containing different random numbers are to be printed in different places like a linear equation or algebraic expressions. But two of each question in a single A4 page on either half.
– rhlchd
Jan 14 at 16:58
Thanks @DavidCarlisle for the effort but you are duplicating a random number 5 times one after another. That solves the problem in strict technical sense. But it does not help in obtaining the duplicate of a random number(s) many lines down the page. Think like this: I need to store the random number in a variable & print the variable in any two different places on the page. And many different variables containing different random numbers are to be printed in different places like a linear equation or algebraic expressions. But two of each question in a single A4 page on either half.
– rhlchd
Jan 14 at 16:58
@rhlchd you can use as many boxes as you like and display the content as often as you need, so you can for example typeset your whole question sheet into a box and then re-use that box multiple times, but as you did not provide a test file just a large but unusable code fragment it wasn't clear to me exactly what you wanted to duplicate.
– David Carlisle
Jan 14 at 17:25
@rhlchd you can use as many boxes as you like and display the content as often as you need, so you can for example typeset your whole question sheet into a box and then re-use that box multiple times, but as you did not provide a test file just a large but unusable code fragment it wasn't clear to me exactly what you wanted to duplicate.
– David Carlisle
Jan 14 at 17:25
|
show 3 more comments
documentclass{article}
usepackage{tikz}
tikzset{declare function={randomfixed(a,b) = int(random(0,int(b-a))+a);}}
% get random integer
newcommandrandomint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"(myval)":myval}%
finalegroup%
}
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
newcommandrandomeqint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"myval":myval}%
finalegroup%
}
% get random operator
defops{{"+","-","times","/"}}
defpmop{{"+","-"}}
newcommandrandomop{bgrouppgfmathsetmacroop{ops[int(rnd*4)]}opegroup}
%get random + or - only
newcommandrandompm{{bgrouppgfmathsetmacroop{pmop[int(rnd*2)]}opegroup}}
% choose random seed
edefmyrandomseed{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}
this looks promising will keep you updated :) Thanks
– rhlchd
Jan 14 at 17:02
@rhlchd the only addition to your code isedefmyrandomseed{pdfuniformdeviate 10000000}
which definesmyrandomseed
and uses it afterwards.
– user4686
Jan 14 at 17:04
Thanks I borrowed your test file:-) (@rhlchd)
– David Carlisle
Jan 14 at 19:47
Thanks to you too @jfbu for your solution. Will try it too. Multicol looks good to duplicate as a textbook layout. This is promising too :)
– rhlchd
Jan 15 at 12:46
add a comment |
documentclass{article}
usepackage{tikz}
tikzset{declare function={randomfixed(a,b) = int(random(0,int(b-a))+a);}}
% get random integer
newcommandrandomint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"(myval)":myval}%
finalegroup%
}
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
newcommandrandomeqint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"myval":myval}%
finalegroup%
}
% get random operator
defops{{"+","-","times","/"}}
defpmop{{"+","-"}}
newcommandrandomop{bgrouppgfmathsetmacroop{ops[int(rnd*4)]}opegroup}
%get random + or - only
newcommandrandompm{{bgrouppgfmathsetmacroop{pmop[int(rnd*2)]}opegroup}}
% choose random seed
edefmyrandomseed{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}
this looks promising will keep you updated :) Thanks
– rhlchd
Jan 14 at 17:02
@rhlchd the only addition to your code isedefmyrandomseed{pdfuniformdeviate 10000000}
which definesmyrandomseed
and uses it afterwards.
– user4686
Jan 14 at 17:04
Thanks I borrowed your test file:-) (@rhlchd)
– David Carlisle
Jan 14 at 19:47
Thanks to you too @jfbu for your solution. Will try it too. Multicol looks good to duplicate as a textbook layout. This is promising too :)
– rhlchd
Jan 15 at 12:46
add a comment |
documentclass{article}
usepackage{tikz}
tikzset{declare function={randomfixed(a,b) = int(random(0,int(b-a))+a);}}
% get random integer
newcommandrandomint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"(myval)":myval}%
finalegroup%
}
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
newcommandrandomeqint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"myval":myval}%
finalegroup%
}
% get random operator
defops{{"+","-","times","/"}}
defpmop{{"+","-"}}
newcommandrandomop{bgrouppgfmathsetmacroop{ops[int(rnd*4)]}opegroup}
%get random + or - only
newcommandrandompm{{bgrouppgfmathsetmacroop{pmop[int(rnd*2)]}opegroup}}
% choose random seed
edefmyrandomseed{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}
documentclass{article}
usepackage{tikz}
tikzset{declare function={randomfixed(a,b) = int(random(0,int(b-a))+a);}}
% get random integer
newcommandrandomint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"(myval)":myval}%
finalegroup%
}
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
newcommandrandomeqint[2]{bgroup%
pgfmathsetmacromyval{randomfixed(#1,#2)}%
pgfmathsetmacrofinal{(myval < 0)?"myval":myval}%
finalegroup%
}
% get random operator
defops{{"+","-","times","/"}}
defpmop{{"+","-"}}
newcommandrandomop{bgrouppgfmathsetmacroop{ops[int(rnd*4)]}opegroup}
%get random + or - only
newcommandrandompm{{bgrouppgfmathsetmacroop{pmop[int(rnd*2)]}opegroup}}
% choose random seed
edefmyrandomseed{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}
answered Jan 14 at 16:01
user4686
this looks promising will keep you updated :) Thanks
– rhlchd
Jan 14 at 17:02
@rhlchd the only addition to your code isedefmyrandomseed{pdfuniformdeviate 10000000}
which definesmyrandomseed
and uses it afterwards.
– user4686
Jan 14 at 17:04
Thanks I borrowed your test file:-) (@rhlchd)
– David Carlisle
Jan 14 at 19:47
Thanks to you too @jfbu for your solution. Will try it too. Multicol looks good to duplicate as a textbook layout. This is promising too :)
– rhlchd
Jan 15 at 12:46
add a comment |
this looks promising will keep you updated :) Thanks
– rhlchd
Jan 14 at 17:02
@rhlchd the only addition to your code isedefmyrandomseed{pdfuniformdeviate 10000000}
which definesmyrandomseed
and uses it afterwards.
– user4686
Jan 14 at 17:04
Thanks I borrowed your test file:-) (@rhlchd)
– David Carlisle
Jan 14 at 19:47
Thanks to you too @jfbu for your solution. Will try it too. Multicol looks good to duplicate as a textbook layout. This is promising too :)
– rhlchd
Jan 15 at 12:46
this looks promising will keep you updated :) Thanks
– rhlchd
Jan 14 at 17:02
this looks promising will keep you updated :) Thanks
– rhlchd
Jan 14 at 17:02
@rhlchd the only addition to your code is
edefmyrandomseed{pdfuniformdeviate 10000000}
which defines myrandomseed
and uses it afterwards.– user4686
Jan 14 at 17:04
@rhlchd the only addition to your code is
edefmyrandomseed{pdfuniformdeviate 10000000}
which defines myrandomseed
and uses it afterwards.– user4686
Jan 14 at 17:04
Thanks I borrowed your test file:-) (@rhlchd)
– David Carlisle
Jan 14 at 19:47
Thanks I borrowed your test file:-) (@rhlchd)
– David Carlisle
Jan 14 at 19:47
Thanks to you too @jfbu for your solution. Will try it too. Multicol looks good to duplicate as a textbook layout. This is promising too :)
– rhlchd
Jan 15 at 12:46
Thanks to you too @jfbu for your solution. Will try it too. Multicol looks good to duplicate as a textbook layout. This is promising too :)
– rhlchd
Jan 15 at 12:46
add a comment |
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f470038%2fhow-can-i-duplicate-a-random-number-in-two-places-on-the-same-page-in-a-latex-ed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
save the random number in a macro, or rather set the random seed identically (now that I see it is a whole series you want replicated)
– user4686
Jan 14 at 10:34
Thanks for the answer @jfbu but I am fairly new to latex. Don't know macro too well. If you could provide me with easy resource to read on macro, I would be very grateful. Thanks.
– rhlchd
Jan 14 at 16:59
"macro" is a generic name, LaTeX "commands" are macros. But what you are lacking is knowledge of
let
oredef
which are not described usually in LaTeX books. But this is what is needed here simply to store once and for all a given random seed in a "macro" and use it after, see my answer.– user4686
Jan 14 at 17:04