Visualize Pascal's triangle and other triangle shaped lists

How can Pascal's triangle be visualised like this in Mathematica?

Or more generally, how can a 'triangular' list like

{{1},{1, 1}, {1, 2, 1}}


be visualized in this way.

Also I would like to do 'conditional things' like colouring the number two red.

-
We're out of red. Can we make it blue, please? –  stevenvh Aug 28 '12 at 13:57
But should Pascal's triangle be displayed like that, with each row center-aligned -- or would it be better to have all the rows left-aligned? In the latter form, it's often easier to calculate with it. That's an old insight by Ken Iverson; see, e.g.: jsoftware.com/jwiki/Essays/Pascal's%20Triangle –  murray Aug 28 '12 at 15:08
sjdh, and edit bumped this question and caused me to take another look at it. I am wondering, have you considered my answer? I know it came late but I feel that my method has a significant advantage over others posted. Will you take a look? –  Mr.Wizard Apr 15 '13 at 21:14

Here is another way:

pascalTriangle[n_] :=
NestList[{1, Sequence @@ Plus @@@ Partition[#, 2, 1], 1} &, {1},
n - 1];

Column[Grid[{#}, ItemSize -> 3] & /@ (pascalTriangle[7] /.
x_Integer :>
Text[Style[x, Large, If[x == 2, Red, Black]]]), Center]


-

t = Table[Binomial[n, k], {n, 0, 8}, {k, 0, n}]

(*out *)
{{1}, {1, 1}, {1, 2, 1}, {1, 3, 3, 1}, {1, 4, 6, 4, 1}, {1, 5, 10, 10, 5, 1},
{1, 6, 15, 20, 15, 6, 1}, {1, 7, 21, 35, 35, 21, 7, 1}, {1, 8, 28, 56, 70, 56, 28, 8, 1}}


You can format the list as a matrix:

MatrixForm[t]


You can insert tabs between the items and print each row, literally as a Row, in a grid.

{Row[#, "\t"]} & /@ t // Grid


By using Row, we are sending Grid one item for each gridrow. (If Row were not employed, Grid would treat each item of a sublist as requiring its own column. That will lead to a triangle skewed from left to right. )

Here are a couple of ideas on how to style the 2 as large and red. The following will make any entry of 2 red (it will not color the 2 in 20).

t1 = Table[Binomial[n, k], {n, 0, 8}, {k, 0, n}] /. {2 -> Style[2, Red, 18]}


You may then format t1 as a matrix or as a grid.

The following makes the 2 in the center of a sublist with 3 elements red. It's not necessary to do this because 2 only shows up once in Pascal's triangle. But you get the idea...

t2 = Table[Binomial[n, k], {n, 0, 8}, {k, 0, n}] /. {a_, 2, c_} :> {a, Style[2, Red, 18], c}

-
Thank you. From your examples I learned a lot. –  sjdh Aug 28 '12 at 15:06
Glad to be of help. –  David Carraher Aug 28 '12 at 15:09

My modest attempt:

With[{n = 7},
Graphics[Table[Text[Style[Binomial[n - j, n - i], Large], {Sqrt[3] (i - j/2), 3 j/2}],
{i, n}, {j, i}]]]


Here's a more general function:

triangularArrayLayout[triArray_List, opts___] := Module[{n = Length[triArray]},
Graphics[MapIndexed[
Text[Style[#1, Large], {Sqrt[3] (n - 1 + #2.{-1, 2}), 3 (n - First[#2] + 1)}/2] &,
triArray, {2}], opts]]


Use it on the Stirling subset numbers $\left\{{n \atop k}\right\}$:

triangularArrayLayout[Table[StirlingS2[n, k], {n, 0, 5}, {k, 0, n}]]


-
How can I control the horizontal spacing between elements within a row here? (I tried using this method for something else and everything worked fine except the entries are quite cramped). –  JohnD Dec 3 '12 at 20:47
@John, tweaking ImageSize might help here. –  Ｊ. Ｍ. Apr 15 '13 at 17:35

I like the following way of visualizing the Pascal triangle:

PascalTriangleForm[li : {{_}, {_, _}, __List}] /;
(Length /@ li) == Range[Length[li]] :=
Level[Table[Table[{i, n + 1 - i}, {i, 1, n}], {n,Length[li]}], {2}] ->
Level[li, {2}]
], Automatic, ""]]


-
Thanks for your answer. Is there any particular reason you like this representation? –  sjdh Aug 29 '12 at 8:03
@sjdh This is the way the Pascal triangle was first historically arranged. I have seen it in this book by Hald. –  Sasha Aug 29 '12 at 12:21

This is my favorite way to draw Pascal's triangle.

pt = NestList[{0, ##} + {##, 0} & @@ # &, {1}, #] &;

ptform[pt : {_List ..}] :=
With[{n = Length@Last@pt, long = Max@Map[StringLength@ToString@# &, pt, {2}]},
Graphics[MapIndexed[Text[#, {#2 - #/2, -#} & @@ #2] &, pt, {2}],
PlotRange -> All, AspectRatio -> 0.7,
BaseStyle -> FontSize -> Scaled[1.5/(n long)]]]


The advantage here is that the produced graphic is resizable, and the font size is automatically selected. Elements can be styled either with /. or MapAt.

pt@7 /. 2 -> Style[2, Red] // ptform


MapAt[Style[#, Red] &, pt@11, {{5, 3}, {6, 4}, {7, 1}}] // ptform


-

This is actually demonstrated on the Wolfram Demonstrations Project pages. Download the notebook!

-
@sjdh and just so you know: I didn't happen to know that it was part of the Wolfram Demonstrations. I used a search engine to find that out. –  F'x Aug 28 '12 at 14:00
Also discussed here, if you scroll down (with a nice Manipulate). –  programming_historian Aug 28 '12 at 14:12
You noticed, no doubt, that the code makes use of CellularAutomaton. –  David Carraher Aug 28 '12 at 14:20
@F'x This Wolfram demonstration I have seen before, I'm sure there are simpler ways to draw Pascal's triangle that can be used to visualise any list with a triangle shape. –  sjdh Aug 28 '12 at 14:29