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$
5
  • $\begingroup$ TraditionalForm[Defer[1/2 ] k ]? $\endgroup$
    – kglr
    Commented Jan 29, 2019 at 17:32
  • $\begingroup$ I would to like to change the function Times. $\endgroup$
    – Mateus
    Commented Jan 29, 2019 at 17:36
  • $\begingroup$ how about Infix[1/2 k, "\[InvisibleTimes]"]? $\endgroup$
    – kglr
    Commented Jan 29, 2019 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
    Commented Jan 29, 2019 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
    Commented Jan 29, 2019 at 18:31

2 Answers 2

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$
3
  • $\begingroup$ Should the first FormatValue be RowBox[{MakeBoxes[r], Parenthesize[s, StandardForm, Times]}]? $\endgroup$
    – Greg Hurst
    Commented Jan 29, 2019 at 22:10
  • $\begingroup$ @ChipHurst Yes, that would be a good idea, so that 1/2 (a+b) formats correctly. $\endgroup$
    – Carl Woll
    Commented Jan 30, 2019 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
    Commented Jan 30, 2019 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$
2
  • $\begingroup$ Update: you must <<Notation` first. $\endgroup$
    – evanb
    Commented Jan 29, 2019 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
    Commented Jan 29, 2019 at 20:25

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.