I would like to define an additional OutputForm command such that, when applied to a long expression involving several variables (probably at different levels, within nested arrays and functions), it replaces all the variables with their corresponding formats. In this way, I'd like to be able to display SomeExpression in two different ways, using

SomeExpression //OutputForm
SomeExpression //OutputForm2

As for the format definition, for each variable, I'd like to be able to specify it via:


In addition, it would be desirable to set OutputForm as the default function for variables that lack a pre-defined OutputForm2 format.

P.S. Note that Similar questions have been asked before, but no solutions have been given without overwriting the default OutputForm/StandardForm/etc functions.

  • 1
    $\begingroup$ Slightly difficult to work without examples, but would this work? Clear[OutputForm2]; OutputForm2[expr_] := Block[{OutputForm}, Format[f[x_], OutputForm] := f^x; OutputForm[expr] ]; OutputForm2[f[x] + f[y] + 1] $\endgroup$
    – Domen
    Jan 19 at 14:55
  • $\begingroup$ Thanks @Domen. However, please note that although you added {OutputForm} to Block[], this is still rewriting the OutputForm of f[x]. To check this, write: OutputForm[f[x]+f[y]+1], then OutputForm2[f[x]+f[y]+1], and then again OutputForm[f[x]+f[y]+1]. It might be because OutputForm is Protected?. Perhaps we should so something like this instead: OutputForm2[expr_]:=Block[{OutputForm},Format[f[x_],OutputForm]:=f^x; Return[OutputForm[expr]]; "..." ]; where "..." stands for some code to undo the initial Format[f[x_],OutputForm] call. $\endgroup$
    – JuanC97
    Jan 20 at 0:11
  • 1
    $\begingroup$ Ahh, yes, I see ... Hm. The only silly solution that comes to my mind now is to make OutputForm3 behave as a regular OutputForm by removing all FormatValues: OutputForm2[expr_] := Module[{}, Format[f[x_], OutputForm] := f^x; OutputForm[expr] ]; OutputForm3[expr_] := Module[{}, FormatValues[f] := {}; OutputForm[expr] ]; $\endgroup$
    – Domen
    Jan 21 at 14:35


Your Answer

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

Browse other questions tagged or ask your own question.