# How can I get this result when using Expand?

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

• 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)] Jul 10 at 5:42

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

myExpand[(x-2) (x-3)] myExpand[(x^2-2x+9) (x^2-3)] 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[InternalSyntacticNegativeQ[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?

• How can I use your code? I copy your code and compile, I cannot get the result. Jul 10 at 6:11
• @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 Jul 10 at 6:14
• I document LaTeX, I write \usepackage{Matex}. Is this true? Jul 10 at 6:17
• @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. Jul 10 at 6:23
• 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. Jul 10 at 6:24