11
$\begingroup$

How can I get Mma to display an array like {{2, 1, 0, 0}, {1, 0, 0}, {0, 0}, {0}} as an upside down triangle where the first row is the base and the last row is the tip, with each row centred something like (although the centering here is not perfect):

enter image description here

In this specific examples my array has lots of 0's but in general this will not be the case.

Moreover, the general array would contain $n$ rows, with row 1 containing $n$ elements, row 2 containing $n-1$ elements etc. and I would like to display this as a triangle where each row is centred w/r to the first row, generalizing the figure above.

[For context these arrays are Gelfand-Tsetlin patterns.]

$\endgroup$
10
$\begingroup$

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

enter image description here

CenterArray is new in 11.0, but it should be easy to implement in earlier versions.

$\endgroup$
  • $\begingroup$ So simple when you ask an expert. Thanks. $\endgroup$ – ZeroTheHero Oct 17 '17 at 19:45
6
$\begingroup$
centered = Column[Row[#, Spacer @ 5] & /@ #, Alignment -> Center] & @ #;

centered @ arr

enter image description here

centered @ {{1111, 2, 3333, 4}, {a, bbbb, c}, {9, 8}}

enter image description here

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}}

enter image description here

$\endgroup$
5
$\begingroup$

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]]]
];
$\endgroup$
  • $\begingroup$ @ZeroTheHero: There's a scroll bar beneath it, and it should copy fine. kglr's answer is cleaner though, unless you want to make sure that none of the elements overlap. $\endgroup$ – eyorble Oct 17 '17 at 21:14
  • $\begingroup$ Hmmm... I can scroll but do not see a scrollbar. Well: it works and that what matter. Thanks! $\endgroup$ – ZeroTheHero Oct 17 '17 at 21:19
  • $\begingroup$ It's an iPad thing... the scrollbar clearly appears on the laptop. I really like your solution as a module. Thanks. $\endgroup$ – ZeroTheHero Oct 18 '17 at 3:38
5
$\begingroup$

Here is a variation of @Szabolcs' solution that allows elements to overlap:

Options[triangleGrid] = Options[Grid];

triangleGrid[a_List, opts:OptionsPattern[]] := With[{len = 2 Length[a[[1]]]},
    Grid[
        CenterArray[
            Riffle[#, SpanFromLeft, {2, 2 Length[#], 2}],
            len,
            ""
        ]& /@ a,
        opts
    ]
]

For your example:

triangleGrid[{{2,1,0,0}, {1,0,0}, {0,0}, {0}}]

enter image description here

When the widths of the elements are not the same:

triangleGrid[{{1111,2,3333,4}, {a,bbbb,c}, {9,8}}]

enter image description here

Note how 3333 and bbbb overlap. Also, in the above, the individual columns are all sized differently. You can make them the same with:

triangleGrid[{{1111,2,3333,4}, {a,bbbb,c}, {9,8}}, ItemSize->1]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.