I have some rows I want to put into a Grid
:
rows = {{a, "foo"}, {b, "bar"}, {c, SpanFromAbove},
{d, "baz"}, {e, SpanFromAbove}, {f, SpanFromAbove},
{g, "quux"}};
In order to make it easier to read, I want to use alternating background colors, which I tried to do the standard way:
Grid[rows, Alignment -> {{Automatic, Left}, Center},
Background -> {Automatic, {{LightYellow, LightBlue}}}]
However, what I get is this ugly mess:
What I want looks like this:
In order to get that, though, I had to resort to a rather convoluted solution that explicitly inserts Item
s around elements to color them.
Module[{except = Except[SpanFromAbove],
colors = {LightYellow, LightBlue}, split, item},
split = SequenceCases[rows,
seq:{{_, except}, {_, SpanFromAbove}...} :>
Map[Replace[x:except :> item[x]], seq, {2}]];
Grid[Catenate[MapThread[
#1 /. item[x_] :> Item[x, Background -> #2] & ,
{split, PadRight[{}, Length[split], colors]}]],
Alignment -> {{Automatic, Left}, Center}]]
I could probably make this a bit less complicated with some elbow grease, but it would be really nice if I could somehow tell grid to have the colors alternate based on the spanned groups of rows and let it do the hard work.
Is there a way to do that?
Failing that, is there a really slick way to get the Item
s where I want them to go?