How to create a random math problem in LaTeX?Random quadratic equationHow to break out of a loopIncluding a...
Are small insurances worth it
Did 5.25" floppies undergo a change in magnetic coating?
Where is the line between being obedient and getting bullied by a boss
How to approximate rolls for potions of healing using only d6's?
my breadboard simulation doesn't work properly
What can I substitute for soda pop in a sweet pork recipe?
What does 'acting greedily' mean?
What am I? I am in theaters and computer programs
How to kill a localhost:8080
Traversing Africa: A Cryptic Journey
Where is the fallacy here?
Skis versus snow shoes - when to choose which for travelling the backcountry?
Can I become debt free or should I file for bankruptcy? How do I manage my debt and finances?
Canadian citizen, on US no-fly list. What can I do in order to be allowed on flights which go through US airspace?
Hacker Rank: Array left rotation
Inverse of the covariance matrix of a multivariate normal distribution
Why is working on the same position for more than 15 years not a red flag?
Practical reasons to have both a large police force and bounty hunting network?
Did Amazon pay $0 in taxes last year?
When was drinking water recognized as crucial in marathon running?
What is the wife of a henpecked husband called?
I encountered my boss during an on-site interview at another company. Should I bring it up when seeing him next time?
Is there a ternary operator in math?
How to lift/raise/repair a segment of concrete slab?
How to create a random math problem in LaTeX?
Random quadratic equationHow to break out of a loopIncluding a Matlab table in documentCreating random problems by using rand optionAutomatically add fractions and reduce the result (if neccessary)Automated Creation of Questions and Solutions for a Worksheet/ExamRandom form letter textHow to include random images?Generating random numbers without repetitionsProblem generating a random worksheetHow can one create a random GUID?Somewhat(?) Random Numbers?Prune list for random order without repetition problemHow to create multipage tables of binary arithmetical problems (and associated answers) involving pseudo-random numbers?How to Create Random Letter?How can I duplicate a random number in two places on the same page in a latex editor?
I want to create a math problem-solution sheet. The coefficients a, b, c must be assigned on the fly at random so the generated pdf will show a numeric quadratic equation rather than a symbolic quadratic equation. Note a must not be zero.
documentclass{article}
begin{document}
section{Problem}
Let (x_1) and (x_2) be the roots of (a x^2 +bx +c=0). Find (x_1x_2).
section{Solution}
[
x_1x_2=frac{c}{a}
]
end{document}
Please kindly make the solution get automatically simplified. For example: frac{c}{a}=3 when a=2 and c=6.
Actually the real case is more complex than this one. Not only the equations must be generated at random, but also the associated graphic plots that I write in PSTricks.
I chose Paulo's answer because using Maxima as the computation engine is very very great idea. Other answers are also good and creative, especially Altermundus'.
random-numbers
This question has an open bounty worth +500
reputation from The Inventor of God ending in 6 days.
This question has not received enough attention.
--- The bounty will be granted to ones who can use Computer Algebra Systems that have not been used by the existing answers. Using SageTeX is preferred.
|
show 1 more comment
I want to create a math problem-solution sheet. The coefficients a, b, c must be assigned on the fly at random so the generated pdf will show a numeric quadratic equation rather than a symbolic quadratic equation. Note a must not be zero.
documentclass{article}
begin{document}
section{Problem}
Let (x_1) and (x_2) be the roots of (a x^2 +bx +c=0). Find (x_1x_2).
section{Solution}
[
x_1x_2=frac{c}{a}
]
end{document}
Please kindly make the solution get automatically simplified. For example: frac{c}{a}=3 when a=2 and c=6.
Actually the real case is more complex than this one. Not only the equations must be generated at random, but also the associated graphic plots that I write in PSTricks.
I chose Paulo's answer because using Maxima as the computation engine is very very great idea. Other answers are also good and creative, especially Altermundus'.
random-numbers
This question has an open bounty worth +500
reputation from The Inventor of God ending in 6 days.
This question has not received enough attention.
--- The bounty will be granted to ones who can use Computer Algebra Systems that have not been used by the existing answers. Using SageTeX is preferred.
2
Pretty interesting question. I remember dealing with something similar, but my approach was quite hybrid. It was something like generating random coefficients in LaTeX, call a computer algebra system to solve it for me, then redirect the output back to LaTeX. I used Maxima's functiontex(solve(equation))which gives me the result in the LaTeX math format.
– Paulo Cereda
Jun 10 '11 at 15:40
2
andreas.scherbaum.la/blog/archives/…
– Marco Daniel
Jun 10 '11 at 15:43
Oh no.. I did not seeSageTeXhas been used. :-(
– The Inventor of God
23 hours ago
Yes. That post inspired me to create a lot of tests/quizzes using specific problem types where the numbers are created at random (and then create a solution sheet) using thesagetexpackage. It is the package to learn if you are teaching math
– DJP
22 hours ago
If this is still going to be open for a bounty, what sort of random problems are you looking for: arithmetic, algebra, precalculus, discrete math? Or you still want quadratic mentioned and already solved?
– DJP
22 hours ago
|
show 1 more comment
I want to create a math problem-solution sheet. The coefficients a, b, c must be assigned on the fly at random so the generated pdf will show a numeric quadratic equation rather than a symbolic quadratic equation. Note a must not be zero.
documentclass{article}
begin{document}
section{Problem}
Let (x_1) and (x_2) be the roots of (a x^2 +bx +c=0). Find (x_1x_2).
section{Solution}
[
x_1x_2=frac{c}{a}
]
end{document}
Please kindly make the solution get automatically simplified. For example: frac{c}{a}=3 when a=2 and c=6.
Actually the real case is more complex than this one. Not only the equations must be generated at random, but also the associated graphic plots that I write in PSTricks.
I chose Paulo's answer because using Maxima as the computation engine is very very great idea. Other answers are also good and creative, especially Altermundus'.
random-numbers
I want to create a math problem-solution sheet. The coefficients a, b, c must be assigned on the fly at random so the generated pdf will show a numeric quadratic equation rather than a symbolic quadratic equation. Note a must not be zero.
documentclass{article}
begin{document}
section{Problem}
Let (x_1) and (x_2) be the roots of (a x^2 +bx +c=0). Find (x_1x_2).
section{Solution}
[
x_1x_2=frac{c}{a}
]
end{document}
Please kindly make the solution get automatically simplified. For example: frac{c}{a}=3 when a=2 and c=6.
Actually the real case is more complex than this one. Not only the equations must be generated at random, but also the associated graphic plots that I write in PSTricks.
I chose Paulo's answer because using Maxima as the computation engine is very very great idea. Other answers are also good and creative, especially Altermundus'.
random-numbers
random-numbers
edited Jun 10 '11 at 20:31
xport
asked Jun 10 '11 at 15:17
xportxport
22.1k30139262
22.1k30139262
This question has an open bounty worth +500
reputation from The Inventor of God ending in 6 days.
This question has not received enough attention.
--- The bounty will be granted to ones who can use Computer Algebra Systems that have not been used by the existing answers. Using SageTeX is preferred.
This question has an open bounty worth +500
reputation from The Inventor of God ending in 6 days.
This question has not received enough attention.
--- The bounty will be granted to ones who can use Computer Algebra Systems that have not been used by the existing answers. Using SageTeX is preferred.
2
Pretty interesting question. I remember dealing with something similar, but my approach was quite hybrid. It was something like generating random coefficients in LaTeX, call a computer algebra system to solve it for me, then redirect the output back to LaTeX. I used Maxima's functiontex(solve(equation))which gives me the result in the LaTeX math format.
– Paulo Cereda
Jun 10 '11 at 15:40
2
andreas.scherbaum.la/blog/archives/…
– Marco Daniel
Jun 10 '11 at 15:43
Oh no.. I did not seeSageTeXhas been used. :-(
– The Inventor of God
23 hours ago
Yes. That post inspired me to create a lot of tests/quizzes using specific problem types where the numbers are created at random (and then create a solution sheet) using thesagetexpackage. It is the package to learn if you are teaching math
– DJP
22 hours ago
If this is still going to be open for a bounty, what sort of random problems are you looking for: arithmetic, algebra, precalculus, discrete math? Or you still want quadratic mentioned and already solved?
– DJP
22 hours ago
|
show 1 more comment
2
Pretty interesting question. I remember dealing with something similar, but my approach was quite hybrid. It was something like generating random coefficients in LaTeX, call a computer algebra system to solve it for me, then redirect the output back to LaTeX. I used Maxima's functiontex(solve(equation))which gives me the result in the LaTeX math format.
– Paulo Cereda
Jun 10 '11 at 15:40
2
andreas.scherbaum.la/blog/archives/…
– Marco Daniel
Jun 10 '11 at 15:43
Oh no.. I did not seeSageTeXhas been used. :-(
– The Inventor of God
23 hours ago
Yes. That post inspired me to create a lot of tests/quizzes using specific problem types where the numbers are created at random (and then create a solution sheet) using thesagetexpackage. It is the package to learn if you are teaching math
– DJP
22 hours ago
If this is still going to be open for a bounty, what sort of random problems are you looking for: arithmetic, algebra, precalculus, discrete math? Or you still want quadratic mentioned and already solved?
– DJP
22 hours ago
2
2
Pretty interesting question. I remember dealing with something similar, but my approach was quite hybrid. It was something like generating random coefficients in LaTeX, call a computer algebra system to solve it for me, then redirect the output back to LaTeX. I used Maxima's function
tex(solve(equation)) which gives me the result in the LaTeX math format.– Paulo Cereda
Jun 10 '11 at 15:40
Pretty interesting question. I remember dealing with something similar, but my approach was quite hybrid. It was something like generating random coefficients in LaTeX, call a computer algebra system to solve it for me, then redirect the output back to LaTeX. I used Maxima's function
tex(solve(equation)) which gives me the result in the LaTeX math format.– Paulo Cereda
Jun 10 '11 at 15:40
2
2
andreas.scherbaum.la/blog/archives/…
– Marco Daniel
Jun 10 '11 at 15:43
andreas.scherbaum.la/blog/archives/…
– Marco Daniel
Jun 10 '11 at 15:43
Oh no.. I did not see
SageTeX has been used. :-(– The Inventor of God
23 hours ago
Oh no.. I did not see
SageTeX has been used. :-(– The Inventor of God
23 hours ago
Yes. That post inspired me to create a lot of tests/quizzes using specific problem types where the numbers are created at random (and then create a solution sheet) using the
sagetex package. It is the package to learn if you are teaching math– DJP
22 hours ago
Yes. That post inspired me to create a lot of tests/quizzes using specific problem types where the numbers are created at random (and then create a solution sheet) using the
sagetex package. It is the package to learn if you are teaching math– DJP
22 hours ago
If this is still going to be open for a bounty, what sort of random problems are you looking for: arithmetic, algebra, precalculus, discrete math? Or you still want quadratic mentioned and already solved?
– DJP
22 hours ago
If this is still going to be open for a bounty, what sort of random problems are you looking for: arithmetic, algebra, precalculus, discrete math? Or you still want quadratic mentioned and already solved?
– DJP
22 hours ago
|
show 1 more comment
7 Answers
7
active
oldest
votes
This is a partial solution and probably it's not what you want. However, I'm posting it solely for record purposes, in case anyone would need it. =)
I'll use Maxima:
Maxima is a system for the manipulation of symbolic and numerical expressions, including differentiation, integration, Taylor series, Laplace transforms, ordinary differential equations, systems of linear equations, polynomials, and sets, lists, vectors, matrices, and tensors. Maxima yields high precision numeric results by using exact fractions, arbitrary precision integers, and variable precision floating point numbers. Maxima can plot functions and data in two and three dimensions.
First of all, I created a script file named solve.cmd in my working directory:
@echo off
rem If Maxima is not in your path, you need to set it.
rem Also, I'm making use of the 'head' command, you can
rem obtain the Win32 version easily with MSys.
set PATH=%PATH%;"C:Program FilesMaxima-5.24.0bin"
maxima --very-quiet -r %1 | head --lines=-1
I'm not in my Linux box right now, but a .sh file is similar.
Using Jake's code, I added a call to this script:
documentclass{article}
usepackage{pgf}
begin{document}
section{Problem}
pgfmathsetseed{1}
pgfmathtruncatemacrocoeffa{random(2,10)}
pgfmathtruncatemacrocoeffb{random(2,10)}
pgfmathtruncatemacrocoeffc{random(2,10)}
% call to the 'solve' script providing the coefficients.
% Maxima's 'solve' function will return x1 and x2, and the
% 'tex' function will convert the answer to TeX. The last
% line of the output will be removed (the 'head' command)
% because Maxima adds a 'false' line to the end of the file.
% Then we redirect the output to a file.
immediatewrite18{solve.cmd "tex(solve(coeffa*x^2 + (coeffb*x) + (coeffc)));" > solution1.tex}
Let (x_1) and (x_2) be the roots of (coeffa x^2 +coeffb x +coeffc=0). Find (x_1x_2).
section{Solution}
% read the solution
input solution1
end{document}
I tried to use Maxima's plot2d function together with the call, but I failed to fix a minor error with file extensions. I intended to create a png plot with gnuplot and include it. Sorry, it didn't work yet.
Don't forget to use the --shell-escape or --enable-write18 flag.
EDIT: It gave me a tremendous headache, but I guess I found a nice improvement. =)
I'm not used to Maxima, though I like to use it for simple calculations. If my code is somehow incorrect, please help me improve it. Thanks.
I edited the solve.cmd file in my working directory to look like this:
@echo off
rem If Maxima is not in your path, you need to set it.
rem Now, I don't use 'head' anymore, but 'grep'. The idea
rem behind this code is:
rem 1. generate the three coefficients,
rem 2. output both equation and solution to a file,
rem 3. get rid of the 'false' lines in the text (Maxima's fault)
rem (we do this thanks to 'grep' using an inverse flag)
rem 4. direct output to a file 'problem.txt'.
@echo off
set PATH=%PATH%;"C:Program FilesMaxima-5.24.0bin"
maxima --very-quiet -r "s: make_random_state(true)$ set_random_state(s)$ fullrand(low, high) := floor(random(1.0) * ( 1 + high - low)) + low$ a : fullrand(-10,10)$ s: b : fullrand(-10,10)$ c : fullrand(-10,10)$ tex(a*x^2 + b*x + c); tex(solve(a*x^2 + b*x + c));" | grep -v false > problem.txt
Now the tricky part, the LaTeX code:
documentclass{article}
immediatewrite18{solve.cmd}
newreadmyread
openinmyread=problem.txt
readmyread to theequation
readmyread to thesolution
begin{document}
section{Problem}
This is our equation:
theequation
section{Solution}
And this is our solution:
thesolution
end{document}
Let's check the code: first of all, we run solve.cmd, which outputs both equation and solution to a file named problem.txt. Then we create a newreadmyread and open this file openinmyread=problem.txt. Now we map the first line to theequation (which is the equation which we defined in our Maxima code) and the second line to thesolution (same logic). Then we call it when needed.
Again, probably some of my Maxima code is faulty. But I hope you get the idea behind this. In case of changing the equation or solution to a inline version, we may probably do some sed in it and replace the pattern. =)
@xport: glad you like it. I'm trying to improve the code as you suggested, but unfortunately I'm not that used with Maxima. EDIT: I hope to come up with something. ;-)
– Paulo Cereda
Jun 10 '11 at 18:55
@xport: hm did you use the--shell-escapeflag? Unfortunately, a single mistake (like a missing;) will break the call. I suffered a lot before getting it right.
– Paulo Cereda
Jun 10 '11 at 19:27
@xport: hm good, at least it worked! The missingheadcommand is part of a Win32 port of some useful Unix tools. I uploaded mine for you to use, put in your working directory. I hope it helps. PS: I'm on a fight with Maxima right now. =)
– Paulo Cereda
Jun 10 '11 at 19:47
@xport: Mine came from MSys/MinGW, but I believe it's a big payload unless you want to usegccand related tools. This project may sound more interesting.
– Paulo Cereda
Jun 10 '11 at 19:57
@xport: hm your code works for me, using MikTeX 2.9. I'm gonna try in another machine with TeXLive.
– Paulo Cereda
Jun 10 '11 at 20:15
|
show 10 more comments
I know I'm late to the party here, but I would use Sage and SageTeX:
documentclass{article}
usepackage{sagetex}
begin{document}
section{Problem}
begin{sagesilent}
a = ZZ.random_element(-10,10)
while a == 0:
a = ZZ.random_element(-10,10)
b = ZZ.random_element(-10,10)
c = ZZ.random_element(-10,10)
poly = a*x^2 + b*x + c
end{sagesilent}
Let (x_1) and (x_2) be the roots of (sage{poly}=0).
Find (x_1x_2).
section{Solution}
[
x_1x_2=sage{c/a}.
]
sageplot{plot(poly) }
end{document}
The output looks like this:

1
A little late but worth the wait! Your solution inspired me to solve the problems I had earlier (with the system finding sagetex.sty) and after spending a couple of hours messing around, it seems clear to me that sagetex package is the "must have" package for those who might use a program like Sage.
– DJP
Jul 18 '11 at 13:59
add a comment |
The lcg package contains macros to generate (pseudo) random numbers. A little example (the plot is also generated but using TikZ):
documentclass{article}
usepackage[counter=coefa,first=-8, last=12]{lcg}
usepackage{pgfplots}
begin{document}
randarabic{coefa}
letCoefathecoefa
chgrand[counter=coefb]
randarabic{coefb}
letCoefbthecoefb
chgrand[counter=coefc]
randarabic{coefc}
letCoefcthecoefc
section{Problem}
Let (x_1) and (x_2) be the roots of ( Coefa x^2 +Coefb x + Coefc =0). Find (x_1x_2).
section{Solution}
[
x_1x_2=frac{Coefc}{Coefa}
]
begin{tikzpicture}
begin{axis}[
xlabel=$x$,
ylabel={$f(x) = Coefa x^2 + Coefb x + Coefc $}
]
addplot[color=orange,thick] {Coefa*x^2 + Coefb* x + Coefc};
end{axis}
end{tikzpicture}
end{document}

Of course, when compiling my example you'll get different values for the corfficients, so a different equation and plot will be obtained.
@xport: I'll think about the simplification problem. I've updated my answer adding the plot of the quadratic function (but usingTikZ).
– Gonzalo Medina
Jun 10 '11 at 16:39
Beautiful answer.
– ℝaphink
Jun 10 '11 at 17:32
add a comment |
To simplify the fractions, you can use Euclid's Method, using the minimal below with
reduceFrac{27}{81}
$frac{rfNumer}{rfDenom}$
will give you 1/3. To simplify to a round number is more difficult, as you need to check if there is a remainder left over, so you will need a decimal to fraction conversion routine. See How to break out of a loop at the end of the code, you will find how to use the macros to convert for example 0.375 to the fractional approximation 768/2048. No guarantees for its accuracy, needs a bit of refinement.
documentclass{article}
begin{document}
makeatletter
% Use Euclid's Algorithm to find the greatest
% common divisor of two integers.
defgcd#1#2{{% #1 = a, #2 = b
ifnum#2=0 edefnext{#1}else
@tempcnta=#1 @tempcntb=#2 divide@tempcnta by@tempcntb
multiply@tempcnta by@tempcntb % q*b
@tempcntb=#1
advance@tempcntb by-@tempcnta % remainder in @tempcntb
ifnum@tempcntb=0
@tempcnta=#2
ifnum@tempcnta < 0 @tempcnta=-@tempcntafi
xdefgcd@next{noexpand%
defnoexpandthegcd{the@tempcnta}}%
else
xdefgcd@next{noexpandgcd{#2}{the@tempcntb}}%
fi
fi}gcd@next
}
newcommandreduceFrac[2]
{%
gcd{#1}{#2}{@tempcnta=#1 divide@tempcnta bythegcd
@tempcntb=#2 divide@tempcntb bythegcd
ifnum@tempcntb<0relax
@tempcntb=-@tempcntb
@tempcnta=-@tempcnta
fi
xdefrfNumer{the@tempcnta}
xdefrfDenom{the@tempcntb}}%
}
reduceFrac{27}{81}
$frac{rfNumer}{rfDenom}$
makeatother
end{document}
@ Yiannisgcdis now in pgf 2.1 CVS and reduceFrac is calledtkzReducFracintkz-tools-arith.tex. Now it's a part of TL2011.gcdis also intkz-tools-arith.texbut I made a test to use pgfmath if the version is the CVS one . See my answer
– Alain Matthes
Jun 10 '11 at 17:38
@Altermundus that is great, I can now delete all these from my hardisk!
– Yiannis Lazarides
Jun 10 '11 at 17:41
@ Yiannis Yes great but I'm afraid ... I hope there are not too many errors in all the packages
– Alain Matthes
Jun 10 '11 at 17:55
@Altermundus See if you can also make use of tex.stackexchange.com/questions/12481/…
– Yiannis Lazarides
Jun 10 '11 at 18:03
@Yiannis I don't see this question. It's interesting
– Alain Matthes
Jun 10 '11 at 18:07
add a comment |
A solution with Lua that handles coefficients that are smaller than 2 correctly.
% Compile with lualatex.
documentclass{article}
usepackage{luacode}
begin{luacode*}
function gcd (a,b)
if b == 0 then
return a
end
return gcd(b, a % b)
end
function makeproblem ()
local a,b,c
a = math.random (-10,10)
if a == 0 then a = 1 end
b = math.random (-10,10)
c = math.random (-10,10)
-- Pretty-print the equation.
if a == - 1 then
tex.sprint('-')
elseif a ~= 1 then
tex.sprint(a)
end
tex.sprint( 'x^2' )
if b ~= 0 then
if b > 0 then tex.sprint('+') end
if b ~= 1 and b ~= -1 then tex.sprint(b) end
if b == -1 then tex.sprint('-') end
tex.sprint('x')
end
if c ~= 0 then
if c > 0 then tex.sprint('+') end
tex.sprint(c)
end
tex.sprint('=0')
-- Simplify the solution
local d
d = gcd(a,c)
a = a/d
c = c/d
local sign = 1
if a < 0 then
sign = -1
a = -a
end
if c < 0 then
sign = -sign
c = -c
end
-- Store the solution in solution.
tex.sprint('\gdef\solution{')
if sign < 0 then tex.sprint('-') end
if a == 1 or c == 0 then
tex.sprint(c)
else
tex.sprint('\frac{' .. c ..'}{'.. a ..'}')
end
tex.sprint('}')
end
end{luacode*}
newcommandmakeproblem{directlua{makeproblem()}}
begin{document}
section{Problem}
Let (x_1) and (x_2) be the roots of (makeproblem). Find (x_1x_2).
section{Solution}
[
x_1x_2=solution
]
end{document}

+1 It is also good. However, it will become more complicated for creating more complicated math problems.
– xport
Jun 11 '11 at 5:24
I've taken the liberty of adding a bit of meta code to pretty-print the Lua code chunk. Feel free to revert.
– Mico
18 hours ago
add a comment |
Funny problem and it's a possibility to test TL 2011 with my package tkz-fct. This package calls tkz-tool-arith.tex and inside this file I define gcd and tkzReducFrac. gcd now is a part of pgf CVS with isprime isodd and iseven.
I use the code of Jake for random. The study of the function is not completed, I give this solution only to see what we can make with little tools.
I compile with xelatex and with -shell-escape because tkz-fct uses only gnuplot
tkzReducFrac defines two macros tkzMathFirstResultand tkzMathSecondResult
documentclass{article}
usepackage{tkz-fct}
begin{document}
section{Problem}
pgfmathsetseed{1}
pgfmathtruncatemacrocoeffa{random(2,10)}
pgfmathtruncatemacrocoeffb{random(2,10)}
pgfmathtruncatemacrocoeffc{random(2,10)}
Let (x_1) and (x_2) be the roots of (coeffa x^2 +coeffb x +coeffc=0). Find (x_1x_2).
tkzReducFrac{coeffc}{coeffa}
section{Solution}
[
x_1x_2=frac{tkzMathFirstResult}{tkzMathSecondResult}
]
% only to make some tests
pgfmathsetmacrodiscrimin{coeffb*coeffb-4*coeffa*coeffc}
pgfmathparse{ifthenelse(notless(discrimin,0)==1,">=0","<0")}
$Delta=b^2-4ac=discriminpgfmathresult$
$a=coeffa$
pgfmathsetmacroxmid{-coeffb/(2*coeffa)}
pgfmathsetmacroxmin{xmid-5} % to adjust the grid and the background
pgfmathsetmacroxmax{xmid+5}
$-b/(2a)=xmid$
% minimum to adjust vertically but it's possible to use a better code
ifnumcoeffa > 0
pgfmathsetmacroymin{-1-discrimin/(coeffa*coeffa)}
pgfmathsetmacroymax{ymin+10}
else
pgfmathsetmacroymax{-1-discrimin/(coeffa*coeffa)}
pgfmathsetmacroymin{ymax-10}
fi
begin{tikzpicture}
tkzInit[xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax]
tkzGrid
tkzAxeXY
tkzFct[blue,thick]{coeffa*x*x+coeffb*x+coeffc}
end{tikzpicture}
end{document}

@xport: +1 is for good answers, not for simply answering.
– Vivi
Jun 11 '11 at 9:27
@xport: this was a good answer, but your comment here and elsewhere of "+1 for answering" implies differently.
– Vivi
Jun 11 '11 at 21:24
add a comment |
MATLAB version, possibly adaptable to Octave:
First, download my parsetex.m function to somewhere in your search path. Next,
make a template .tex file named foo-gradingkey.tex, with contents like
documentclass{article}
begin{document}
Hello, **studentName**.
section{Problem}
% ^^a=round(9*rand)+1;^^
% ^^b=round(10*rand);^^
% ^^c=round(10*rand);^^
Let (x_1) and (x_2) be the roots of (**a** x^2 + **b** x + **c**=0).
Find (x_1x_2).
section{Solution}
[ x_1x_2=frac{**c**}{**a**}= **c/a** ]
end{document}
Paired double carets and paired double asterisks indicate MATLAB code to be evaluated. Material in paired double carets will remain in the final file, while material in pared double asterisks will be replaced with the evaluated result.
Run the following command in MATLAB:
parsetex('foo-%s.tex',{'Mike Renfro'},{'renfro'});which results in the following message on the command line:
Creating file foo-renfro.tex: done
The contents of foo-renfro.tex will be something like
documentclass{article}
begin{document}
Hello, Mike Renfro.
section{Problem}
% ^^a=round(9*rand)+1;^^
% ^^b=round(10*rand);^^
% ^^c=round(10*rand);^^
Let (x_1) and (x_2) be the roots of (10 x^2 + 2 x + 10=0).
Find (x_1x_2).
section{Solution}
[ x_1x_2=frac{10}{10}= 1 ]
end{document}
It's not perfect, since I've not yet managed to get it to reduce fractions to simplest form, i.e., 2/5 ends up being evaluated as 0.4.
+1: it is interesting. Unfortunately, Matlab & Mathematica are very expensive softwares.
– xport
Jun 11 '11 at 5:21
This approach is very fluid coding workflow.
– xport
Jun 11 '11 at 5:39
@xport: it should work in GNU Octave (this page looks like they have compatible cell arrays), but I've not tried it myself.
– Mike Renfro
Jun 11 '11 at 12:55
I haven't looked through this solution thoroughly to see if this would solve your problem with expressing decimal numbers as rational fractions, but Matlab has aratfunction that might help here; however, it also might make for messy fractions when the answer is better-expressed as an approximate decimal number.
– JohnReed
Aug 5 '12 at 13:40
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%2f20404%2fhow-to-create-a-random-math-problem-in-latex%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is a partial solution and probably it's not what you want. However, I'm posting it solely for record purposes, in case anyone would need it. =)
I'll use Maxima:
Maxima is a system for the manipulation of symbolic and numerical expressions, including differentiation, integration, Taylor series, Laplace transforms, ordinary differential equations, systems of linear equations, polynomials, and sets, lists, vectors, matrices, and tensors. Maxima yields high precision numeric results by using exact fractions, arbitrary precision integers, and variable precision floating point numbers. Maxima can plot functions and data in two and three dimensions.
First of all, I created a script file named solve.cmd in my working directory:
@echo off
rem If Maxima is not in your path, you need to set it.
rem Also, I'm making use of the 'head' command, you can
rem obtain the Win32 version easily with MSys.
set PATH=%PATH%;"C:Program FilesMaxima-5.24.0bin"
maxima --very-quiet -r %1 | head --lines=-1
I'm not in my Linux box right now, but a .sh file is similar.
Using Jake's code, I added a call to this script:
documentclass{article}
usepackage{pgf}
begin{document}
section{Problem}
pgfmathsetseed{1}
pgfmathtruncatemacrocoeffa{random(2,10)}
pgfmathtruncatemacrocoeffb{random(2,10)}
pgfmathtruncatemacrocoeffc{random(2,10)}
% call to the 'solve' script providing the coefficients.
% Maxima's 'solve' function will return x1 and x2, and the
% 'tex' function will convert the answer to TeX. The last
% line of the output will be removed (the 'head' command)
% because Maxima adds a 'false' line to the end of the file.
% Then we redirect the output to a file.
immediatewrite18{solve.cmd "tex(solve(coeffa*x^2 + (coeffb*x) + (coeffc)));" > solution1.tex}
Let (x_1) and (x_2) be the roots of (coeffa x^2 +coeffb x +coeffc=0). Find (x_1x_2).
section{Solution}
% read the solution
input solution1
end{document}
I tried to use Maxima's plot2d function together with the call, but I failed to fix a minor error with file extensions. I intended to create a png plot with gnuplot and include it. Sorry, it didn't work yet.
Don't forget to use the --shell-escape or --enable-write18 flag.
EDIT: It gave me a tremendous headache, but I guess I found a nice improvement. =)
I'm not used to Maxima, though I like to use it for simple calculations. If my code is somehow incorrect, please help me improve it. Thanks.
I edited the solve.cmd file in my working directory to look like this:
@echo off
rem If Maxima is not in your path, you need to set it.
rem Now, I don't use 'head' anymore, but 'grep'. The idea
rem behind this code is:
rem 1. generate the three coefficients,
rem 2. output both equation and solution to a file,
rem 3. get rid of the 'false' lines in the text (Maxima's fault)
rem (we do this thanks to 'grep' using an inverse flag)
rem 4. direct output to a file 'problem.txt'.
@echo off
set PATH=%PATH%;"C:Program FilesMaxima-5.24.0bin"
maxima --very-quiet -r "s: make_random_state(true)$ set_random_state(s)$ fullrand(low, high) := floor(random(1.0) * ( 1 + high - low)) + low$ a : fullrand(-10,10)$ s: b : fullrand(-10,10)$ c : fullrand(-10,10)$ tex(a*x^2 + b*x + c); tex(solve(a*x^2 + b*x + c));" | grep -v false > problem.txt
Now the tricky part, the LaTeX code:
documentclass{article}
immediatewrite18{solve.cmd}
newreadmyread
openinmyread=problem.txt
readmyread to theequation
readmyread to thesolution
begin{document}
section{Problem}
This is our equation:
theequation
section{Solution}
And this is our solution:
thesolution
end{document}
Let's check the code: first of all, we run solve.cmd, which outputs both equation and solution to a file named problem.txt. Then we create a newreadmyread and open this file openinmyread=problem.txt. Now we map the first line to theequation (which is the equation which we defined in our Maxima code) and the second line to thesolution (same logic). Then we call it when needed.
Again, probably some of my Maxima code is faulty. But I hope you get the idea behind this. In case of changing the equation or solution to a inline version, we may probably do some sed in it and replace the pattern. =)
@xport: glad you like it. I'm trying to improve the code as you suggested, but unfortunately I'm not that used with Maxima. EDIT: I hope to come up with something. ;-)
– Paulo Cereda
Jun 10 '11 at 18:55
@xport: hm did you use the--shell-escapeflag? Unfortunately, a single mistake (like a missing;) will break the call. I suffered a lot before getting it right.
– Paulo Cereda
Jun 10 '11 at 19:27
@xport: hm good, at least it worked! The missingheadcommand is part of a Win32 port of some useful Unix tools. I uploaded mine for you to use, put in your working directory. I hope it helps. PS: I'm on a fight with Maxima right now. =)
– Paulo Cereda
Jun 10 '11 at 19:47
@xport: Mine came from MSys/MinGW, but I believe it's a big payload unless you want to usegccand related tools. This project may sound more interesting.
– Paulo Cereda
Jun 10 '11 at 19:57
@xport: hm your code works for me, using MikTeX 2.9. I'm gonna try in another machine with TeXLive.
– Paulo Cereda
Jun 10 '11 at 20:15
|
show 10 more comments
This is a partial solution and probably it's not what you want. However, I'm posting it solely for record purposes, in case anyone would need it. =)
I'll use Maxima:
Maxima is a system for the manipulation of symbolic and numerical expressions, including differentiation, integration, Taylor series, Laplace transforms, ordinary differential equations, systems of linear equations, polynomials, and sets, lists, vectors, matrices, and tensors. Maxima yields high precision numeric results by using exact fractions, arbitrary precision integers, and variable precision floating point numbers. Maxima can plot functions and data in two and three dimensions.
First of all, I created a script file named solve.cmd in my working directory:
@echo off
rem If Maxima is not in your path, you need to set it.
rem Also, I'm making use of the 'head' command, you can
rem obtain the Win32 version easily with MSys.
set PATH=%PATH%;"C:Program FilesMaxima-5.24.0bin"
maxima --very-quiet -r %1 | head --lines=-1
I'm not in my Linux box right now, but a .sh file is similar.
Using Jake's code, I added a call to this script:
documentclass{article}
usepackage{pgf}
begin{document}
section{Problem}
pgfmathsetseed{1}
pgfmathtruncatemacrocoeffa{random(2,10)}
pgfmathtruncatemacrocoeffb{random(2,10)}
pgfmathtruncatemacrocoeffc{random(2,10)}
% call to the 'solve' script providing the coefficients.
% Maxima's 'solve' function will return x1 and x2, and the
% 'tex' function will convert the answer to TeX. The last
% line of the output will be removed (the 'head' command)
% because Maxima adds a 'false' line to the end of the file.
% Then we redirect the output to a file.
immediatewrite18{solve.cmd "tex(solve(coeffa*x^2 + (coeffb*x) + (coeffc)));" > solution1.tex}
Let (x_1) and (x_2) be the roots of (coeffa x^2 +coeffb x +coeffc=0). Find (x_1x_2).
section{Solution}
% read the solution
input solution1
end{document}
I tried to use Maxima's plot2d function together with the call, but I failed to fix a minor error with file extensions. I intended to create a png plot with gnuplot and include it. Sorry, it didn't work yet.
Don't forget to use the --shell-escape or --enable-write18 flag.
EDIT: It gave me a tremendous headache, but I guess I found a nice improvement. =)
I'm not used to Maxima, though I like to use it for simple calculations. If my code is somehow incorrect, please help me improve it. Thanks.
I edited the solve.cmd file in my working directory to look like this:
@echo off
rem If Maxima is not in your path, you need to set it.
rem Now, I don't use 'head' anymore, but 'grep'. The idea
rem behind this code is:
rem 1. generate the three coefficients,
rem 2. output both equation and solution to a file,
rem 3. get rid of the 'false' lines in the text (Maxima's fault)
rem (we do this thanks to 'grep' using an inverse flag)
rem 4. direct output to a file 'problem.txt'.
@echo off
set PATH=%PATH%;"C:Program FilesMaxima-5.24.0bin"
maxima --very-quiet -r "s: make_random_state(true)$ set_random_state(s)$ fullrand(low, high) := floor(random(1.0) * ( 1 + high - low)) + low$ a : fullrand(-10,10)$ s: b : fullrand(-10,10)$ c : fullrand(-10,10)$ tex(a*x^2 + b*x + c); tex(solve(a*x^2 + b*x + c));" | grep -v false > problem.txt
Now the tricky part, the LaTeX code:
documentclass{article}
immediatewrite18{solve.cmd}
newreadmyread
openinmyread=problem.txt
readmyread to theequation
readmyread to thesolution
begin{document}
section{Problem}
This is our equation:
theequation
section{Solution}
And this is our solution:
thesolution
end{document}
Let's check the code: first of all, we run solve.cmd, which outputs both equation and solution to a file named problem.txt. Then we create a newreadmyread and open this file openinmyread=problem.txt. Now we map the first line to theequation (which is the equation which we defined in our Maxima code) and the second line to thesolution (same logic). Then we call it when needed.
Again, probably some of my Maxima code is faulty. But I hope you get the idea behind this. In case of changing the equation or solution to a inline version, we may probably do some sed in it and replace the pattern. =)
@xport: glad you like it. I'm trying to improve the code as you suggested, but unfortunately I'm not that used with Maxima. EDIT: I hope to come up with something. ;-)
– Paulo Cereda
Jun 10 '11 at 18:55
@xport: hm did you use the--shell-escapeflag? Unfortunately, a single mistake (like a missing;) will break the call. I suffered a lot before getting it right.
– Paulo Cereda
Jun 10 '11 at 19:27
@xport: hm good, at least it worked! The missingheadcommand is part of a Win32 port of some useful Unix tools. I uploaded mine for you to use, put in your working directory. I hope it helps. PS: I'm on a fight with Maxima right now. =)
– Paulo Cereda
Jun 10 '11 at 19:47
@xport: Mine came from MSys/MinGW, but I believe it's a big payload unless you want to usegccand related tools. This project may sound more interesting.
– Paulo Cereda
Jun 10 '11 at 19:57
@xport: hm your code works for me, using MikTeX 2.9. I'm gonna try in another machine with TeXLive.
– Paulo Cereda
Jun 10 '11 at 20:15
|
show 10 more comments
This is a partial solution and probably it's not what you want. However, I'm posting it solely for record purposes, in case anyone would need it. =)
I'll use Maxima:
Maxima is a system for the manipulation of symbolic and numerical expressions, including differentiation, integration, Taylor series, Laplace transforms, ordinary differential equations, systems of linear equations, polynomials, and sets, lists, vectors, matrices, and tensors. Maxima yields high precision numeric results by using exact fractions, arbitrary precision integers, and variable precision floating point numbers. Maxima can plot functions and data in two and three dimensions.
First of all, I created a script file named solve.cmd in my working directory:
@echo off
rem If Maxima is not in your path, you need to set it.
rem Also, I'm making use of the 'head' command, you can
rem obtain the Win32 version easily with MSys.
set PATH=%PATH%;"C:Program FilesMaxima-5.24.0bin"
maxima --very-quiet -r %1 | head --lines=-1
I'm not in my Linux box right now, but a .sh file is similar.
Using Jake's code, I added a call to this script:
documentclass{article}
usepackage{pgf}
begin{document}
section{Problem}
pgfmathsetseed{1}
pgfmathtruncatemacrocoeffa{random(2,10)}
pgfmathtruncatemacrocoeffb{random(2,10)}
pgfmathtruncatemacrocoeffc{random(2,10)}
% call to the 'solve' script providing the coefficients.
% Maxima's 'solve' function will return x1 and x2, and the
% 'tex' function will convert the answer to TeX. The last
% line of the output will be removed (the 'head' command)
% because Maxima adds a 'false' line to the end of the file.
% Then we redirect the output to a file.
immediatewrite18{solve.cmd "tex(solve(coeffa*x^2 + (coeffb*x) + (coeffc)));" > solution1.tex}
Let (x_1) and (x_2) be the roots of (coeffa x^2 +coeffb x +coeffc=0). Find (x_1x_2).
section{Solution}
% read the solution
input solution1
end{document}
I tried to use Maxima's plot2d function together with the call, but I failed to fix a minor error with file extensions. I intended to create a png plot with gnuplot and include it. Sorry, it didn't work yet.
Don't forget to use the --shell-escape or --enable-write18 flag.
EDIT: It gave me a tremendous headache, but I guess I found a nice improvement. =)
I'm not used to Maxima, though I like to use it for simple calculations. If my code is somehow incorrect, please help me improve it. Thanks.
I edited the solve.cmd file in my working directory to look like this:
@echo off
rem If Maxima is not in your path, you need to set it.
rem Now, I don't use 'head' anymore, but 'grep'. The idea
rem behind this code is:
rem 1. generate the three coefficients,
rem 2. output both equation and solution to a file,
rem 3. get rid of the 'false' lines in the text (Maxima's fault)
rem (we do this thanks to 'grep' using an inverse flag)
rem 4. direct output to a file 'problem.txt'.
@echo off
set PATH=%PATH%;"C:Program FilesMaxima-5.24.0bin"
maxima --very-quiet -r "s: make_random_state(true)$ set_random_state(s)$ fullrand(low, high) := floor(random(1.0) * ( 1 + high - low)) + low$ a : fullrand(-10,10)$ s: b : fullrand(-10,10)$ c : fullrand(-10,10)$ tex(a*x^2 + b*x + c); tex(solve(a*x^2 + b*x + c));" | grep -v false > problem.txt
Now the tricky part, the LaTeX code:
documentclass{article}
immediatewrite18{solve.cmd}
newreadmyread
openinmyread=problem.txt
readmyread to theequation
readmyread to thesolution
begin{document}
section{Problem}
This is our equation:
theequation
section{Solution}
And this is our solution:
thesolution
end{document}
Let's check the code: first of all, we run solve.cmd, which outputs both equation and solution to a file named problem.txt. Then we create a newreadmyread and open this file openinmyread=problem.txt. Now we map the first line to theequation (which is the equation which we defined in our Maxima code) and the second line to thesolution (same logic). Then we call it when needed.
Again, probably some of my Maxima code is faulty. But I hope you get the idea behind this. In case of changing the equation or solution to a inline version, we may probably do some sed in it and replace the pattern. =)
This is a partial solution and probably it's not what you want. However, I'm posting it solely for record purposes, in case anyone would need it. =)
I'll use Maxima:
Maxima is a system for the manipulation of symbolic and numerical expressions, including differentiation, integration, Taylor series, Laplace transforms, ordinary differential equations, systems of linear equations, polynomials, and sets, lists, vectors, matrices, and tensors. Maxima yields high precision numeric results by using exact fractions, arbitrary precision integers, and variable precision floating point numbers. Maxima can plot functions and data in two and three dimensions.
First of all, I created a script file named solve.cmd in my working directory:
@echo off
rem If Maxima is not in your path, you need to set it.
rem Also, I'm making use of the 'head' command, you can
rem obtain the Win32 version easily with MSys.
set PATH=%PATH%;"C:Program FilesMaxima-5.24.0bin"
maxima --very-quiet -r %1 | head --lines=-1
I'm not in my Linux box right now, but a .sh file is similar.
Using Jake's code, I added a call to this script:
documentclass{article}
usepackage{pgf}
begin{document}
section{Problem}
pgfmathsetseed{1}
pgfmathtruncatemacrocoeffa{random(2,10)}
pgfmathtruncatemacrocoeffb{random(2,10)}
pgfmathtruncatemacrocoeffc{random(2,10)}
% call to the 'solve' script providing the coefficients.
% Maxima's 'solve' function will return x1 and x2, and the
% 'tex' function will convert the answer to TeX. The last
% line of the output will be removed (the 'head' command)
% because Maxima adds a 'false' line to the end of the file.
% Then we redirect the output to a file.
immediatewrite18{solve.cmd "tex(solve(coeffa*x^2 + (coeffb*x) + (coeffc)));" > solution1.tex}
Let (x_1) and (x_2) be the roots of (coeffa x^2 +coeffb x +coeffc=0). Find (x_1x_2).
section{Solution}
% read the solution
input solution1
end{document}
I tried to use Maxima's plot2d function together with the call, but I failed to fix a minor error with file extensions. I intended to create a png plot with gnuplot and include it. Sorry, it didn't work yet.
Don't forget to use the --shell-escape or --enable-write18 flag.
EDIT: It gave me a tremendous headache, but I guess I found a nice improvement. =)
I'm not used to Maxima, though I like to use it for simple calculations. If my code is somehow incorrect, please help me improve it. Thanks.
I edited the solve.cmd file in my working directory to look like this:
@echo off
rem If Maxima is not in your path, you need to set it.
rem Now, I don't use 'head' anymore, but 'grep'. The idea
rem behind this code is:
rem 1. generate the three coefficients,
rem 2. output both equation and solution to a file,
rem 3. get rid of the 'false' lines in the text (Maxima's fault)
rem (we do this thanks to 'grep' using an inverse flag)
rem 4. direct output to a file 'problem.txt'.
@echo off
set PATH=%PATH%;"C:Program FilesMaxima-5.24.0bin"
maxima --very-quiet -r "s: make_random_state(true)$ set_random_state(s)$ fullrand(low, high) := floor(random(1.0) * ( 1 + high - low)) + low$ a : fullrand(-10,10)$ s: b : fullrand(-10,10)$ c : fullrand(-10,10)$ tex(a*x^2 + b*x + c); tex(solve(a*x^2 + b*x + c));" | grep -v false > problem.txt
Now the tricky part, the LaTeX code:
documentclass{article}
immediatewrite18{solve.cmd}
newreadmyread
openinmyread=problem.txt
readmyread to theequation
readmyread to thesolution
begin{document}
section{Problem}
This is our equation:
theequation
section{Solution}
And this is our solution:
thesolution
end{document}
Let's check the code: first of all, we run solve.cmd, which outputs both equation and solution to a file named problem.txt. Then we create a newreadmyread and open this file openinmyread=problem.txt. Now we map the first line to theequation (which is the equation which we defined in our Maxima code) and the second line to thesolution (same logic). Then we call it when needed.
Again, probably some of my Maxima code is faulty. But I hope you get the idea behind this. In case of changing the equation or solution to a inline version, we may probably do some sed in it and replace the pattern. =)
edited Jun 10 '11 at 23:50
xport
22.1k30139262
22.1k30139262
answered Jun 10 '11 at 18:21
Paulo CeredaPaulo Cereda
34k8128211
34k8128211
@xport: glad you like it. I'm trying to improve the code as you suggested, but unfortunately I'm not that used with Maxima. EDIT: I hope to come up with something. ;-)
– Paulo Cereda
Jun 10 '11 at 18:55
@xport: hm did you use the--shell-escapeflag? Unfortunately, a single mistake (like a missing;) will break the call. I suffered a lot before getting it right.
– Paulo Cereda
Jun 10 '11 at 19:27
@xport: hm good, at least it worked! The missingheadcommand is part of a Win32 port of some useful Unix tools. I uploaded mine for you to use, put in your working directory. I hope it helps. PS: I'm on a fight with Maxima right now. =)
– Paulo Cereda
Jun 10 '11 at 19:47
@xport: Mine came from MSys/MinGW, but I believe it's a big payload unless you want to usegccand related tools. This project may sound more interesting.
– Paulo Cereda
Jun 10 '11 at 19:57
@xport: hm your code works for me, using MikTeX 2.9. I'm gonna try in another machine with TeXLive.
– Paulo Cereda
Jun 10 '11 at 20:15
|
show 10 more comments
@xport: glad you like it. I'm trying to improve the code as you suggested, but unfortunately I'm not that used with Maxima. EDIT: I hope to come up with something. ;-)
– Paulo Cereda
Jun 10 '11 at 18:55
@xport: hm did you use the--shell-escapeflag? Unfortunately, a single mistake (like a missing;) will break the call. I suffered a lot before getting it right.
– Paulo Cereda
Jun 10 '11 at 19:27
@xport: hm good, at least it worked! The missingheadcommand is part of a Win32 port of some useful Unix tools. I uploaded mine for you to use, put in your working directory. I hope it helps. PS: I'm on a fight with Maxima right now. =)
– Paulo Cereda
Jun 10 '11 at 19:47
@xport: Mine came from MSys/MinGW, but I believe it's a big payload unless you want to usegccand related tools. This project may sound more interesting.
– Paulo Cereda
Jun 10 '11 at 19:57
@xport: hm your code works for me, using MikTeX 2.9. I'm gonna try in another machine with TeXLive.
– Paulo Cereda
Jun 10 '11 at 20:15
@xport: glad you like it. I'm trying to improve the code as you suggested, but unfortunately I'm not that used with Maxima. EDIT: I hope to come up with something. ;-)
– Paulo Cereda
Jun 10 '11 at 18:55
@xport: glad you like it. I'm trying to improve the code as you suggested, but unfortunately I'm not that used with Maxima. EDIT: I hope to come up with something. ;-)
– Paulo Cereda
Jun 10 '11 at 18:55
@xport: hm did you use the
--shell-escape flag? Unfortunately, a single mistake (like a missing ;) will break the call. I suffered a lot before getting it right.– Paulo Cereda
Jun 10 '11 at 19:27
@xport: hm did you use the
--shell-escape flag? Unfortunately, a single mistake (like a missing ;) will break the call. I suffered a lot before getting it right.– Paulo Cereda
Jun 10 '11 at 19:27
@xport: hm good, at least it worked! The missing
head command is part of a Win32 port of some useful Unix tools. I uploaded mine for you to use, put in your working directory. I hope it helps. PS: I'm on a fight with Maxima right now. =)– Paulo Cereda
Jun 10 '11 at 19:47
@xport: hm good, at least it worked! The missing
head command is part of a Win32 port of some useful Unix tools. I uploaded mine for you to use, put in your working directory. I hope it helps. PS: I'm on a fight with Maxima right now. =)– Paulo Cereda
Jun 10 '11 at 19:47
@xport: Mine came from MSys/MinGW, but I believe it's a big payload unless you want to use
gcc and related tools. This project may sound more interesting.– Paulo Cereda
Jun 10 '11 at 19:57
@xport: Mine came from MSys/MinGW, but I believe it's a big payload unless you want to use
gcc and related tools. This project may sound more interesting.– Paulo Cereda
Jun 10 '11 at 19:57
@xport: hm your code works for me, using MikTeX 2.9. I'm gonna try in another machine with TeXLive.
– Paulo Cereda
Jun 10 '11 at 20:15
@xport: hm your code works for me, using MikTeX 2.9. I'm gonna try in another machine with TeXLive.
– Paulo Cereda
Jun 10 '11 at 20:15
|
show 10 more comments
I know I'm late to the party here, but I would use Sage and SageTeX:
documentclass{article}
usepackage{sagetex}
begin{document}
section{Problem}
begin{sagesilent}
a = ZZ.random_element(-10,10)
while a == 0:
a = ZZ.random_element(-10,10)
b = ZZ.random_element(-10,10)
c = ZZ.random_element(-10,10)
poly = a*x^2 + b*x + c
end{sagesilent}
Let (x_1) and (x_2) be the roots of (sage{poly}=0).
Find (x_1x_2).
section{Solution}
[
x_1x_2=sage{c/a}.
]
sageplot{plot(poly) }
end{document}
The output looks like this:

