这个功能似乎做到了:
Clear[MakeBoxesStop];
MakeBoxesStop /: MakeBoxes[MakeBoxesStop[expr_], form_] :=
Module[{heldHeads =
Join @@ Cases[expr,s_Symbol[___] :> HoldComplete[s], {0, Infinity},
Heads -> True],
modified, direct, tempContext = ToString[Unique[]] <> "`"},
Block[{$ContextPath = $ContextPath, $Packages = $Packages},
BeginPackage[tempContext];
modified =
Join @@ Map[
Function[head,
ToExpression[ToLowerCase[ToString[Unevaluated[head]]],InputForm, HoldComplete],
HoldAllComplete],
heldHeads];
EndPackage[];
With[{newexpr =
expr /. (List @@ Thread[HoldPattern /@ heldHeads -> modified, HoldComplete])},
With[{result =
MakeBoxes[newexpr, form] /.
Thread[Rule @@
Map[List @@
Map[Function[head, ToString[Unevaluated[head]], HoldAllComplete], #] &,
{modified , heldHeads}]]
},
Remove @@ Names[tempContext <> "*"];
result]]]];
它不会赢得优雅竞赛,并且可能不是很干净,但它似乎可以满足您的要求:
In[270]:= MakeBoxesStop[Graphics[Disk[]]]
Out[270]= Graphics[Disk[List[0, 0]]]
如果您不想MakeBoxesStop
计算内部的表达式,请在正文中添加适当的属性和Unevaluated
包装器。
编辑
以下简单的盒子制作功能基于此处发布的 Mathematica 解析器:
Clear[toBoxes];
toBoxes[expr_] :=
First[parse[tokenize[ToString@FullForm[expr]]] //. {
head_String[elem_] :> RowBox[{head, "[", elem, "]"}],
head_String[elems___] :> RowBox[{head, "[", RowBox[Riffle[{elems}, ","]], "]"}]}]
然后,我们需要:
Clear[MakeBoxesStopAlt];
MakeBoxesStopAlt /: MakeBoxes[MakeBoxesStopAlt[expr_], form_] := toBoxes[expr]
例如:
In[327]:= MakeBoxesStopAlt[Graphics[Disk[]]]
Out[327]= Graphics[Disk[List[0, 0]]]