3
$\begingroup$

I lost some time trying to figure out why I got

f[]~MatchQ~f[]

False

Copy pasting or using // Hold // FullForm showed what the problem is

\!\(\*
TagBox[
RowBox[{"f", "[", "]"}],
HoldForm]\)~MatchQ~f[]

The lefthand side was copy-pasted from somewhere (probably a message) where it was displayed in HoldForm.

I would like such expressions to look differently.

In version 11 we finally got different styling of Initialization cells (gray background), not just a tiny "I" in the cell brace. Inactivated expressions also display differently. But HoldForm are visually indistinguishable from regular input.

Can I do something to change the formatting of HoldForm expressions? Which Format rules or stylesheets do I have to change to get this?

$\endgroup$
3
$\begingroup$

I'm using this now:

Unprotect@HoldForm;
Format[HoldForm[x_], 
   StandardForm] := {boxes = MakeBoxes@x, bg = LightBlue}~With~
   RawBoxes@
    InterpretationBox[
     TagBox[StyleBox[boxes, Background -> bg], "", 
      Selectable -> False], HoldForm[x]];
Protect@HoldForm;

The TagBox stops you from selecting anything inside the expression.

This makes the result clearer:

enter image description here

Another variant would be to just make HoldForm display directly like Hold, but the visual clutter might be unacceptable to you:

Format[HoldForm[x_], StandardForm] := 
  RawBoxes@TagBox[RowBox@{"HoldForm", "[", MakeBoxes@x, "]"}, "", 
    Selectable -> False];

(I left out the InterpretationBox here - TODO)

For reference, this is what HoldForm does by default:

Format[HoldForm[x_], StandardForm] := 
  RawBoxes@TagBox[MakeBoxes@x, HoldForm];
$\endgroup$
3
$\begingroup$

Nice idea. Your original construct did not play well with ReleaseHold though; here is one that should:

MakeBoxes[x_HoldForm, StandardForm] :=
  Block[{$holdstyle = True},
    Interpretation[
      Style[x, Background -> LightBlue, StripOnInput -> False, Selectable -> False],
      x
    ] // ToBoxes
  ] /; ! TrueQ[$holdstyle]
$\endgroup$
  • 1
    $\begingroup$ Thanks, I fixed a problem with ReleaseHold in the current version. $\endgroup$ – masterxilo Aug 30 '16 at 12:17
  • $\begingroup$ @masterxilo So I see. I guess then I'll emphasize the ability to use Style rather than StyleBox etc., making writing this just a little easier. $\endgroup$ – Mr.Wizard Aug 30 '16 at 12:19
  • $\begingroup$ I did not think of catching nested HoldForm, I guess your version will perform better in such cases. $\endgroup$ – masterxilo Aug 30 '16 at 12:19

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.