6
$\begingroup$

There appears to be a problem with a menu command Convert To / StandardForm (Crtl+Shift+N) with respect to InterpretationBox which contains TagBox (which is how Column/Grid and friends are typeset).

Here's an example:

obj /: MakeBoxes[obj[arg__], fmt_] := With[
  {appearance = MakeBoxes[Panel[Column[{arg}], "obj"], fmt]}
, InterpretationBox[appearance, obj[arg]]
]

obj[1, 2, 3, 4] 

enter image description here

% // InputForm

obj[1, 2, 3, 4]

So far everything is ok. But select the Input cell containing intial obj and use the menu command. This is what happens:

enter image description here

I find it unexpected because all round trips like

ToExpression@MakeBoxes@obj[1, 2, 3, 4]

work well.

Question: Is this expected? If yes, what would be a general fix to the typesetting idiom presented above?

Few workarounds are presented in: Make a custom object look like MatrixForm of a matrix?, where this issue came up. But they are not general enough.

The issue was noticed by xzczd and investigated by jkuczm but without any final conclusion.

$\endgroup$

1 Answer 1

7
$\begingroup$

This report finally found its way to me, and this is not a bug. I'm sorry support gave you incorrect information.

This has nothing to do with the particular boxes involved, but rather with your use of the typeset generators Panel and Column. Most typeset generators, Graphics expression, and certain other symbols have "conditional text formatting rules". Specifically, they check the value of BoxForm`UseTextFormattingQ when converting to a box from. This variable defaults to being True in input cells and False in output, though this can be changed somewhere in the options inspector. The theory is, if you did Convert To StandardForm on code (say, your MakeBoxes rule), you wouldn't necessarily want to go to a form where it can really be easily edited.

So, if you want to create a box structure that always uses box formatting, you can block the variable to False like so:

obj /: MakeBoxes[obj[arg__], fmt_] := With[
     {appearance = Block[
          {BoxForm`UseTextFormattingQ = False}, 
          MakeBoxes[Panel[Column[{arg}], "obj"], fmt]
     ]}, 
     InterpretationBox[appearance, obj[arg]]
]

Workarounds for existing structures would be to use Evaluate in Place rather than Convert To StandardForm (since many of the heads to use this have no evaluation semantics), or change the value of the option "UseTextFormattingWhenConvertingInput" to False.

$\endgroup$
2
  • $\begingroup$ The solution doesn't seem to work if I modify Panel[Column[{arg}], "obj"] to MatrixForm[{arg}]? $\endgroup$
    – xzczd
    Commented Jun 26, 2018 at 6:44
  • 1
    $\begingroup$ Grumble Grumble. Yes, the *Forms are special because of the wrapper stripping behavior. You can make it work by adding Internal`$ConvertForms = {} to the Block variable list. $\endgroup$ Commented Jun 26, 2018 at 23:52

Your Answer

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

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