1
A little late but worth the wait! Your solution inspired me to solve the problems I had earlier (with the system finding sagetex.sty) and after spending a couple of hours messing around, it seems clear to me that sagetex package is the "must have" package for those who might use a program like Sage.
– DJP
Jul 18 '11 at 13:59
add a comment |
I know I'm late to the party here, but I would use Sage and SageTeX:
documentclass{article}
usepackage{sagetex}
begin{document}
section{Problem}
begin{sagesilent}
a = ZZ.random_element(-10,10)
while a == 0:
a = ZZ.random_element(-10,10)
b = ZZ.random_element(-10,10)
c = ZZ.random_element(-10,10)
poly = a*x^2 + b*x + c
end{sagesilent}
Let (x_1) and (x_2) be the roots of (sage{poly}=0).
Find (x_1x_2).
section{Solution}
[
x_1x_2=sage{c/a}.
]
sageplot{plot(poly) }
end{document}
The output looks like this:

1
A little late but worth the wait! Your solution inspired me to solve the problems I had earlier (with the system finding sagetex.sty) and after spending a couple of hours messing around, it seems clear to me that sagetex package is the "must have" package for those who might use a program like Sage.
– DJP
Jul 18 '11 at 13:59
add a comment |
I know I'm late to the party here, but I would use Sage and SageTeX:
documentclass{article}
usepackage{sagetex}
begin{document}
section{Problem}
begin{sagesilent}
a = ZZ.random_element(-10,10)
while a == 0:
a = ZZ.random_element(-10,10)
b = ZZ.random_element(-10,10)
c = ZZ.random_element(-10,10)
poly = a*x^2 + b*x + c
end{sagesilent}
Let (x_1) and (x_2) be the roots of (sage{poly}=0).
Find (x_1x_2).
section{Solution}
[
x_1x_2=sage{c/a}.
]
sageplot{plot(poly) }
end{document}
The output looks like this:

