1
$\begingroup$

When I have an input like:

1/2*k

The output is:

enter image description here

How to change the output of Timesfunction to see this result:

enter image description here

Another option is to put all rational number using Deferfunction automatically.

$\endgroup$
  • $\begingroup$ TraditionalForm[Defer[1/2 ] k ]? $\endgroup$ – kglr Jan 29 at 17:32
  • $\begingroup$ I would to like to change the function Times. $\endgroup$ – Mateus Jan 29 at 17:36
  • $\begingroup$ how about Infix[1/2 k, "\[InvisibleTimes]"]? $\endgroup$ – kglr Jan 29 at 18:06
  • $\begingroup$ The problem is, every time I want to write a Rational number I will have to put Infix..... I want a way to program just in one line for every Rational number that I write from now on. $\endgroup$ – Mateus Jan 29 at 18:11
  • $\begingroup$ You want to look at the Notation package. You certainly do not want to change the workings of Times---it's very deep down and all sorts of things depend on it working correctly. $\endgroup$ – evanb Jan 29 at 18:31
2
$\begingroup$

You can just override the formatting of Times:

MakeBoxes[Times[r_Rational, s_], StandardForm] := RowBox[{MakeBoxes[r], MakeBoxes[s]}]
MakeBoxes[Times[r_Rational, s__], StandardForm] := RowBox[{MakeBoxes[r], MakeBoxes[Times[s]]}]

Then:

k/2

enter image description here

$\endgroup$
  • $\begingroup$ Should the first FormatValue be RowBox[{MakeBoxes[r], Parenthesize[s, StandardForm, Times]}]? $\endgroup$ – Chip Hurst Jan 29 at 22:10
  • $\begingroup$ @ChipHurst Yes, that would be a good idea, so that 1/2 (a+b) formats correctly. $\endgroup$ – Carl Woll Jan 30 at 0:42
  • $\begingroup$ The problem is when I have a symbol in the denominator. For example, for 1/α*k the output will be k/α because for Mathematica 1/α is not a Rational number. How to change for the output be 1/α k. $\endgroup$ – Mateus Jan 30 at 22:45
2
$\begingroup$

You just want to change notation, not the way Times actually works.

You can try using a separator, as @kglr suggests in the comments.

<<Notation`
Notation[ParsedBoxWrapper[
    RowBox[{"a_", "\[InvisibleTimes]", "b_"}]] 
    \[DoubleLongLeftRightArrow] ParsedBoxWrapper[
    RowBox[{"Times", "[", RowBox[{"a_", ",", "b_"}], "]"}]]]

After evaluating the above, when I type

k/2

I see

(1/2) k

But when I type k/3/d I see (1/3)((1/d)k). So maybe more finesse is needed...

$\endgroup$
  • $\begingroup$ Update: you must <<Notation` first. $\endgroup$ – evanb Jan 29 at 18:55
  • $\begingroup$ According to stackoverflow.com/questions/4635701/… you may want to set Notation`AutoLoadNotationPalette=False at the beginning, when you're done fiddling with notation. $\endgroup$ – evanb Jan 29 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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