2

I have updated my recent post am I am trying to automate it but need help with a method, I have read all the macro books I can but can not find an answer.

\documentclass{article}
\usepackage{mathtools}

\begin{document}

\begin{gather} ax^2+bx+c=0,\ \shortintertext{for example: } x^2 + 2x -15 = 0 \ %this what I want to automate (x+5)(x-3) = 0, \ %to give this answer \shortintertext{therefore} c=5 \times -3 = -15\quad \text{and}\quad b = 5-3=2. %and this so I can insert the results into another diagram I have made.
\end{gather
}

\end{document}

any help or direction would be amazing.

user202729
  • 7,143
Paul A
  • 1,055
  • 4
  • 14
  • 3
    Check Package polynom. Please, while posting a question write the question on the main text, not only as a comment in the code. – FHZ Jul 02 '22 at 15:19
  • Side note, it's actually a bit unusual to see people mentioning they read books, but if you are serious there's always the option of reading the TeXbook. (or TeX by topic for an unpaid option, or learn+use Lua to program macros) – user202729 Jul 02 '22 at 15:31
  • 1
    you could automate the factorisation in TeX, but why? TeX is a tool for typesetting not (primarily) a tool for calculation. For example you picked an easy formula with integer roots. If you just want to show such cases for different roots I would give the roots as input so \showex{-5}{3} as it is easy to generate all your display from there. If instead you start from the polynomial, it is harder to parse and harder to process, and more cases, complex roots etc – David Carlisle Jul 02 '22 at 15:38
  • 2
    There's no documentation, but I've written a few macros for doing this which are gathered at https://github.com/loopspace/quadratics/blob/master/quadratics.sty – Andrew Stacey Jul 02 '22 at 17:42

2 Answers2

3

Here's a solution using Lua. It isn't perfect, I assumed the main coefficient is 1 and I wasn't very careful with rounding floating values, but it should be easy to tweak to your needs.

\documentclass{article}
\usepackage{luacode}

\begin{luacode} -- Computes the roots of x^2+bx+c=0 -- Returns nothing if they aren't real function roots(b, c) local delta = bb-4c if delta > 0 then deltasq = math.sqrt(delta) local r1 = math.round((-b-deltasq)/2) local r2 = math.round((-b+deltasq)/2) return r1, r2 end end

-- Outputs x^2+bx+c in developed form to LaTeX function display_polynome(b, c) p = "x^2" if b > 0 then p = p .. "+" .. tostring(b) .. "x" elseif b < 0 then p = p .. tostring(b) .. "x" end if c > 0 then p = p .. "+" .. tostring(c) elseif c < 0 then p = p .. tostring(c) end tex.print(p) end

-- Outputs x^2+bx+c in factorized form to LaTeX function display_factorized_polynom(b, c) r1, r2 = roots(b, c) p = "(x" if r1 > 0 then p = p .. "-" .. tostring(r1) .. ")(x" elseif r1 < 0 then p = p .. "+" .. tostring(-r1) .. ")(x" end if r2 > 0 then p = p .. "-" .. tostring(r2) .. ")" elseif r2 < 0 then p = p .. "+" .. tostring(-r2) .. ")" end tex.print(p) end \end{luacode}

\begin{document}

$\directlua{display_polynome(2, -15)}$

$\directlua{display_factorized_polynom(2, -15)}$

\end{document}

result

Bonus points if you're using VS Code, as it switches to Lua syntax highlighting in luacode environments.

Miyase
  • 2,544
  • 2
  • 12
  • 26
3

This is a solution using xfp that should work in any engine — it's not perfect either, and you'll need a lot of ifthenelse to beautify the output, but...

\documentclass{article}
\usepackage{mathtools}
\usepackage{xfp}
\usepackage{ifthen}

\newcommand{\secondorder}[3]{% a, b ,c \def\discr{\fpeval{#2#2-4#1#3}} \ifthenelse{\discr > 0}{ \def\xone{\fpeval{(-#2+sqrt{\discr})/#1/2}} \def\xtwo{\fpeval{(-#2-sqrt{\discr})/#1/2}} \begin{gather} ax^2+bx+c=0,\ \shortintertext{for example: } #1\cdot x^2 + (#2)\cdot x + (#3) = 0 \ (x-(\xone))(x-(\xtwo)) = 0, \ \shortintertext{therefore} c=\xone \times (\xtwo) = #3 \quad \text{and}\quad b = -(\xone)-(\xtwo)=#2. \end{gather}% }{% \begin{gather} ax^2+bx+c=0,\ \shortintertext{for example: } #1\cdot x^2 + (#2)\cdot x + (#3) = 0 \ \shortintertext{has no real roots} \end{gather*} } }

\begin{document}

\secondorder{1}{2}{-15}

\secondorder{1}{2}{1}

\secondorder{1}{0}{-1}

\end{document}

enter image description here

Rmano
  • 40,848
  • 3
  • 64
  • 125