# Prevent Grid from overriding GridBoxItemSize from Stylesheet

I have problems getting the Stylesheet settings for GridBoxOptions/GridBoxItemSize to be applied to Grid expressions. Consider the following example:

(* Create a new Stylesheet with a GridTest style that sets the column widths of GridBox *)
SetOptions[
EvaluationNotebook[], StyleDefinitions -> Notebook[
{
Cell[
StyleData["GridTest"],
GridBoxOptions -> {
GridBoxItemSize -> {"Columns" -> {Scaled@0.7, {Scaled@0.3}}}
}
]
}
]
]

(* Create a cell containing a Grid expression *)
CellPrint@Cell[BoxData@ToBoxes@Grid[{{"a", "b"}}], "GridTest"]


(* Manually create the GridBox *)
CellPrint@Cell[BoxData@GridBox@{{"a", "b"}}, "GridTest"]


As you can see, the setting from the Stylesheet is only applied to the case where the GridBox is manually built. Inspecting the box structure of the automatically generated GridBox reveals the problem:

ToBoxes@Grid[{{"a", "b"}}]
(* TagBox[
GridBox[
{{"\"a\"", "\"b\""}},
AutoDelete -> False,
GridBoxItemSize -> {"Columns" -> {{Automatic}}, "Rows" -> {{Automatic}}}
],
"Grid"
] *)


Apparently, ToBoxes automatically adds a default for GridBoxItemSize, which obviously overrides the Stylesheet default.

The question is now: How can I convert a Grid expression into a box structure such that the Stylesheet options are not randomly overridden?

I've tried to specify things like ItemSize->{} or ItemSize->Inherited, but they do not work. There seems to be another issue that causes ToBoxes to replace ItemSize->Inherited with GridBoxItemSize->{}, rather than GridBoxItemSize->Inherited (which would behave properly).

The reason why I need this: I need to generate a cell containing a GridBox from a Grid expression with user defined option values and have it respect the stylesheet defaults. Since the mapping from Grid options to GridBoxOptions is not documented, I can't ask the user to provide the GridBox options directly. Of course, I could manually perform the mapping or try to detect which options need to be deleted, this feels hacky and partly relies on undocumented behavior.

### Intro

It is not an isolated problem, similar limitation affects e.g. Button's Appearance. And it is internally considered a minor bug, read more in comments to that answer:

Evaluation leak from Dynamic in Button's action

### Solution

Summing up, I don't think there is an easy way. You either have to modify built-in GridDumpGridToBoxes definitions or you need to insert GridBoxItemSize -> Inherited to your Grids. E.g.:

Grid[{{"a", "b"}}
, GridBoxItemSize -> Inherited,  BaseStyle -> {"GridTest"}
]


### Grumble

That obviously makes coding annoying so one (I) ends up with myGrid and myButton aliases where I put every tweak I need.

But then why to use stylesheets? Now you split Grid/Button's properties on a stylesheet and myButton definition, it is not a good coding practice.

At the end I only use stylesheets for cell/notebook related styles/actions and only in case where multiple notebooks. And it is a pity as it is only a consequence of minor issues not a fundamental limitation of them.

• Thank you for the link! And thanks for the answer of course ;). That specifying GridBoxItemSize works this way is a bit surprising (I must have overlooked it when first looking at the GridToBoxes code...), but it seems to work without issues, even if the user specifies a custom ItemSize`. – Lukas Lang Jul 30 '18 at 10:53