(edit: added simplification if needed)
This output
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}