2
$\begingroup$

Sometimes, I want to create some versions of exercises with the same content. For example, solve the equations:

  1. $x^2 - 5x - 6 =0$;
  2. $x^2 + 5x - 6 =0$;
  3. $2x^2 + 5x - 7 =0$.

Then I solve each equation in my document.

enter image description here

How can I create some versions of exercises and get a LaTeX file?

$\endgroup$
3
  • $\begingroup$ “to solutions” should be “two solutions”, and the variables should be $x_1$ and $x_2$, not $x_1$ and $x_1$ $\endgroup$
    – Ghoster
    Commented Jan 31 at 0:50
  • $\begingroup$ The intermediate result for your second $\Delta$ is incorrect. $\endgroup$
    – Ghoster
    Commented Jan 31 at 0:54
  • $\begingroup$ Thank you very much. $\endgroup$ Commented Jan 31 at 0:55

2 Answers 2

5
$\begingroup$

(edit: added simplification if needed)

This output

enter image description here

is generated by this code (note: code is written in CODE cell (not INPUT) cell). To copy please copy to code cell, else it will get mis-formatted).

Here is link to notebook also.

To use, add your quadratic equation to the list eqs below and run the whole code. It will create file A.tex in same folder as notebook. Compile the A.tex file using lualatex or pdflatex or any Latex compiler to obtain the PDF showing the step by step solution.

eqs={x^2-5*x-6==0,x^2+5*x-6==0,2*x^2+5*x-7==0,Sqrt[2] x^2+x-(Sqrt[2]+1)==0}
toX[e_]:=ToString[TeXForm[e]];
formatA[eqIn_Equal,x_]:=Module[{eq=eqIn,s,lis,a,b,c,discriminant},
    eq=First@eq-Last@eq;
    lis=CoefficientList[eq,x]; 
    Print["lis=",lis];
    If[Length[lis]!=3,Abort];
    c=lis[[1]];b=lis[[2]];a=lis[[3]];
    discriminant=b^2-4*a*c; 
    If[LeafCount[Simplify@discriminant]<LeafCount[discriminant],
        s="\\[\n\\begin{array}[c]{cccccc}\n"<>
          "\\Delta &= b^2 - 4 a c &=\\left("<> toX[b]<>"\\right)^2- 4\\left("<>toX[a]<>"\\right)\\left("<>toX[c]<>"\\right)"<>
          "&="<>toX[discriminant]<>"&="<>toX[Simplify@discriminant]<>
          "\n\\end{array}\n\\]\n";
        discriminant=Simplify@discriminant
    ,
        s="\\[\n\\begin{array}[c]{cccc}\n"<>
          "\\Delta &= b^2 - 4 a c &=\\left("<> toX[b]<>"\\right)^2- 4\\left("<>toX[a]<>"\\right)\\left("<>toX[c]<>"\\right)"<>
          "&="<>toX[discriminant]<>
          "\n\\end{array}\n\\]\n";
   ];
   {s,a,b,c,discriminant}
];

formatB[a_,b_,c_,x_,disc_]:=Module[{s,sol},
      sol=(-b+Sqrt[disc])/(2*a);
      If[LeafCount[FullSimplify[sol]]<LeafCount[sol],
         s="\\[\n\\begin{array}[c]{ccccc}\n"<>
             toX[x]<>"_1 &= \\frac{-b + \\sqrt{\\Delta}}{2a}"<>
             "&= \\frac{-\\left("<>toX[b]<>"\\right) + \\sqrt{"<>toX[disc]<>"}}{2\\left("<>toX[a]<>"\\right)} "<>
             "&= "<>toX[sol]<>" &="<>toX[FullSimplify[sol]]<>"\n\\end{array}\n\\]\n";
         ,
         s="\\[\n\\begin{array}[c]{cccc}\n"<>
             toX[x]<>"_1 &= \\frac{-b + \\sqrt{\\Delta}}{2a}"<>
             "&= \\frac{-\\left("<>toX[b]<>"\\right) + \\sqrt{"<>toX[disc]<>"}}{2\\left("<>toX[a]<>"\\right)} "<>
             "&= "<>toX[sol]<>"\n\\end{array}\n\\]\n";
      ];
      s=s<>"And\n";
      
      sol=(-b-Sqrt[disc])/(2*a);
      If[LeafCount[FullSimplify[sol]]<LeafCount[sol],
         s=s<>"\\[\n\\begin{array}[c]{ccccc}\n"<>
             toX[x]<>"_2 &= \\frac{-b - \\sqrt{\\Delta}}{2a}"<>
             "&= \\frac{-\\left("<>toX[b]<>"\\right) - \\sqrt{"<>toX[disc]<>"}}{2\\left("<>toX[a]<>"\\right)} "<>
             "&= "<>toX[sol]<>" &="<>toX[FullSimplify[sol]]<>"\n\\end{array}\n\\]\n";
         ,
         s=s<>"\\[\n\\begin{array}[c]{cccc}\n"<>
             toX[x]<>"_2 &= \\frac{-b - \\sqrt{\\Delta}}{2a}"<>
             "&= \\frac{-\\left("<>toX[b]<>"\\right) - \\sqrt{"<>toX[disc]<>"}}{2\\left("<>toX[a]<>"\\right)} "<>
             "&= "<>toX[sol]<>"\n\\end{array}\n\\]\n";
      ];
        
      s
];

