3
$\begingroup$

We have

Floor[x]//TraditionalForm

with output

⌊x⌋

If we define

FracPart[x_]:=x-Floor[x]

How can we get

FracPart[x]//TraditionalForm

to output something like

{x}
$\endgroup$
2
  • 3
    $\begingroup$ Well certainly you won't get $\{ x \}$ as the curly brackets denote lists in Mathematica. $\endgroup$ Aug 31, 2018 at 17:14
  • $\begingroup$ I actually want to get this in LaTeX form. I am not really using the Traditional Form in Mathematica. $\endgroup$
    – faceclean
    Sep 3, 2018 at 8:12

2 Answers 2

5
$\begingroup$

I would do this by giving FracPart a TemplateBox formatting rule:

MakeBoxes[FracPart[x_], TraditionalForm]:=TemplateBox[{MakeBoxes[x,TraditionalForm]},
    "FracPart",
    DisplayFunction->(RowBox[{"{",#, "}"}]&),
    Tooltip->"Fractional part"
]

Then:

FracPart[x^2-1] //TraditionalForm

enter image description here

Using a TemplateBox ensures that copy/paste produces a FracPart object instead of a List object, and the Tooltip is a useful reminder that the braces are used to represent the FracPart function instead of a list.

Addendum

The OP asks how this format can be extended to Inactive[FracPart]. Since FracPart would be too deep for UpValues to work, one needs to modify the Inactive symbol instead. However, Inactive already has many FormatValues defined, and one needs to define the new FormatValues so that it takes precedence over the already existing ones. One way to do this is with my Initial function, which I reproduce below:

Initial /: Verbatim[TagSetDelayed][Initial[sym_], lhs_, rhs_] := With[
    {
    new=Block[{sym},
        TagSetDelayed[sym,lhs,rhs];
        First @ Language`ExtendedDefinition[sym]
    ],
    protect=Unprotect[sym]
    },

    sym;
    Quiet@MakeBoxes[sym[],TraditionalForm];

    Unprotect[sym];
    Replace[
        new,
        Rule[values_,n:Except[{}]] :> (
            values[sym] = DeleteDuplicates @ Join[n, values[sym]]
        ),
        {2}
    ];
    Protect@protect;
]

Then, use the Initial wrapper:

Unprotect[Inactive];
Initial[Inactive] /: MakeBoxes[Inactive[FracPart][x_], TraditionalForm] := TemplateBox[
    {MakeBoxes[x,TraditionalForm]},
    "FracPart",
    DisplayFunction->(RowBox[{"{",#,"}"}]&),
    Tooltip->"Fractional part"
]
Protect[Inactive];

Here is the formatting in action:

Inactive[FracPart][2.3] //TraditionalForm

{2.3}

$\endgroup$
1
  • $\begingroup$ Is there any way to make this work for Inactive[FracPar][x^2-1]? $\endgroup$
    – faceclean
    Sep 3, 2018 at 8:23
2
$\begingroup$

As noted in Stork's comment, your requested notation is ill advised. If you still want to shoot yourself in the foot, try the following.

Clear[FracPart];

FracPart[x_?NumericQ] := x - Floor[x];

FracPart /: Format[FracPart[x_], TraditionalForm] := 
 Interpretation[
  RawBoxes[RowBox[{"{", ToBoxes[x, TraditionalForm], "}"}]], 
  FracPart[x]]

The condition in the definition is so that only numerical values expand.

{FracPart[5/4], FracPart[x], FracPart[x] // TraditionalForm}
(*{1/4, FracPart[x], {x}}*)

The downside of using TraditionalForm is that the wrap prevents further calculations.

TraditionalForm[1] - 1
(* -1 + 1 *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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