3
$\begingroup$

Suppose I want to display some expression, say foo[p1_,p2_], as a point in a circle.

I can achieve this using MakeBoxes, like in the following:

ClearAll[foo];
foo /: MakeBoxes[foo[p1_, p2_], fmt_] := 
 GraphicsBox[{CircleBox[{0, 0}], PointSize@0.08, PointBox[{p1, p2}]}, 
  ImageSize -> 70]

which produces the desired result:

enter image description here

However, a problem arises when I use ??foo:

enter image description here

As you can see, Mathematica is also issuing MakeBoxes on the foo[p1_,p2_] that should appear in the informations. Infact, the problem can be traced back to the fact that also the expression

foo[a_,b_]

triggers the MakeBoxes evaluation.

How can this problem be avoided?

PS: apologies for the not-much-informative title, please change it if you find a better way to describe the question.

$\endgroup$
  • 2
    $\begingroup$ Why not restrict the argument, since the display will only make sense for those arguments: MakeBoxes[foo[p1_?NumberQ, p2_?NumberQ], fmt_]? $\endgroup$ – J. M. will be back soon Feb 27 '16 at 1:35
2
$\begingroup$

I concur with the answer J.M. gave in a comment to the question, but would limit evaluation by a condition that constrain the coordinates given to lie on or within the unit circle.

ClearAll[foo];
foo /: MakeBoxes[foo[p1_, p2_] /; Norm[{p1, p2}] <= 1, form_] := 
  GraphicsBox[{CircleBox[{0, 0}], PointSize @ .08, PointBox[{p1, p2}]}, 
  ImageSize -> 70]

Testing with points along the ray through {1, 1}

foo[#, #] & /@ Range[0, 1, .5]

test1

Testing with points lying on the circle.

foo @@@ N[CirclePoints[5]]

test2

??foo

info

$\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.