When I have an input like:
1/2*k
The output is:
How to change the output of Times
function to see this result:
Another option is to put all rational number using Defer
function automatically.
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
RowBox[{MakeBoxes[r], Parenthesize[s, StandardForm, Times]}]
?
$\endgroup$
Commented
Jan 29, 2019 at 22:10
1/2 (a+b)
formats correctly.
$\endgroup$
Commented
Jan 30, 2019 at 0:42
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$
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...
TraditionalForm[Defer[1/2 ] k ]
? $\endgroup$Times
. $\endgroup$Infix[1/2 k, "\[InvisibleTimes]"]
? $\endgroup$Infix.....
I want a way to program just in one line for every Rational number that I write from now on. $\endgroup$Notation
package. You certainly do not want to change the workings ofTimes
---it's very deep down and all sorts of things depend on it working correctly. $\endgroup$