3
$\begingroup$

I tried to expand this expression (x-2)(x-3). I only want to recieive this result (x-2)(x-3) = x^2 - 2x - 3x + 6, not this result 6 - 5 x + x^2 when I use Expand.

Expand[(x - 2) (x - 3)]

6 - 5 x + x^2

$\endgroup$
1
  • $\begingroup$ I can't see why you want to distribute just the x term but not the integers. Nevertheless, try this: Distribute[(x - 2) . (x - 3)] $\endgroup$ Commented Jul 10, 2023 at 5:42

1 Answer 1

3
$\begingroup$

If you just want this for formatting output, you can use Latex

myExpand[(x-2) (x-3)]

Mathematica graphics

myExpand[(x^2-2x+9) (x^2-3)]

Mathematica graphics

Because this uses Latex strings to build the result, it does not ofcourse simplify same power terms of $x$ as Expand does.

<<MaTeX`
myExpand[p_Times]:=Module[{s,z,n,m,p1,p2},
  p1=First@p; p2=Last@p; 
  s="";
  Do[
    z=p1[[n]]*p2[[m]];
    s=s<>If[Internal`SyntacticNegativeQ[z],
       ToString[TeXForm@z]
    ,
       If[s=="",ToString[TeXForm@z],"+"<>ToString[TeXForm@z]]
    ]
    ,{n,1,Length@p1},{m,1,Length@p2}
  ];
   MaTeX[s,Magnification->2]
]

You might also be interested in How can I get scheme product of two polynomials like this?

$\endgroup$
5
  • $\begingroup$ How can I use your code? I copy your code and compile, I cannot get the result. $\endgroup$ Commented Jul 10, 2023 at 6:11
  • $\begingroup$ @JohnPaulPeter do you have MaTeX package installed? You need that ofcourse to work. The first command is <<MaTeX which loads the package. This is well known package and very useful to have. Here is screen shot from my computer. !Mathematica graphics $\endgroup$
    – Nasser
    Commented Jul 10, 2023 at 6:14
  • $\begingroup$ I document LaTeX, I write \usepackage{Matex}. Is this true? $\endgroup$ Commented Jul 10, 2023 at 6:17
  • $\begingroup$ @JohnPaulPeter No. You do not need to do anything like this. Simply install the MaTeX package inside Mathematica. It will do everything for you and will compile the string to Latex inside Mathematica. The string s you see in the function is Latex, but you need MaTeX to compile it. Just go to MaTeXInstall and run the command shown there. That is all. $\endgroup$
    – Nasser
    Commented Jul 10, 2023 at 6:23
  • $\begingroup$ If you do not need to use this inside the notebook, simply copy the string s and use it in your Latex document as is. Change the function above to return s instead of calling MaTeX as I showed. $\endgroup$
    – Nasser
    Commented Jul 10, 2023 at 6:24

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.