Display a list or array as a centred triangle
You could do it like this:
arr = {{2, 1, 0, 0}, {1, 0, 0}, {0, 0}, {0}};
With[
{r = Riffle[#, ""] & /@ arr},
{l = Max[Length /@ r]},
Grid[CenterArray[#, l, ""] & /@ r]
]
CenterArray
is new in 11.0, but it should be easy to implement in earlier versions.
centered = Column[Row[#, Spacer @ 5] & /@ #, Alignment -> Center] & @ #;
centered @ arr
centered @ {{1111, 2, 3333, 4}, {a, bbbb, c}, {9, 8}}
or, perhaps, for cases like the second example, wrap each entry with Pane
with appropriate ImageSize
option value:
centered2 = Column[Row[Pane[#, {80, Automatic}, Alignment -> {Center, Center}] & /@ #,
Spacer[5]] & /@ #, Alignment -> Center] &;
centered2 @ {{1111, 2, 3333, 4}, {a, bbbb, c}, {9, 8}}
For those not using version 11, this works on 10.1, though it's a bit messier:
triangleDisplay[l_] := Module[{pl, rl},
pl = Table[{i, i + 2 j - 1}, {i, 1, Length[l]}, {j, 1, Length[l[[i]]]}];
rl = Flatten[Table[Rule[pl[[i, j]], l[[i, j]]], {i, 1, Length[l]}, {j, 1, Length[l[[i]]]}], 2];
Grid[Normal[SparseArray[rl, {Length[l], 2 Max[Length /@ l] + 1}, Null]]]
];