I know I'm late to the party here, but I would use Sage and SageTeX:
documentclass{article}
usepackage{sagetex}
begin{document}
section{Problem}
begin{sagesilent}
a = ZZ.random_element(-10,10)
while a == 0:
a = ZZ.random_element(-10,10)
b = ZZ.random_element(-10,10)
c = ZZ.random_element(-10,10)
poly = a*x^2 + b*x + c
end{sagesilent}
Let (x_1) and (x_2) be the roots of (sage{poly}=0).
Find (x_1x_2).
section{Solution}
[
x_1x_2=sage{c/a}.
]
sageplot{plot(poly) }
end{document}
The output looks like this:

answered Jul 18 '11 at 0:08
John PalmieriJohn Palmieri
1,041815
1,041815
1
A little late but worth the wait! Your solution inspired me to solve the problems I had earlier (with the system finding sagetex.sty) and after spending a couple of hours messing around, it seems clear to me that sagetex package is the "must have" package for those who might use a program like Sage.
– DJP
Jul 18 '11 at 13:59
add a comment |
1
A little late but worth the wait! Your solution inspired me to solve the problems I had earlier (with the system finding sagetex.sty) and after spending a couple of hours messing around, it seems clear to me that sagetex package is the "must have" package for those who might use a program like Sage.
– DJP
Jul 18 '11 at 13:59
1
1
A little late but worth the wait! Your solution inspired me to solve the problems I had earlier (with the system finding sagetex.sty) and after spending a couple of hours messing around, it seems clear to me that sagetex package is the "must have" package for those who might use a program like Sage.
– DJP
Jul 18 '11 at 13:59
A little late but worth the wait! Your solution inspired me to solve the problems I had earlier (with the system finding sagetex.sty) and after spending a couple of hours messing around, it seems clear to me that sagetex package is the "must have" package for those who might use a program like Sage.
– DJP
Jul 18 '11 at 13:59
add a comment |
The lcg package contains macros to generate (pseudo) random numbers. A little example (the plot is also generated but using TikZ):
documentclass{article}
usepackage[counter=coefa,first=-8, last=12]{lcg}
usepackage{pgfplots}
begin{document}
randarabic{coefa}
letCoefathecoefa
chgrand[counter=coefb]
randarabic{coefb}
letCoefbthecoefb
chgrand[counter=coefc]
randarabic{coefc}
letCoefcthecoefc
section{Problem}
Let (x_1) and (x_2) be the roots of ( Coefa x^2 +Coefb x + Coefc =0). Find (x_1x_2).
section{Solution}
[
x_1x_2=frac{Coefc}{Coefa}
]
begin{tikzpicture}
begin{axis}[
xlabel=$x$,
ylabel={$f(x) = Coefa x^2 + Coefb x + Coefc $}
]
addplot[color=orange,thick] {Coefa*x^2 + Coefb* x + Coefc};
end{axis}
end{tikzpicture}
end{document}

