2
$\begingroup$

When using TagBox with SyntaxForm, I noticed sometimes it is not well parsed by the FrontEnd. Here is a minimal example. First input

TagBox["f", "test", SyntaxForm -> "^"] // DisplayForm

Then copy the output f to new input, and do some prefix or postfix operations. There is an error message.

A screenshot is attached.

enter image description here

To debug further, one note that where there is a problem, in the saved file the cell is broken into more than one RowBox, for example:

Cell[BoxData[
 RowBox[{
  TagBox[
   TagBox["f",
    "test",
    SyntaxForm->"^"],
   DisplayForm], 
  RowBox[{"//", "ToBoxes", " ", 
   RowBox[{"(*", " ", 
    RowBox[{
    "this", " ", "f", " ", "is", " ", "copied", " ", "and", " ", "pasted", 
     " ", "from", " ", "the", " ", "above", " ", "line"}], " ", 
    "*)"}]}]}]], "Input",
 CellChangeTimes->{{3.618418507443947*^9, 3.618418516513115*^9}},
 EmphasizeSyntaxErrors->True]

I am using Mathematica 10.0 on Linux x64. I have also tried SyntaxForm->"a^b", or other operators. None of them work well.

Am I doing anything wrong or is it a bug?

Edit:

As @WReach commented, I should have provided more details about my goal, otherwise why not go without SyntaxForm? And here is what I wanted (still a simplified version of the realistic case): typeset derivative in a good-looking way.

The simplest implementation is

MakeBoxes[d[f_], StandardForm] := 
 TagBox[GridBox[{{"\[PartialD]", ToBoxes[f]}}], "d"]

However, if I evaluate

d[f]^2

∂ f²

This is not what I want (it evaluates correctly but does not look correct). Instead I want (∂ f)². Note that here I could use RowBox instead of GridBox and then there is no such problem. But I do need GridBox in the realistic case because it is more feature-rich (I could use RowBox[{GridBox[...], ""}] to replace GridBox[...] as a workaround. But I hope there is a cleaner and less hacky way). Also the Notation package is not powerful enough for my actual need so I want to do it myself.

I think the logical move to solve this problem is to use SyntaxForm. Indeed, if I define

MakeBoxes[d2[f_], StandardForm] := 
 TagBox[GridBox[{{"\[PartialD]", ToBoxes[f]}}], "d2", SyntaxForm -> "^"]

MakeExpression[TagBox[GridBox[{{"\[PartialD]", f_}}], "d2", ___], 
  StandardForm] := With[{g = ToExpression@f}, HoldComplete@d2@g]

I get the desired (∂ f)² appearance for d2[f]^2. However, now the problem that I have described appears. If I evaluate d2[f], and copy the output to new input and use, say, post-fix form of operators, it ends up with an error.

$\endgroup$
  • $\begingroup$ Still there in v10.0.2 $\endgroup$ – Yi Wang Dec 11 '14 at 17:09
1
$\begingroup$

Using a TemplateBox instead of a TagBox should fix your problem.

MakeBoxes[d[f_], StandardForm] ^:= TemplateBox[
    {MakeBoxes[f]},
    "d",
    DisplayFunction->(GridBox[{{"\[PartialD]",#}}]&),
    SyntaxForm->"^"
]

Then:

d[f]^2

enter image description here

And copying:

enter image description here

Power[d[f],2]

$\endgroup$

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.