SetDirectory[NotebookDirectory[]]
fileName=FileNameJoin[{Directory[],"A.tex"}]
If[FileExistsQ[fileName],DeleteFile[fileName]];
file=OpenWrite[fileName,PageWidth->Infinity];
WriteString[file,"\\documentclass[12pt,a4paper]{article}\n"<>
       "\\usepackage[margin=1in]{geometry}\n"<>
       "\\usepackage{amsmath}\n"<>
        "\\usepackage{mathtools}\n"<>
        "\\begin{document}\n"
];

WriteString[file,"Solve the following equations\n\\begin{enumerate}\n"];
Do[WriteString[file,"\\item $"<>toX[eqs[[counter]]]<>"$\n"],{counter,1,Length@eqs}];
WriteString[file,"\\end{enumerate}\n"];
WriteString[file,"\\begin{enumerate}\n"];

Do[currentEq=eqs[[counter]];
    {s,a,b,c,discriminant}=formatA[currentEq,x];
    WriteString[file,"\\item For the equation $"<>toX[currentEq]<>"$ we have\n"<>s<>
        "The given equation has two solutions\n"<>formatB[a,b,c,x,discriminant]
    ],
    {counter,1,Length@eqs}
];

WriteString[file,"\\end{enumerate}\n\\end{document}\n"];
Close[file]

And this is the latex file