Of course, when compiling my example you'll get different values for the corfficients, so a different equation and plot will be obtained.
@xport: I'll think about the simplification problem. I've updated my answer adding the plot of the quadratic function (but usingTikZ).
– Gonzalo Medina
Jun 10 '11 at 16:39
Beautiful answer.
– ℝaphink
Jun 10 '11 at 17:32
add a comment |
The lcg package contains macros to generate (pseudo) random numbers. A little example (the plot is also generated but using TikZ):
documentclass{article}
usepackage[counter=coefa,first=-8, last=12]{lcg}
usepackage{pgfplots}
begin{document}
randarabic{coefa}
letCoefathecoefa
chgrand[counter=coefb]
randarabic{coefb}
letCoefbthecoefb
chgrand[counter=coefc]
randarabic{coefc}
letCoefcthecoefc
section{Problem}
Let (x_1) and (x_2) be the roots of ( Coefa x^2 +Coefb x + Coefc =0). Find (x_1x_2).
section{Solution}
[
x_1x_2=frac{Coefc}{Coefa}
]
begin{tikzpicture}
begin{axis}[
xlabel=$x$,
ylabel={$f(x) = Coefa x^2 + Coefb x + Coefc $}
]
addplot[color=orange,thick] {Coefa*x^2 + Coefb* x + Coefc};
end{axis}
end{tikzpicture}
end{document}

