You can create your own ...Form
wrapper that will format inactive elements as if they where active.
Let's start with a helper function, that we'll use in a TagBox
, to reinterpret expression created from active boxes, as inactive expression:
ClearAll[inactivateHead]
SetAttributes[inactivateHead, HoldAllComplete]
inactivateHead[head_[args___]] := Inactive[head][args]
Now our final activeForm
wrapper, which locally overrides MakeBoxes
for Inactive
.
ClearAll[activeForm]
Options[activeForm] = {Editable -> True};
activeForm /: MakeBoxes[activeForm[expr_, opts : OptionsPattern[]], form_] :=
Block[{Inactive},
If[OptionValue[activeForm, {opts}, Editable],
Inactive /: MakeBoxes[Inactive[head_][args___], f_] :=
TagBox[MakeBoxes[head[args], f], inactivateHead]
(* else *),
Inactive /: MakeBoxes[Inactive[head_][args___], f_] :=
InterpretationBox[#, expr] &@MakeBoxes[head[args], f]
];
TagBox[MakeBoxes[expr, form], activeForm]
]
If you want to copy and paste output of activeForm
and then edit what you pasted, set Editable
option to True
, then TagBox
, with our inactivateHead
function, will be used, to reinterpret expression from edited boxes as inactive expression, but this reinterpretation might not be completely accurate.
If you don't need to modify copy/pasted output of activeForm
, you can use Editable -> False
, then an uneditable InterpretationBox
will be used.
To be able to convert activeForm
-wrapped expressions to strings in arbitrary forms we can override ToString
by setting activeForm
up-values:
activeForm /: ToString[activeForm[expr_], rest___] :=
Function[activeExpr,
ToString[Unevaluated[activeExpr], rest],
HoldAllComplete
] @@ (HoldComplete[expr] /. Inactive[x_] :> x)
activeForm /: ToString[expr_, activeForm, rest___] :=
ToString[activeForm[expr], StandardForm, rest]
Finally add activeForm
to $OutputForms
:
If[FreeQ[$OutputForms, #, {1}],
Unprotect @ $OutputForms;
PrependTo[$OutputForms, #];
Protect @ $OutputForms;
]& @ activeForm
An example inactive expression in StandardForm
and activeForm
:
tmpInactive = Inactive[Plus][Inactive[Subtract][1, 2], Inactive[Divide][6, 3], 4]
tmpInactive // activeForm
Same expression converted to string in default OutputForm
:
ToString[tmpInactive]
ToString[tmpInactive // activeForm]
in InputForm
:
ToString[tmpInactive, InputForm]
ToString[tmpInactive // activeForm, InputForm]
in StandardForm
:
ToString[tmpInactive, StandardForm]
ToString[tmpInactive // activeForm, StandardForm]
and directly in activeForm
which is equivalent to activeForm
with StandardForm
:
ToString[tmpInactive, activeForm]
All expressions given by OP (click on image to enlarge it):
res1 // activeForm
MakeBoxes[Inactive[Plus][x__], StandardForm] := MakeBoxes[Plus[x], StandardForm]
? $\endgroup$ToString[res1[[1]], FormatType -> StandardForm] // ToString
and this can give the string result. $\endgroup$