3

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?

masterxilo
  • 5,739
  • 17
  • 39

2 Answers2

3

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];
masterxilo
  • 5,739
  • 17
  • 39
3

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]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371