Of course, when compiling my example you'll get different values for the corfficients, so a different equation and plot will be obtained.
@xport: I'll think about the simplification problem. I've updated my answer adding the plot of the quadratic function (but usingTikZ).
– Gonzalo Medina
Jun 10 '11 at 16:39
Beautiful answer.
– ℝaphink
Jun 10 '11 at 17:32
add a comment |
The lcg package contains macros to generate (pseudo) random numbers. A little example (the plot is also generated but using TikZ):
documentclass{article}
usepackage[counter=coefa,first=-8, last=12]{lcg}
usepackage{pgfplots}
begin{document}
randarabic{coefa}
letCoefathecoefa
chgrand[counter=coefb]
randarabic{coefb}
letCoefbthecoefb
chgrand[counter=coefc]
randarabic{coefc}
letCoefcthecoefc
section{Problem}
Let (x_1) and (x_2) be the roots of ( Coefa x^2 +Coefb x + Coefc =0). Find (x_1x_2).
section{Solution}
[
x_1x_2=frac{Coefc}{Coefa}
]
begin{tikzpicture}
begin{axis}[
xlabel=$x$,
ylabel={$f(x) = Coefa x^2 + Coefb x + Coefc $}
]
addplot[color=orange,thick] {Coefa*x^2 + Coefb* x + Coefc};
end{axis}
end{tikzpicture}
end{document}

Of course, when compiling my example you'll get different values for the corfficients, so a different equation and plot will be obtained.
The lcg package contains macros to generate (pseudo) random numbers. A little example (the plot is also generated but using TikZ):
documentclass{article}
usepackage[counter=coefa,first=-8, last=12]{lcg}
usepackage{pgfplots}
begin{document}
randarabic{coefa}
letCoefathecoefa
chgrand[counter=coefb]
randarabic{coefb}
letCoefbthecoefb
chgrand[counter=coefc]
randarabic{coefc}
letCoefcthecoefc
section{Problem}
Let (x_1) and (x_2) be the roots of ( Coefa x^2 +Coefb x + Coefc =0). Find (x_1x_2).
section{Solution}
[
x_1x_2=frac{Coefc}{Coefa}
]
begin{tikzpicture}
begin{axis}[
xlabel=$x$,
ylabel={$f(x) = Coefa x^2 + Coefb x + Coefc $}
]
addplot[color=orange,thick] {Coefa*x^2 + Coefb* x + Coefc};
end{axis}
end{tikzpicture}
end{document}