\documentclass[12pt,a4paper]{article}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath}
\usepackage{mathtools}
\begin{document}
Solve the following equations
\begin{enumerate}
\item $x^2-5 x-6=0$
\item $x^2+5 x-6=0$
\item $2 x^2+5 x-7=0$
\item $\sqrt{2} x^2+x-\sqrt{2}-1=0$
\end{enumerate}
\begin{enumerate}
\item For the equation $x^2-5 x-6=0$ we have
\[
\begin{array}[c]{cccc}
\Delta &= b^2 - 4 a c &=\left(-5\right)^2- 4\left(1\right)\left(-6\right)&=49
\end{array}
\]
The given equation has two solutions
\[
\begin{array}[c]{cccc}
x_1 &= \frac{-b + \sqrt{\Delta}}{2a}&= \frac{-\left(-5\right) + \sqrt{49}}{2\left(1\right)} &= 6
\end{array}
\]
And
\[
\begin{array}[c]{cccc}
x_2 &= \frac{-b - \sqrt{\Delta}}{2a}&= \frac{-\left(-5\right) - \sqrt{49}}{2\left(1\right)} &= -1
\end{array}
\]
\item For the equation $x^2+5 x-6=0$ we have
\[
\begin{array}[c]{cccc}
\Delta &= b^2 - 4 a c &=\left(5\right)^2- 4\left(1\right)\left(-6\right)&=49
\end{array}
\]
The given equation has two solutions
\[
\begin{array}[c]{cccc}
x_1 &= \frac{-b + \sqrt{\Delta}}{2a}&= \frac{-\left(5\right) + \sqrt{49}}{2\left(1\right)} &= 1
\end{array}
\]
And
\[
\begin{array}[c]{cccc}
x_2 &= \frac{-b - \sqrt{\Delta}}{2a}&= \frac{-\left(5\right) - \sqrt{49}}{2\left(1\right)} &= -6
\end{array}
\]
\item For the equation $2 x^2+5 x-7=0$ we have
\[
\begin{array}[c]{cccc}
\Delta &= b^2 - 4 a c &=\left(5\right)^2- 4\left(2\right)\left(-7\right)&=81
\end{array}
\]
The given equation has two solutions
\[
\begin{array}[c]{cccc}
x_1 &= \frac{-b + \sqrt{\Delta}}{2a}&= \frac{-\left(5\right) + \sqrt{81}}{2\left(2\right)} &= 1
\end{array}
\]
And
\[
\begin{array}[c]{cccc}
x_2 &= \frac{-b - \sqrt{\Delta}}{2a}&= \frac{-\left(5\right) - \sqrt{81}}{2\left(2\right)} &= -\frac{7}{2}
\end{array}
\]
\item For the equation $\sqrt{2} x^2+x-\sqrt{2}-1=0$ we have
\[
\begin{array}[c]{cccccc}
\Delta &= b^2 - 4 a c &=\left(1\right)^2- 4\left(\sqrt{2}\right)\left(-1-\sqrt{2}\right)&=1-4 \sqrt{2} \left(-1-\sqrt{2}\right)&=9+4 \sqrt{2}
\end{array}
\]
The given equation has two solutions
\[
\begin{array}[c]{ccccc}
x_1 &= \frac{-b + \sqrt{\Delta}}{2a}&= \frac{-\left(1\right) + \sqrt{9+4 \sqrt{2}}}{2\left(\sqrt{2}\right)} &= \frac{\sqrt{9+4 \sqrt{2}}-1}{2 \sqrt{2}} &=1
\end{array}
\]
And
\[
\begin{array}[c]{ccccc}
x_2 &= \frac{-b - \sqrt{\Delta}}{2a}&= \frac{-\left(1\right) - \sqrt{9+4 \sqrt{2}}}{2\left(\sqrt{2}\right)} &= \frac{-1-\sqrt{9+4 \sqrt{2}}}{2 \sqrt{2}} &=-1-\frac{1}{\sqrt{2}}
\end{array}
\]
\end{enumerate}
\end{document}
$\endgroup$
6
  • $\begingroup$ Please see output of pdf file at Delta. $\endgroup$ Commented Jan 31 at 5:21
  • $\begingroup$ Please simplify two solutions of the equation Sqrt[2] x^2 + x - (Sqrt[2] + 1) == 0 $\endgroup$ Commented Jan 31 at 6:24
  • $\begingroup$ Delta of the first Delta = (-5)^2 - 4 (1) - 6 must be Delta = (-5)^2 - 4 (1)( - 6) $\endgroup$ Commented Jan 31 at 8:53
  • $\begingroup$ At 4th equation, with Delta = 9 + 4 Sqrt[2]. Can we write this number equal to (2 Sqrt[2] + 1)^2? $\endgroup$ Commented Jan 31 at 8:57
  • $\begingroup$ Can we write this number equal to (2 Sqrt[2] + 1)^2? I am sure for this specific case. But I did FullSimplify and this is what Mathematica returned and that is what I used. If you want to customize things more case by case you could do that. $\endgroup$
    – Nasser
    Commented Jan 31 at 9:07
1
$\begingroup$

Question Challenge: Non-Mathematica Answer.

I hope this is not against the rules of the site, but I suggest that the best tool for this job is not Mathematica.

If you can compile in LuaLaTeX, the luacas package allows you to do exactly this sort of computation / replacement straight in the .tex file, with very simple syntax.

And in case you are interested, I am working on a similar system with random generation of quadratic equations, which is now available on GitHub.

$\endgroup$
4
  • $\begingroup$ Can you give me a text file? $\endgroup$ Commented Feb 1 at 6:18
  • $\begingroup$ @minhthien_2016 how do you mean -- a text file of what? of my project? $\endgroup$ Commented Feb 1 at 21:53
  • $\begingroup$ A text file answer my question of your project. $\endgroup$ Commented Feb 2 at 0:55
  • $\begingroup$ I try your package. I think, It is not very simple. $\endgroup$ Commented Apr 21 at 2:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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