# How to change styling of HoldForm expressions?

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?

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:

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];


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]

• Thanks, I fixed a problem with ReleaseHold in the current version. – masterxilo Aug 30 '16 at 12:17
• @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. – Mr.Wizard Aug 30 '16 at 12:19
• I did not think of catching nested HoldForm, I guess your version will perform better in such cases. – masterxilo Aug 30 '16 at 12:19