Of course, when compiling my example you'll get different values for the corfficients, so a different equation and plot will be obtained.
edited Jul 17 '11 at 23:26
Martin Scharrer♦
202k47647823
202k47647823
answered Jun 10 '11 at 15:28
Gonzalo MedinaGonzalo Medina
400k4113071575
400k4113071575
@xport: I'll think about the simplification problem. I've updated my answer adding the plot of the quadratic function (but usingTikZ).
– Gonzalo Medina
Jun 10 '11 at 16:39
Beautiful answer.
– ℝaphink
Jun 10 '11 at 17:32
add a comment |
@xport: I'll think about the simplification problem. I've updated my answer adding the plot of the quadratic function (but usingTikZ).
– Gonzalo Medina
Jun 10 '11 at 16:39
Beautiful answer.
– ℝaphink
Jun 10 '11 at 17:32
@xport: I'll think about the simplification problem. I've updated my answer adding the plot of the quadratic function (but using
TikZ).– Gonzalo Medina
Jun 10 '11 at 16:39
@xport: I'll think about the simplification problem. I've updated my answer adding the plot of the quadratic function (but using
TikZ).– Gonzalo Medina
Jun 10 '11 at 16:39
Beautiful answer.
– ℝaphink
Jun 10 '11 at 17:32
Beautiful answer.
– ℝaphink
Jun 10 '11 at 17:32
add a comment |
To simplify the fractions, you can use Euclid's Method, using the minimal below with
reduceFrac{27}{81}
$frac{rfNumer}{rfDenom}$
will give you 1/3. To simplify to a round number is more difficult, as you need to check if there is a remainder left over, so you will need a decimal to fraction conversion routine. See How to break out of a loop at the end of the code, you will find how to use the macros to convert for example 0.375 to the fractional approximation 768/2048. No guarantees for its accuracy, needs a bit of refinement.
documentclass{article}
begin{document}
makeatletter
% Use Euclid's Algorithm to find the greatest
% common divisor of two integers.
defgcd#1#2{{% #1 = a, #2 = b
ifnum#2=0 edefnext{#1}else
@tempcnta=#1 @tempcntb=#2 divide@tempcnta by@tempcntb
multiply@tempcnta by@tempcntb % q*b
@tempcntb=#1
advance@tempcntb by-@tempcnta % remainder in @tempcntb
ifnum@tempcntb=0
@tempcnta=#2
ifnum@tempcnta < 0 @tempcnta=-@tempcntafi
xdefgcd@next{noexpand%
defnoexpandthegcd{the@tempcnta}}%
else
xdefgcd@next{noexpandgcd{#2}{the@tempcntb}}%
fi
fi}gcd@next
}
newcommandreduceFrac[2]
{%
gcd{#1}{#2}{@tempcnta=#1 divide@tempcnta bythegcd
@tempcntb=#2 divide@tempcntb bythegcd
ifnum@tempcntb<0relax
@tempcntb=-@tempcntb
@tempcnta=-@tempcnta
fi
xdefrfNumer{the@tempcnta}
xdefrfDenom{the@tempcntb}}%
}
reduceFrac{27}{81}
$frac{rfNumer}{rfDenom}$
makeatother
end{document}
@ Yiannisgcdis now in pgf 2.1 CVS and reduceFrac is calledtkzReducFracintkz-tools-arith.tex. Now it's a part of TL2011.gcdis also intkz-tools-arith.texbut I made a test to use pgfmath if the version is the CVS one . See my answer
– Alain Matthes
Jun 10 '11 at 17:38
@Altermundus that is great, I can now delete all these from my hardisk!
– Yiannis Lazarides
Jun 10 '11 at 17:41
@ Yiannis Yes great but I'm afraid ... I hope there are not too many errors in all the packages
– Alain Matthes
Jun 10 '11 at 17:55
@Altermundus See if you can also make use of tex.stackexchange.com/questions/12481/…
– Yiannis Lazarides
Jun 10 '11 at 18:03
@Yiannis I don't see this question. It's interesting
– Alain Matthes
Jun 10 '11 at 18:07
add a comment |
To simplify the fractions, you can use Euclid's Method, using the minimal below with
reduceFrac{27}{81}
$frac{rfNumer}{rfDenom}$
will give you 1/3. To simplify to a round number is more difficult, as you need to check if there is a remainder left over, so you will need a decimal to fraction conversion routine. See How to break out of a loop at the end of the code, you will find how to use the macros to convert for example 0.375 to the fractional approximation 768/2048. No guarantees for its accuracy, needs a bit of refinement.
documentclass{article}
begin{document}
makeatletter
% Use Euclid's Algorithm to find the greatest
% common divisor of two integers.
defgcd#1#2{{% #1 = a, #2 = b
ifnum#2=0 edefnext{#1}else
@tempcnta=#1 @tempcntb=#2 divide@tempcnta by@tempcntb
multiply@tempcnta by@tempcntb % q*b
@tempcntb=#1
advance@tempcntb by-@tempcnta % remainder in @tempcntb
ifnum@tempcntb=0
@tempcnta=#2
ifnum@tempcnta < 0 @tempcnta=-@tempcntafi
xdefgcd@next{noexpand%
defnoexpandthegcd{the@tempcnta}}%
else
xdefgcd@next{noexpandgcd{#2}{the@tempcntb}}%
fi
fi}gcd@next
}
newcommandreduceFrac[2]
{%
gcd{#1}{#2}{@tempcnta=#1 divide@tempcnta bythegcd
@tempcntb=#2 divide@tempcntb bythegcd
ifnum@tempcntb<0relax
@tempcntb=-@tempcntb
@tempcnta=-@tempcnta
fi
xdefrfNumer{the@tempcnta}
xdefrfDenom{the@tempcntb}}%
}
reduceFrac{27}{81}
$frac{rfNumer}{rfDenom}$
makeatother
end{document}
@ Yiannisgcdis now in pgf 2.1 CVS and reduceFrac is calledtkzReducFracintkz-tools-arith.tex. Now it's a part of TL2011.gcdis also intkz-tools-arith.texbut I made a test to use pgfmath if the version is the CVS one . See my answer
– Alain Matthes
Jun 10 '11 at 17:38
@Altermundus that is great, I can now delete all these from my hardisk!
– Yiannis Lazarides
Jun 10 '11 at 17:41
@ Yiannis Yes great but I'm afraid ... I hope there are not too many errors in all the packages
– Alain Matthes
Jun 10 '11 at 17:55
@Altermundus See if you can also make use of tex.stackexchange.com/questions/12481/…
– Yiannis Lazarides
Jun 10 '11 at 18:03
@Yiannis I don't see this question. It's interesting
– Alain Matthes
Jun 10 '11 at 18:07
add a comment |
To simplify the fractions, you can use Euclid's Method, using the minimal below with
reduceFrac{27}{81}
$frac{rfNumer}{rfDenom}$
will give you 1/3. To simplify to a round number is more difficult, as you need to check if there is a remainder left over, so you will need a decimal to fraction conversion routine. See How to break out of a loop at the end of the code, you will find how to use the macros to convert for example 0.375 to the fractional approximation 768/2048. No guarantees for its accuracy, needs a bit of refinement.
documentclass{article}
begin{document}
makeatletter
% Use Euclid's Algorithm to find the greatest
% common divisor of two integers.
defgcd#1#2{{% #1 = a, #2 = b
ifnum#2=0 edefnext{#1}else
@tempcnta=#1 @tempcntb=#2 divide@tempcnta by@tempcntb
multiply@tempcnta by@tempcntb % q*b
@tempcntb=#1
advance@tempcntb by-@tempcnta % remainder in @tempcntb
ifnum@tempcntb=0
@tempcnta=#2
ifnum@tempcnta < 0 @tempcnta=-@tempcntafi
xdefgcd@next{noexpand%
defnoexpandthegcd{the@tempcnta}}%
else
xdefgcd@next{noexpandgcd{#2}{the@tempcntb}}%
fi
fi}gcd@next
}
newcommandreduceFrac[2]
{%
gcd{#1}{#2}{@tempcnta=#1 divide@tempcnta bythegcd
@tempcntb=#2 divide@tempcntb bythegcd
ifnum@tempcntb<0relax
@tempcntb=-@tempcntb
@tempcnta=-@tempcnta
fi
xdefrfNumer{the@tempcnta}
xdefrfDenom{the@tempcntb}}%
}
reduceFrac{27}{81}
$frac{rfNumer}{rfDenom}$
makeatother
end{document}
To simplify the fractions, you can use Euclid's Method, using the minimal below with
reduceFrac{27}{81}
$frac{rfNumer}{rfDenom}$
will give you 1/3. To simplify to a round number is more difficult, as you need to check if there is a remainder left over, so you will need a decimal to fraction conversion routine. See How to break out of a loop at the end of the code, you will find how to use the macros to convert for example 0.375 to the fractional approximation 768/2048. No guarantees for its accuracy, needs a bit of refinement.
documentclass{article}
begin{document}
makeatletter
% Use Euclid's Algorithm to find the greatest
% common divisor of two integers.
defgcd#1#2{{% #1 = a, #2 = b
ifnum#2=0 edefnext{#1}else
@tempcnta=#1 @tempcntb=#2 divide@tempcnta by@tempcntb
multiply@tempcnta by@tempcntb % q*b
@tempcntb=#1
advance@tempcntb by-@tempcnta % remainder in @tempcntb
ifnum@tempcntb=0
@tempcnta=#2
ifnum@tempcnta < 0 @tempcnta=-@tempcntafi
xdefgcd@next{noexpand%
defnoexpandthegcd{the@tempcnta}}%
else
xdefgcd@next{noexpandgcd{#2}{the@tempcntb}}%
fi
fi}gcd@next
}
newcommandreduceFrac[2]
{%
gcd{#1}{#2}{@tempcnta=#1 divide@tempcnta bythegcd
@tempcntb=#2 divide@tempcntb bythegcd
ifnum@tempcntb<0relax
@tempcntb=-@tempcntb
@tempcnta=-@tempcnta
fi
xdefrfNumer{the@tempcnta}
xdefrfDenom{the@tempcntb}}%
}
reduceFrac{27}{81}
$frac{rfNumer}{rfDenom}$
makeatother
end{document}
edited Apr 13 '17 at 12:35
Community♦
1
1
answered Jun 10 '11 at 17:29
Yiannis LazaridesYiannis Lazarides
92.6k20234514
92.6k20234514
@ Yiannisgcdis now in pgf 2.1 CVS and reduceFrac is calledtkzReducFracintkz-tools-arith.tex. Now it's a part of TL2011.gcdis also intkz-tools-arith.texbut I made a test to use pgfmath if the version is the CVS one . See my answer
– Alain Matthes
Jun 10 '11 at 17:38
@Altermundus that is great, I can now delete all these from my hardisk!
– Yiannis Lazarides
Jun 10 '11 at 17:41
@ Yiannis Yes great but I'm afraid ... I hope there are not too many errors in all the packages
– Alain Matthes
Jun 10 '11 at 17:55
@Altermundus See if you can also make use of tex.stackexchange.com/questions/12481/…
– Yiannis Lazarides
Jun 10 '11 at 18:03
@Yiannis I don't see this question. It's interesting
– Alain Matthes
Jun 10 '11 at 18:07
add a comment |
@ Yiannisgcdis now in pgf 2.1 CVS and reduceFrac is calledtkzReducFracintkz-tools-arith.tex. Now it's a part of TL2011.gcdis also intkz-tools-arith.texbut I made a test to use pgfmath if the version is the CVS one . See my answer
– Alain Matthes
Jun 10 '11 at 17:38
@Altermundus that is great, I can now delete all these from my hardisk!
– Yiannis Lazarides
Jun 10 '11 at 17:41
@ Yiannis Yes great but I'm afraid ... I hope there are not too many errors in all the packages
– Alain Matthes
Jun 10 '11 at 17:55
@Altermundus See if you can also make use of tex.stackexchange.com/questions/12481/…
– Yiannis Lazarides
Jun 10 '11 at 18:03
@Yiannis I don't see this question. It's interesting
– Alain Matthes
Jun 10 '11 at 18:07
@ Yiannis
gcd is now in pgf 2.1 CVS and reduceFrac is called tkzReducFracin tkz-tools-arith.tex. Now it's a part of TL2011. gcdis also in tkz-tools-arith.tex but I made a test to use pgfmath if the version is the CVS one . See my answer– Alain Matthes
Jun 10 '11 at 17:38
@ Yiannis
gcd is now in pgf 2.1 CVS and reduceFrac is called tkzReducFracin tkz-tools-arith.tex. Now it's a part of TL2011. gcdis also in tkz-tools-arith.tex but I made a test to use pgfmath if the version is the CVS one . See my answer– Alain Matthes
Jun 10 '11 at 17:38
@Altermundus that is great, I can now delete all these from my hardisk!
– Yiannis Lazarides
Jun 10 '11 at 17:41
@Altermundus that is great, I can now delete all these from my hardisk!
– Yiannis Lazarides
Jun 10 '11 at 17:41
@ Yiannis Yes great but I'm afraid ... I hope there are not too many errors in all the packages
– Alain Matthes
Jun 10 '11 at 17:55
@ Yiannis Yes great but I'm afraid ... I hope there are not too many errors in all the packages
– Alain Matthes
Jun 10 '11 at 17:55
@Altermundus See if you can also make use of tex.stackexchange.com/questions/12481/…
– Yiannis Lazarides
Jun 10 '11 at 18:03
@Altermundus See if you can also make use of tex.stackexchange.com/questions/12481/…
– Yiannis Lazarides
Jun 10 '11 at 18:03
@Yiannis I don't see this question. It's interesting
– Alain Matthes
Jun 10 '11 at 18:07
@Yiannis I don't see this question. It's interesting
– Alain Matthes
Jun 10 '11 at 18:07
add a comment |
A solution with Lua that handles coefficients that are smaller than 2 correctly.
% Compile with lualatex.
documentclass{article}
usepackage{luacode}
begin{luacode*}
function gcd (a,b)
if b == 0 then
return a
end
return gcd(b, a % b)
end
function makeproblem ()
local a,b,c
a = math.random (-10,10)
if a == 0 then a = 1 end
b = math.random (-10,10)
c = math.random (-10,10)
-- Pretty-print the equation.
if a == - 1 then
tex.sprint('-')
elseif a ~= 1 then
tex.sprint(a)
end
tex.sprint( 'x^2' )
if b ~= 0 then
if b > 0 then tex.sprint('+') end
if b ~= 1 and b ~= -1 then tex.sprint(b) end
if b == -1 then tex.sprint('-') end
tex.sprint('x')
end
if c ~= 0 then
if c > 0 then tex.sprint('+') end
tex.sprint(c)
end
tex.sprint('=0')
-- Simplify the solution
local d
d = gcd(a,c)
a = a/d
c = c/d
local sign = 1
if a < 0 then
sign = -1
a = -a
end
if c < 0 then
sign = -sign
c = -c
end
-- Store the solution in solution.
tex.sprint('\gdef\solution{')
if sign < 0 then tex.sprint('-') end
if a == 1 or c == 0 then
tex.sprint(c)
else
tex.sprint('\frac{' .. c ..'}{'.. a ..'}')
end
tex.sprint('}')
end
end{luacode*}
newcommandmakeproblem{directlua{makeproblem()}}
begin{document}
section{Problem}
Let (x_1) and (x_2) be the roots of (makeproblem). Find (x_1x_2).
section{Solution}
[
x_1x_2=solution
]
end{document}

+1 It is also good. However, it will become more complicated for creating more complicated math problems.
– xport
Jun 11 '11 at 5:24
I've taken the liberty of adding a bit of meta code to pretty-print the Lua code chunk. Feel free to revert.
– Mico
18 hours ago
add a comment |
A solution with Lua that handles coefficients that are smaller than 2 correctly.
% Compile with lualatex.
documentclass{article}
usepackage{luacode}
begin{luacode*}
function gcd (a,b)
if b == 0 then
return a
end
return gcd(b, a % b)
end
function makeproblem ()
local a,b,c
a = math.random (-10,10)
if a == 0 then a = 1 end
b = math.random (-10,10)
c = math.random (-10,10)
-- Pretty-print the equation.
if a == - 1 then
tex.sprint('-')
elseif a ~= 1 then
tex.sprint(a)
end
tex.sprint( 'x^2' )
if b ~= 0 then
if b > 0 then tex.sprint('+') end
if b ~= 1 and b ~= -1 then tex.sprint(b) end
if b == -1 then tex.sprint('-') end
tex.sprint('x')
end
if c ~= 0 then
if c > 0 then tex.sprint('+') end
tex.sprint(c)
end
tex.sprint('=0')
-- Simplify the solution
local d
d = gcd(a,c)
a = a/d
c = c/d
local sign = 1
if a < 0 then
sign = -1
a = -a
end
if c < 0 then
sign = -sign
c = -c
end
-- Store the solution in solution.
tex.sprint('\gdef\solution{')
if sign < 0 then tex.sprint('-') end
if a == 1 or c == 0 then
tex.sprint(c)
else
tex.sprint('\frac{' .. c ..'}{'.. a ..'}')
end
tex.sprint('}')
end
end{luacode*}
newcommandmakeproblem{directlua{makeproblem()}}
begin{document}
section{Problem}
Let (x_1) and (x_2) be the roots of (makeproblem). Find (x_1x_2).
section{Solution}
[
x_1x_2=solution
]
end{document}

