5
$\begingroup$

I find the StandardForm formatting for Piecewise, KroneckerDelta, and ConditionalExpression to be very annoying.

How do I turn off the formatting for these objects, without changing their TraditionalForm formatting?

$\endgroup$
  • 1
    $\begingroup$ In what form do you want them displayed? InputForm? TraditionalForm? Custom form? $\endgroup$ – Bob Hanlon Sep 14 at 16:49
  • $\begingroup$ @Bob For example, I am looking for KroneckerDelta[a,b] to format as is without using δ. $\endgroup$ – QuantumDot Oct 6 at 21:49
1
$\begingroup$

When you define a piecewise function, you may wrap InputForm around the definition to force the output to be in input form. E.g. f1 will print in standard form and f2 in input form:

f1[x_] := 
 Piecewise[{{1, 0 < x <= 1}, {2, 1 < x <= 2}, {3, 2 < x <= 3}}]
f2[x_] := 
 InputForm[
  Piecewise[{{1, 0 < x <= 1}, {2, 1 < x <= 2}, {3, 2 < x <= 3}}]]
f1[t]
f2[t]
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ If you include InputForm as part of the definition you will have some problems. Look at the result for f2'[t] you end up with an InputForm' factor. As a general rule, wrappers should not be included in definitions. $\endgroup$ – Bob Hanlon Sep 14 at 17:39
  • $\begingroup$ Well, that's true. But do you have a better idea? $\endgroup$ – Daniel Huber Sep 15 at 9:53
  • $\begingroup$ If the OP wants all output in a specified form, use $Post. It is more involved if only these functions are to have their StandardForm changed. $\endgroup$ – Bob Hanlon Sep 15 at 13:56
1
$\begingroup$

Following up on Bob Hanlon's comment mentioning $Post I found $PrePrint and its documentation and came up with the following solution:

PrePrintFormats=<|
    KroneckerDelta->FullForm,
    Piecewise->FullForm,
    ConditionalExpression->FullForm
|>;
$PrePrint=If[MemberQ[Keys[PrePrintFormats],Head[#]],PrePrintFormats[Head[#]][#],#]&;

This outputs KroneckerDelta, Piecewise and ConditionalExpression using their respective FullForm. This changes only the OutputForm of attached to those symbols without affecting other forms like TraditionalForm or StandartForm.

Output

I also tried a more direct approach by unprotecting the Symbols and trying to add UpValues on Format[...,OutputForm] but this did not work. I am not sure if this modification to $PrePrint has any nasty side-effects but I think the variable is meant for stuff like this so it might be fine.

EDIT: Using InputForm for the output formatting in cells with multiple outputs causes some outputs not to be printed (no real idea why). FullForm seems to work better. But further testing is required.

| improve this answer | |
$\endgroup$
1
$\begingroup$

After playing around, I figured the easiest way is to simply define the FormatValues of these symbols manually:

Unprotect[Piecewise, KroneckerDelta, ConditionalExpression];
FormatValues[Piecewise] = 
  FormatValues[Piecewise] /. 
   Verbatim[BoxForm`fmt_] | BoxForm`fmt :> TraditionalForm;
FormatValues[
   KroneckerDelta] = {HoldPattern[
     MakeBoxes[KroneckerDelta[BoxForm`a$__], TraditionalForm] /; 
      BoxForm`sufficientVersionQ[6.1]] :> 
    TemplateBox[{RowBox[
       Riffle[Function[BoxForm`x$, 
          MakeBoxes[BoxForm`x$, TraditionalForm], HoldAllComplete] /@ 
         Unevaluated[{BoxForm`a$}], ","]]}, "KroneckerDeltaSeq"], 
   HoldPattern[
     MakeBoxes[KroneckerDelta[BoxForm`n__], TraditionalForm]] :> 
    SubscriptBox[BoxForm`FuncIntBox["δ", KroneckerDelta], 
     If[Length[Unevaluated[{BoxForm`n}]] == 1, 
      MakeBoxes[BoxForm`n, TraditionalForm], 
      RowBox[BoxForm`AddCommas[
        List @@ Function[{BoxForm`x}, 
           MakeBoxes[BoxForm`x, TraditionalForm], {HoldAllComplete}] /@
       HoldComplete[BoxForm`n]]]]]};
FormatValues[ConditionalExpression] = {};
Protect[Piecewise, KroneckerDelta, ConditionalExpression];

I have placed these lines of code in my init.m file, so they run when the kernel starts up. Now I don't have to deal with their fancy StandardForm typesetting again.

| improve this answer | |
$\endgroup$
  • $\begingroup$ This method isn't ideal, because it causes Mathematica to generate error messages when it tries to display unrelated things, like SeriesData etc for the first time. I am still looking for a canonical answer. $\endgroup$ – QuantumDot Oct 5 at 6:57

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.