+1 It is also good. However, it will become more complicated for creating more complicated math problems.
– xport
Jun 11 '11 at 5:24
I've taken the liberty of adding a bit of meta code to pretty-print the Lua code chunk. Feel free to revert.
– Mico
18 hours ago
add a comment |
A solution with Lua that handles coefficients that are smaller than 2 correctly.
% Compile with lualatex.
documentclass{article}
usepackage{luacode}
begin{luacode*}
function gcd (a,b)
if b == 0 then
return a
end
return gcd(b, a % b)
end
function makeproblem ()
local a,b,c
a = math.random (-10,10)
if a == 0 then a = 1 end
b = math.random (-10,10)
c = math.random (-10,10)
-- Pretty-print the equation.
if a == - 1 then
tex.sprint('-')
elseif a ~= 1 then
tex.sprint(a)
end
tex.sprint( 'x^2' )
if b ~= 0 then
if b > 0 then tex.sprint('+') end
if b ~= 1 and b ~= -1 then tex.sprint(b) end
if b == -1 then tex.sprint('-') end
tex.sprint('x')
end
if c ~= 0 then
if c > 0 then tex.sprint('+') end
tex.sprint(c)
end
tex.sprint('=0')
-- Simplify the solution
local d
d = gcd(a,c)
a = a/d
c = c/d
local sign = 1
if a < 0 then
sign = -1
a = -a
end
if c < 0 then
sign = -sign
c = -c
end
-- Store the solution in solution.
tex.sprint('\gdef\solution{')
if sign < 0 then tex.sprint('-') end
if a == 1 or c == 0 then
tex.sprint(c)
else
tex.sprint('\frac{' .. c ..'}{'.. a ..'}')
end
tex.sprint('}')
end
end{luacode*}
newcommandmakeproblem{directlua{makeproblem()}}
begin{document}
section{Problem}
Let (x_1) and (x_2) be the roots of (makeproblem). Find (x_1x_2).
section{Solution}
[
x_1x_2=solution
]
end{document}

A solution with Lua that handles coefficients that are smaller than 2 correctly.
% Compile with lualatex.
documentclass{article}
usepackage{luacode}
begin{luacode*}
function gcd (a,b)
if b == 0 then
return a
end
return gcd(b, a % b)
end
function makeproblem ()
local a,b,c
a = math.random (-10,10)
if a == 0 then a = 1 end
b = math.random (-10,10)
c = math.random (-10,10)
-- Pretty-print the equation.
if a == - 1 then
tex.sprint('-')
elseif a ~= 1 then
tex.sprint(a)
end
tex.sprint( 'x^2' )
if b ~= 0 then
if b > 0 then tex.sprint('+') end
if b ~= 1 and b ~= -1 then tex.sprint(b) end
if b == -1 then tex.sprint('-') end
tex.sprint('x')
end
if c ~= 0 then
if c > 0 then tex.sprint('+') end
tex.sprint(c)
end
tex.sprint('=0')
-- Simplify the solution
local d
d = gcd(a,c)
a = a/d
c = c/d
local sign = 1
if a < 0 then
sign = -1
a = -a
end
if c < 0 then
sign = -sign
c = -c
end
-- Store the solution in solution.
tex.sprint('\gdef\solution{')
if sign < 0 then tex.sprint('-') end
if a == 1 or c == 0 then
tex.sprint(c)
else
tex.sprint('\frac{' .. c ..'}{'.. a ..'}')
end
tex.sprint('}')
end
end{luacode*}
newcommandmakeproblem{directlua{makeproblem()}}
begin{document}
section{Problem}
Let (x_1) and (x_2) be the roots of (makeproblem). Find (x_1x_2).
section{Solution}
[
x_1x_2=solution
]
end{document}

edited 18 hours ago
Mico
281k31385774
281k31385774
answered Jun 11 '11 at 1:30
CaramdirCaramdir
64.5k20214273
64.5k20214273
+1 It is also good. However, it will become more complicated for creating more complicated math problems.
– xport
Jun 11 '11 at 5:24
I've taken the liberty of adding a bit of meta code to pretty-print the Lua code chunk. Feel free to revert.
– Mico
18 hours ago
add a comment |
+1 It is also good. However, it will become more complicated for creating more complicated math problems.
– xport
Jun 11 '11 at 5:24
I've taken the liberty of adding a bit of meta code to pretty-print the Lua code chunk. Feel free to revert.
– Mico
18 hours ago
+1 It is also good. However, it will become more complicated for creating more complicated math problems.
– xport
Jun 11 '11 at 5:24
+1 It is also good. However, it will become more complicated for creating more complicated math problems.
– xport
Jun 11 '11 at 5:24
I've taken the liberty of adding a bit of meta code to pretty-print the Lua code chunk. Feel free to revert.
– Mico
18 hours ago
I've taken the liberty of adding a bit of meta code to pretty-print the Lua code chunk. Feel free to revert.
– Mico
18 hours ago
add a comment |
Funny problem and it's a possibility to test TL 2011 with my package tkz-fct. This package calls tkz-tool-arith.tex and inside this file I define gcd and tkzReducFrac. gcd now is a part of pgf CVS with isprime isodd and iseven.
I use the code of Jake for random. The study of the function is not completed, I give this solution only to see what we can make with little tools.
I compile with xelatex and with -shell-escape because tkz-fct uses only gnuplot
tkzReducFrac defines two macros tkzMathFirstResultand tkzMathSecondResult
documentclass{article}
usepackage{tkz-fct}
begin{document}
section{Problem}
pgfmathsetseed{1}
pgfmathtruncatemacrocoeffa{random(2,10)}
pgfmathtruncatemacrocoeffb{random(2,10)}
pgfmathtruncatemacrocoeffc{random(2,10)}
Let (x_1) and (x_2) be the roots of (coeffa x^2 +coeffb x +coeffc=0). Find (x_1x_2).
tkzReducFrac{coeffc}{coeffa}
section{Solution}
[
x_1x_2=frac{tkzMathFirstResult}{tkzMathSecondResult}
]
% only to make some tests
pgfmathsetmacrodiscrimin{coeffb*coeffb-4*coeffa*coeffc}
pgfmathparse{ifthenelse(notless(discrimin,0)==1,">=0","<0")}
$Delta=b^2-4ac=discriminpgfmathresult$
$a=coeffa$
pgfmathsetmacroxmid{-coeffb/(2*coeffa)}
pgfmathsetmacroxmin{xmid-5} % to adjust the grid and the background
pgfmathsetmacroxmax{xmid+5}
$-b/(2a)=xmid$
% minimum to adjust vertically but it's possible to use a better code
ifnumcoeffa > 0
pgfmathsetmacroymin{-1-discrimin/(coeffa*coeffa)}
pgfmathsetmacroymax{ymin+10}
else
pgfmathsetmacroymax{-1-discrimin/(coeffa*coeffa)}
pgfmathsetmacroymin{ymax-10}
fi
begin{tikzpicture}
tkzInit[xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax]
tkzGrid
tkzAxeXY
tkzFct[blue,thick]{coeffa*x*x+coeffb*x+coeffc}
end{tikzpicture}
end{document}

@xport: +1 is for good answers, not for simply answering.
– Vivi
Jun 11 '11 at 9:27
@xport: this was a good answer, but your comment here and elsewhere of "+1 for answering" implies differently.
– Vivi
Jun 11 '11 at 21:24
add a comment |
Funny problem and it's a possibility to test TL 2011 with my package tkz-fct. This package calls tkz-tool-arith.tex and inside this file I define gcd and tkzReducFrac. gcd now is a part of pgf CVS with isprime isodd and iseven.
I use the code of Jake for random. The study of the function is not completed, I give this solution only to see what we can make with little tools.
I compile with xelatex and with -shell-escape because tkz-fct uses only gnuplot
tkzReducFrac defines two macros tkzMathFirstResultand tkzMathSecondResult
documentclass{article}
usepackage{tkz-fct}
begin{document}
section{Problem}
pgfmathsetseed{1}
pgfmathtruncatemacrocoeffa{random(2,10)}
pgfmathtruncatemacrocoeffb{random(2,10)}
pgfmathtruncatemacrocoeffc{random(2,10)}
Let (x_1) and (x_2) be the roots of (coeffa x^2 +coeffb x +coeffc=0). Find (x_1x_2).
tkzReducFrac{coeffc}{coeffa}
section{Solution}
[
x_1x_2=frac{tkzMathFirstResult}{tkzMathSecondResult}
]
% only to make some tests
pgfmathsetmacrodiscrimin{coeffb*coeffb-4*coeffa*coeffc}
pgfmathparse{ifthenelse(notless(discrimin,0)==1,">=0","<0")}
$Delta=b^2-4ac=discriminpgfmathresult$
$a=coeffa$
pgfmathsetmacroxmid{-coeffb/(2*coeffa)}
pgfmathsetmacroxmin{xmid-5} % to adjust the grid and the background
pgfmathsetmacroxmax{xmid+5}
$-b/(2a)=xmid$
% minimum to adjust vertically but it's possible to use a better code
ifnumcoeffa > 0
pgfmathsetmacroymin{-1-discrimin/(coeffa*coeffa)}
pgfmathsetmacroymax{ymin+10}
else
pgfmathsetmacroymax{-1-discrimin/(coeffa*coeffa)}
pgfmathsetmacroymin{ymax-10}
fi
begin{tikzpicture}
tkzInit[xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax]
tkzGrid
tkzAxeXY
tkzFct[blue,thick]{coeffa*x*x+coeffb*x+coeffc}
end{tikzpicture}
end{document}

@xport: +1 is for good answers, not for simply answering.
– Vivi
Jun 11 '11 at 9:27
@xport: this was a good answer, but your comment here and elsewhere of "+1 for answering" implies differently.
– Vivi
Jun 11 '11 at 21:24
add a comment |
Funny problem and it's a possibility to test TL 2011 with my package tkz-fct. This package calls tkz-tool-arith.tex and inside this file I define gcd and tkzReducFrac. gcd now is a part of pgf CVS with isprime isodd and iseven.
I use the code of Jake for random. The study of the function is not completed, I give this solution only to see what we can make with little tools.
I compile with xelatex and with -shell-escape because tkz-fct uses only gnuplot
tkzReducFrac defines two macros tkzMathFirstResultand tkzMathSecondResult
documentclass{article}
usepackage{tkz-fct}
begin{document}
section{Problem}
pgfmathsetseed{1}
pgfmathtruncatemacrocoeffa{random(2,10)}
pgfmathtruncatemacrocoeffb{random(2,10)}
pgfmathtruncatemacrocoeffc{random(2,10)}
Let (x_1) and (x_2) be the roots of (coeffa x^2 +coeffb x +coeffc=0). Find (x_1x_2).
tkzReducFrac{coeffc}{coeffa}
section{Solution}
[
x_1x_2=frac{tkzMathFirstResult}{tkzMathSecondResult}
]
% only to make some tests
pgfmathsetmacrodiscrimin{coeffb*coeffb-4*coeffa*coeffc}
pgfmathparse{ifthenelse(notless(discrimin,0)==1,">=0","<0")}
$Delta=b^2-4ac=discriminpgfmathresult$
$a=coeffa$
pgfmathsetmacroxmid{-coeffb/(2*coeffa)}
pgfmathsetmacroxmin{xmid-5} % to adjust the grid and the background
pgfmathsetmacroxmax{xmid+5}
$-b/(2a)=xmid$
% minimum to adjust vertically but it's possible to use a better code
ifnumcoeffa > 0
pgfmathsetmacroymin{-1-discrimin/(coeffa*coeffa)}
pgfmathsetmacroymax{ymin+10}
else
pgfmathsetmacroymax{-1-discrimin/(coeffa*coeffa)}
pgfmathsetmacroymin{ymax-10}
fi
begin{tikzpicture}
tkzInit[xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax]
tkzGrid
tkzAxeXY
tkzFct[blue,thick]{coeffa*x*x+coeffb*x+coeffc}
end{tikzpicture}
end{document}

Funny problem and it's a possibility to test TL 2011 with my package tkz-fct. This package calls tkz-tool-arith.tex and inside this file I define gcd and tkzReducFrac. gcd now is a part of pgf CVS with isprime isodd and iseven.
I use the code of Jake for random. The study of the function is not completed, I give this solution only to see what we can make with little tools.
I compile with xelatex and with -shell-escape because tkz-fct uses only gnuplot
tkzReducFrac defines two macros tkzMathFirstResultand tkzMathSecondResult
documentclass{article}
usepackage{tkz-fct}
begin{document}
section{Problem}
pgfmathsetseed{1}
pgfmathtruncatemacrocoeffa{random(2,10)}
pgfmathtruncatemacrocoeffb{random(2,10)}
pgfmathtruncatemacrocoeffc{random(2,10)}
Let (x_1) and (x_2) be the roots of (coeffa x^2 +coeffb x +coeffc=0). Find (x_1x_2).
tkzReducFrac{coeffc}{coeffa}
section{Solution}
[
x_1x_2=frac{tkzMathFirstResult}{tkzMathSecondResult}
]
% only to make some tests
pgfmathsetmacrodiscrimin{coeffb*coeffb-4*coeffa*coeffc}
pgfmathparse{ifthenelse(notless(discrimin,0)==1,">=0","<0")}
$Delta=b^2-4ac=discriminpgfmathresult$
$a=coeffa$
pgfmathsetmacroxmid{-coeffb/(2*coeffa)}
pgfmathsetmacroxmin{xmid-5} % to adjust the grid and the background
pgfmathsetmacroxmax{xmid+5}
$-b/(2a)=xmid$
% minimum to adjust vertically but it's possible to use a better code
ifnumcoeffa > 0
pgfmathsetmacroymin{-1-discrimin/(coeffa*coeffa)}
pgfmathsetmacroymax{ymin+10}
else
pgfmathsetmacroymax{-1-discrimin/(coeffa*coeffa)}
pgfmathsetmacroymin{ymax-10}
fi
begin{tikzpicture}
tkzInit[xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax]
tkzGrid
tkzAxeXY
tkzFct[blue,thick]{coeffa*x*x+coeffb*x+coeffc}
end{tikzpicture}
end{document}

edited Jun 10 '11 at 18:02
answered Jun 10 '11 at 17:43
Alain MatthesAlain Matthes
73.4k7162294
73.4k7162294
@xport: +1 is for good answers, not for simply answering.
– Vivi
Jun 11 '11 at 9:27
@xport: this was a good answer, but your comment here and elsewhere of "+1 for answering" implies differently.
– Vivi
Jun 11 '11 at 21:24
add a comment |
@xport: +1 is for good answers, not for simply answering.
– Vivi
Jun 11 '11 at 9:27
@xport: this was a good answer, but your comment here and elsewhere of "+1 for answering" implies differently.
– Vivi
Jun 11 '11 at 21:24
@xport: +1 is for good answers, not for simply answering.
– Vivi
Jun 11 '11 at 9:27
@xport: +1 is for good answers, not for simply answering.
– Vivi
Jun 11 '11 at 9:27
@xport: this was a good answer, but your comment here and elsewhere of "+1 for answering" implies differently.
– Vivi
Jun 11 '11 at 21:24
@xport: this was a good answer, but your comment here and elsewhere of "+1 for answering" implies differently.
– Vivi
Jun 11 '11 at 21:24
add a comment |
MATLAB version, possibly adaptable to Octave:
First, download my parsetex.m function to somewhere in your search path. Next,
make a template .tex file named foo-gradingkey.tex, with contents like
documentclass{article}
begin{document}
Hello, **studentName**.
section{Problem}
% ^^a=round(9*rand)+1;^^
% ^^b=round(10*rand);^^
% ^^c=round(10*rand);^^
Let (x_1) and (x_2) be the roots of (**a** x^2 + **b** x + **c**=0).
Find (x_1x_2).
section{Solution}
[ x_1x_2=frac{**c**}{**a**}= **c/a** ]
end{document}
Paired double carets and paired double asterisks indicate MATLAB code to be evaluated. Material in paired double carets will remain in the final file, while material in pared double asterisks will be replaced with the evaluated result.
Run the following command in MATLAB:
parsetex('foo-%s.tex',{'Mike Renfro'},{'renfro'});which results in the following message on the command line:
Creating file foo-renfro.tex: done
The contents of foo-renfro.tex will be something like
documentclass{article}
begin{document}
Hello, Mike Renfro.
section{Problem}
% ^^a=round(9*rand)+1;^^
% ^^b=round(10*rand);^^
% ^^c=round(10*rand);^^
Let (x_1) and (x_2) be the roots of (10 x^2 + 2 x + 10=0).
Find (x_1x_2).
section{Solution}
[ x_1x_2=frac{10}{10}= 1 ]
end{document}
It's not perfect, since I've not yet managed to get it to reduce fractions to simplest form, i.e., 2/5 ends up being evaluated as 0.4.
+1: it is interesting. Unfortunately, Matlab & Mathematica are very expensive softwares.
– xport
Jun 11 '11 at 5:21
This approach is very fluid coding workflow.
– xport
Jun 11 '11 at 5:39
@xport: it should work in GNU Octave (this page looks like they have compatible cell arrays), but I've not tried it myself.
– Mike Renfro
Jun 11 '11 at 12:55
I haven't looked through this solution thoroughly to see if this would solve your problem with expressing decimal numbers as rational fractions, but Matlab has aratfunction that might help here; however, it also might make for messy fractions when the answer is better-expressed as an approximate decimal number.
– JohnReed
Aug 5 '12 at 13:40
add a comment |
MATLAB version, possibly adaptable to Octave:
First, download my parsetex.m function to somewhere in your search path. Next,
make a template .tex file named foo-gradingkey.tex, with contents like
documentclass{article}
begin{document}
Hello, **studentName**.
section{Problem}
% ^^a=round(9*rand)+1;^^
% ^^b=round(10*rand);^^
% ^^c=round(10*rand);^^
Let (x_1) and (x_2) be the roots of (**a** x^2 + **b** x + **c**=0).
Find (x_1x_2).
section{Solution}
[ x_1x_2=frac{**c**}{**a**}= **c/a** ]
end{document}
Paired double carets and paired double asterisks indicate MATLAB code to be evaluated. Material in paired double carets will remain in the final file, while material in pared double asterisks will be replaced with the evaluated result.
Run the following command in MATLAB:
parsetex('foo-%s.tex',{'Mike Renfro'},{'renfro'});which results in the following message on the command line:
Creating file foo-renfro.tex: done
The contents of foo-renfro.tex will be something like
documentclass{article}
begin{document}
Hello, Mike Renfro.
section{Problem}
% ^^a=round(9*rand)+1;^^
% ^^b=round(10*rand);^^
% ^^c=round(10*rand);^^
Let (x_1) and (x_2) be the roots of (10 x^2 + 2 x + 10=0).
Find (x_1x_2).
section{Solution}
[ x_1x_2=frac{10}{10}= 1 ]
end{document}
It's not perfect, since I've not yet managed to get it to reduce fractions to simplest form, i.e., 2/5 ends up being evaluated as 0.4.
+1: it is interesting. Unfortunately, Matlab & Mathematica are very expensive softwares.
– xport
Jun 11 '11 at 5:21
This approach is very fluid coding workflow.
– xport
Jun 11 '11 at 5:39
@xport: it should work in GNU Octave (this page looks like they have compatible cell arrays), but I've not tried it myself.
– Mike Renfro
Jun 11 '11 at 12:55
I haven't looked through this solution thoroughly to see if this would solve your problem with expressing decimal numbers as rational fractions, but Matlab has aratfunction that might help here; however, it also might make for messy fractions when the answer is better-expressed as an approximate decimal number.
– JohnReed
Aug 5 '12 at 13:40
add a comment |
MATLAB version, possibly adaptable to Octave:
First, download my parsetex.m function to somewhere in your search path. Next,
make a template .tex file named foo-gradingkey.tex, with contents like
documentclass{article}
begin{document}
Hello, **studentName**.
section{Problem}
% ^^a=round(9*rand)+1;^^
% ^^b=round(10*rand);^^
% ^^c=round(10*rand);^^
Let (x_1) and (x_2) be the roots of (**a** x^2 + **b** x + **c**=0).
Find (x_1x_2).
section{Solution}
[ x_1x_2=frac{**c**}{**a**}= **c/a** ]
end{document}
Paired double carets and paired double asterisks indicate MATLAB code to be evaluated. Material in paired double carets will remain in the final file, while material in pared double asterisks will be replaced with the evaluated result.
Run the following command in MATLAB:
parsetex('foo-%s.tex',{'Mike Renfro'},{'renfro'});which results in the following message on the command line:
Creating file foo-renfro.tex: done
The contents of foo-renfro.tex will be something like
documentclass{article}
begin{document}
Hello, Mike Renfro.
section{Problem}
% ^^a=round(9*rand)+1;^^
% ^^b=round(10*rand);^^
% ^^c=round(10*rand);^^
Let (x_1) and (x_2) be the roots of (10 x^2 + 2 x + 10=0).
Find (x_1x_2).
section{Solution}
[ x_1x_2=frac{10}{10}= 1 ]
end{document}
It's not perfect, since I've not yet managed to get it to reduce fractions to simplest form, i.e., 2/5 ends up being evaluated as 0.4.
MATLAB version, possibly adaptable to Octave:
First, download my parsetex.m function to somewhere in your search path. Next,
make a template .tex file named foo-gradingkey.tex, with contents like
documentclass{article}
begin{document}
Hello, **studentName**.
section{Problem}
% ^^a=round(9*rand)+1;^^
% ^^b=round(10*rand);^^
% ^^c=round(10*rand);^^
Let (x_1) and (x_2) be the roots of (**a** x^2 + **b** x + **c**=0).
Find (x_1x_2).
section{Solution}
[ x_1x_2=frac{**c**}{**a**}= **c/a** ]
end{document}
Paired double carets and paired double asterisks indicate MATLAB code to be evaluated. Material in paired double carets will remain in the final file, while material in pared double asterisks will be replaced with the evaluated result.
Run the following command in MATLAB:
parsetex('foo-%s.tex',{'Mike Renfro'},{'renfro'});which results in the following message on the command line:
Creating file foo-renfro.tex: done
The contents of foo-renfro.tex will be something like
documentclass{article}
begin{document}
Hello, Mike Renfro.
section{Problem}
% ^^a=round(9*rand)+1;^^
% ^^b=round(10*rand);^^
% ^^c=round(10*rand);^^
Let (x_1) and (x_2) be the roots of (10 x^2 + 2 x + 10=0).
Find (x_1x_2).
section{Solution}
[ x_1x_2=frac{10}{10}= 1 ]
end{document}
It's not perfect, since I've not yet managed to get it to reduce fractions to simplest form, i.e., 2/5 ends up being evaluated as 0.4.
edited Aug 6 '12 at 19:29
answered Jun 11 '11 at 5:14
Mike RenfroMike Renfro
17.6k14786
17.6k14786
+1: it is interesting. Unfortunately, Matlab & Mathematica are very expensive softwares.
– xport
Jun 11 '11 at 5:21
This approach is very fluid coding workflow.
– xport
Jun 11 '11 at 5:39
@xport: it should work in GNU Octave (this page looks like they have compatible cell arrays), but I've not tried it myself.
– Mike Renfro
Jun 11 '11 at 12:55
I haven't looked through this solution thoroughly to see if this would solve your problem with expressing decimal numbers as rational fractions, but Matlab has aratfunction that might help here; however, it also might make for messy fractions when the answer is better-expressed as an approximate decimal number.
– JohnReed
Aug 5 '12 at 13:40
add a comment |
+1: it is interesting. Unfortunately, Matlab & Mathematica are very expensive softwares.
– xport
Jun 11 '11 at 5:21
This approach is very fluid coding workflow.
– xport
Jun 11 '11 at 5:39
@xport: it should work in GNU Octave (this page looks like they have compatible cell arrays), but I've not tried it myself.
– Mike Renfro
Jun 11 '11 at 12:55
I haven't looked through this solution thoroughly to see if this would solve your problem with expressing decimal numbers as rational fractions, but Matlab has aratfunction that might help here; however, it also might make for messy fractions when the answer is better-expressed as an approximate decimal number.
– JohnReed
Aug 5 '12 at 13:40
+1: it is interesting. Unfortunately, Matlab & Mathematica are very expensive softwares.
– xport
Jun 11 '11 at 5:21
+1: it is interesting. Unfortunately, Matlab & Mathematica are very expensive softwares.
– xport
Jun 11 '11 at 5:21
This approach is very fluid coding workflow.
– xport
Jun 11 '11 at 5:39
This approach is very fluid coding workflow.
– xport
Jun 11 '11 at 5:39
@xport: it should work in GNU Octave (this page looks like they have compatible cell arrays), but I've not tried it myself.
– Mike Renfro
Jun 11 '11 at 12:55
@xport: it should work in GNU Octave (this page looks like they have compatible cell arrays), but I've not tried it myself.
– Mike Renfro
Jun 11 '11 at 12:55
I haven't looked through this solution thoroughly to see if this would solve your problem with expressing decimal numbers as rational fractions, but Matlab has a
rat function that might help here; however, it also might make for messy fractions when the answer is better-expressed as an approximate decimal number.– JohnReed
Aug 5 '12 at 13:40
I haven't looked through this solution thoroughly to see if this would solve your problem with expressing decimal numbers as rational fractions, but Matlab has a
rat function that might help here; however, it also might make for messy fractions when the answer is better-expressed as an approximate decimal number.– JohnReed
Aug 5 '12 at 13:40
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%2f20404%2fhow-to-create-a-random-math-problem-in-latex%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
Pretty interesting question. I remember dealing with something similar, but my approach was quite hybrid. It was something like generating random coefficients in LaTeX, call a computer algebra system to solve it for me, then redirect the output back to LaTeX. I used Maxima's function
tex(solve(equation))which gives me the result in the LaTeX math format.– Paulo Cereda
Jun 10 '11 at 15:40
2
andreas.scherbaum.la/blog/archives/…
– Marco Daniel
Jun 10 '11 at 15:43
Oh no.. I did not see
SageTeXhas been used. :-(– The Inventor of God
23 hours ago
Yes. That post inspired me to create a lot of tests/quizzes using specific problem types where the numbers are created at random (and then create a solution sheet) using the
sagetexpackage. It is the package to learn if you are teaching math– DJP
22 hours ago
If this is still going to be open for a bounty, what sort of random problems are you looking for: arithmetic, algebra, precalculus, discrete math? Or you still want quadratic mentioned and already solved?
– DJP
22 hours ago