12
$\begingroup$

Is there a way with MathJaX (on math.stackexchange.com) of displaying, say, Pascal's Triangle or some similar triangular stack of numbers, such as the Stirling Numbers of the First Kind, without resorting to array or matrix tricks to do it? I can't directly demonstrate what I tried, because MathJaX doesn't work on this forum, but I've posted a comment on math.stackexchange.com in which I tried to do it with code similar to this:

$$\begin{matrix}
&&&&&1\\
&&&&1&&1\\
&&&1&&3&&2\\
&&1&&6&&11&&6\\
&1&&10&&35&&50&&24\\
1&&15&&85&&225&&274&&120
\end{matrix}$$

which renders like this:

Mirror Image Unsigned Stirling Triangle of the First Kind

As you can see, the bigger the numbers get, the farther apart they are spaced, and so far the table is already beginning to look a little asymmetric! I've had better success with HTML tables, but table tags and their helpers aren't permitted on StackExchange sites. Is there a better way to do this on your Math forums? Is this even the right community for this question?

Edit: Here is a screenshot showing the lower right corner of the HTML table, so you can get a feel for the look I want:

One corner of the HTML table

Update: After trying several different approaches, I think I may have found a (fairly) workable solution, although it still has its difficulties: $$\newcommand\cn[2]{\llap{#1}\rlap{#2}\,} \begin{array}{c} &&&&&&\cn{1}{}\\ &&&&&\cn{1}{}&\cn{}{}&\cn{1}{}\\ &&&&\cn{1}{}&\cn{}{}&\cn{3}{}&\cn{}{}&\cn{2}{}\\ &&&\cn{1}{}&\cn{}{}&\cn{6}{}&\cn{}{}&\cn{1}{1}&\cn{}{}&\cn{6}{}\\ &&\cn{1}{}&\cn{}{}&\cn{1}{0}&\cn{}{}&\cn{3}{5}&\cn{}{}&\cn{5}{0}&\cn{}{}&\cn{2}{4}\\ &\cn{1}{}&\cn{}{}&\cn{1}{5}&\cn{}{}&\cn{8}{5}&\cn{}{}&\cn{22}{5}&\cn{}{}&\cn{27}{4}&\cn{}{}&\cn{12}{0}\\ \cn{1}{}&\cn{}{}&\cn{2}{1}&\cn{}{}&\cn{17}{5}&\cn{}{}&\cn{73}{5}&\cn{}{}&\cn{16}{24}&\cn{}{}&\cn{17}{64}&\cn{}{}&\cn{72}{0} \end{array}$$ The code is:

$$\newcommand\cn[2]{\llap{#1}\rlap{#2}\,}
\begin{array}{c}
&&&&&&\cn{1}{}\\
&&&&&\cn{1}{}&\cn{}{}&\cn{1}{}\\
&&&&\cn{1}{}&\cn{}{}&\cn{3}{}&\cn{}{}&\cn{2}{}\\
&&&\cn{1}{}&\cn{}{}&\cn{6}{}&\cn{}{}&\cn{1}{1}&\cn{}{}&\cn{6}{}\\
&&\cn{1}{}&\cn{}{}&\cn{1}{0}&\cn{}{}&\cn{3}{5}&\cn{}{}&\cn{5}{0}&\cn{}{}&\cn{2}{4}\\
&\cn{1}{}&\cn{}{}&\cn{1}{5}&\cn{}{}&\cn{8}{5}&\cn{}{}&\cn{22}{5}&\cn{}{}&\cn{27}{4}&\cn{}{}&\cn{12}{0}\\
\cn{1}{}&\cn{}{}&\cn{2}{1}&\cn{}{}&\cn{17}{5}&\cn{}{}&\cn{73}{5}&\cn{}{}&\cn{16}{24}&\cn{}{}&\cn{17}{64}&\cn{}{}&\cn{72}{0}
\end{array}$$

I used array instead of matrix, and I split each cell into two halves: the left side of each number goes to \llap and the right side goes to rlap. And because it's an array, the table expands to fit the data. The numbers are (sort of) centered now--at least, more so than with a single left or right alignment.

$\endgroup$

migrated from meta.stackoverflow.com Feb 5 '14 at 0:35

This question came from our discussion, support, and feature requests site for professional and enthusiast programmers.

  • $\begingroup$ Google search for "pascal triangle" latex returns for example this. You can try if it is any good. (Or maybe some other results from that search.) It seems that most posts containing the phrase Pascal's triangle on TeX.SE are about TikZ. $\endgroup$ – Martin Sleziak Feb 5 '14 at 7:31
  • $\begingroup$ @MartinSleziak, I don't think \begin{tabular}...\end{tabular} is currently supported by MathJaX. Maybe I could load it. What's the extension name? $\endgroup$ – Brian J. Fink Feb 5 '14 at 21:01
  • $\begingroup$ AFAIK tabular is not supported. But you can try array instead. For more on using tables on this site see: meta.math.stackexchange.com/questions/5020/…, meta.math.stackexchange.com/questions/4240/… and meta.math.stackexchange.com/questions/6734/… $\endgroup$ – Martin Sleziak Feb 6 '14 at 7:33
  • $\begingroup$ Here's another method. it uses two halves and a center. Right-click for source code. $$\newcommand\cn[3]{\llap{#1}#2\rlap{#3}} \begin{array}{c} &&&&&&\cn{}{1}{}\\ &&&&&\cn{}{1}{}&&\cn{}{1}{}\\ &&&&\cn{}{1}{}&&\cn{}{3}{}&&\cn{}{2}{}\\ &&&\cn{}{1}{}&&\cn{}{6}{}&&\cn{1}{}{1}&&\cn{}{6}{}\\ &&\cn{}{1}{}&&\cn{1}{}{0}&&\cn{3}{}{5}&&\cn{5}{}{0}&&\cn{2}{}{4}\\ &\cn{}{1}{}&&\cn{1}{}{5}&&\cn{8}{}{5}&&\cn{2}{2}{5}&&\cn{2}{7}{4}&&\cn{1}{2}{0}\\ \cn{}{1}{}&&\cn{2}{}{1}&&\cn{1}{7}{5}&&\cn{7}{3}{5}&&\cn{16}{}{24}&&\cn{17}{}{64}&&\cn{7}{2}{0} \end{array}$$ $\endgroup$ – Brian J. Fink Feb 6 '14 at 19:20
  • $\begingroup$ Here's how it looks with Pascal's Triangle: $$\newcommand\cn[3]{\llap{#1}#2\rlap{#3}} \begin{array}{c} &&&&&&\cn{}{1}{}\\ &&&&&\cn{}{1}{}&&\cn{}{1}{}\\ &&&&\cn{}{1}{}&&\cn{}{2}{}&&\cn{}{1}{}\\ &&&\cn{}{1}{}&&\cn{}{3}{}&&\cn{}{3}{}&&\cn{}{1}{}\\ &&\cn{}{1}{}&&\cn{}{4}{}&&\cn{}{6}{}&&\cn{}{4}{}&&\cn{}{1}{}\\ &\cn{}{1}{}&&\cn{}{5}{}&&\cn{1}{}{0}&&\cn{1}{}{0}&&\cn{}{5}{}&&\cn{}{1}{}\\ \cn{}{1}{}&&\cn{}{6}{}&&\cn{1}{}{5}&&\cn{2}{}{0}&&\cn{1}{}{5}&&\cn{}{6}{}&&\cn{}{1}{} \end{array}$$ $\endgroup$ – Brian J. Fink Feb 6 '14 at 19:38
  • $\begingroup$ (If only there were a way to collapse the entire number, instead of every digit except the one in the center!) $\endgroup$ – Brian J. Fink Feb 6 '14 at 19:48
  • $\begingroup$ Here it's more compact. I added \hspace to cancel center width: $$\newcommand\scollapse[2]{\hspace{-#1pt}#2\hspace{-#1pt}} \newcommand\cn[3]{\scollapse{1.5}{\llap{#1}#2\rlap{#3}}} \begin{array}{c} &&&&&&\cn{}{1}{}\\ &&&&&\cn{}{1}{}&&\cn{}{1}{}\\ &&&&\cn{}{1}{}&&\cn{}{3}{}&&\cn{}{2}{}\\ &&&\cn{}{1}{}&&\cn{}{6}{}&&\cn{1}{}{1}&&\cn{}{6}{}\\ &&\cn{}{1}{}&&\cn{1}{}{0}&&\cn{3}{}{5}&&\cn{5}{}{0}&&\cn{2}{}{4}\\ &\cn{}{1}{}&&\cn{1}{}{5}&&\cn{8}{}{5}&&\cn{2}{2}{5}&&\cn{2}{7}{4}&&\cn{1}{2}{0}\\ \cn{}{1}{}&&\cn{2}{}{1}&&\cn{1}{7}{5}&&\cn{7}{3}{5}&&\cn{16}{}{24}&&\cn{17}{}{64}&&\cn{7}{2}{0} \end{array}$$ $\endgroup$ – Brian J. Fink Feb 6 '14 at 20:47
8
$\begingroup$

An alternative method is to force each column to be the same width, with the width determined by the widest element in your array.

This can be accomplished by using \phantom to introduce an unseen text of the requisite width, and then printing the numbers overlaid on it using \rlap or \llap (ideally one would use \clap to make it perfectly aligned, but that is not available [even in usual LaTeX without loading additional packages which implements it] so the end result has some minor misalignments).

The code:

\newcommand\pad[1]{\rlap{#1}\phantom{274}}
\begin{matrix}
    &&&&&1\\
    &&&&1&&1\\
    &&&1&&3&&2\\
    &&1&&6&&11&&6\\
    &\pad{1}&&\pad{10}&&\pad{35}&&\pad{50}&&\pad{24}\\
    \pad1&&\pad{15}&&\pad{85}&&\pad{225}&&\pad{274}&&\pad{120}
    \end{matrix}

I defined a new command called \pad which takes one argument: the argument that you want to display. The number 274 is taken from your matrix below where it is the widest element displayed. It is only necessary to pad one element in each column (since everything else is supposed to be narrower originally).

$$\newcommand\pad[1]{\rlap{#1}\phantom{274}} \begin{matrix} &&&&&1\\ &&&&1&&1\\ &&&1&&3&&2\\ &&1&&6&&11&&6\\ &\pad{1}&&\pad{10}&&\pad{35}&&\pad{50}&&\pad{24}\\ \pad1&&\pad{15}&&\pad{85}&&\pad{225}&&\pad{274}&&\pad{120} \end{matrix} $$

If the spacing by the widest element is too large, one can use a smaller number of digits to set the alignment, but that will require using the \pad command also on any appearance of numbers "longer" than the reference number. Here I swapped the 274 reference by 50:

$$\newcommand\pad[1]{\rlap{#1}\phantom{50}} \begin{matrix} &&&&&1\\ &&&&1&&1\\ &&&1&&3&&2\\ &&1&&6&&11&&6\\ &\pad{1}&&\pad{10}&&\pad{35}&&\pad{50}&&\pad{24}\\ \pad1&&\pad{15}&&\pad{85}&&\pad{225}&&\pad{274}&&\pad{120} \end{matrix} $$

The alignment will get a little bit worse with this (again, how I wish I have a \clap available.)

$\endgroup$
  • $\begingroup$ The MathJaX engine on math.stackexchange.com supports \require. I've already used it in another question to load the cancel extension. What's the name of the extension that supports \clap? $\endgroup$ – Brian J. Fink Feb 5 '14 at 18:34
  • 1
    $\begingroup$ How about this code? It takes cues from you @robjohn. $$\newcommand\pad[1]{\hspace{-18pt}\llap{#1}\phantom{1764}} \hspace{18pt}\begin{matrix} &&&&&&\pad{1}\\ &&&&&\pad{1}&\pad{}&\pad{1}\\ &&&&\pad{1}&\pad{}&\pad{3}&\pad{}&\pad{2}\\ &&&\pad{1}&\pad{}&\pad{6}&\pad{}&\pad{11}&\pad{}&\pad{6}\\ &&\pad{1}&\pad{}&\pad{10}&\pad{}&\pad{35}&\pad{}&\pad{50}&\pad{}&\pad{24}\\ &\pad{1}&\pad{}&\pad{15}&\pad{}&\pad{85}&\pad{}&\pad{225}&\pad{}&\pad{274}&\pad{}&\pad{120}\\ \pad{1}&\pad{}&\pad{21}&\pad{}&\pad{175}&\pad{}&\pad{735}&\pad{}&\pad{1624}&\pad{}&\pad{1764}&\pad{}&\pad{720} \end{matrix}$$ $\endgroup$ – Brian J. Fink Feb 5 '14 at 20:31
  • $\begingroup$ It renders: $$\newcommand\pad[1]{\hspace{-18pt}\llap{#1}\phantom{1764}} \hspace{18pt}\begin{matrix} &&&&&&\pad{1}\\ &&&&&\pad{1}&\pad{}&\pad{1}\\ &&&&\pad{1}&\pad{}&\pad{3}&\pad{}&\pad{2}\\ &&&\pad{1}&\pad{}&\pad{6}&\pad{}&\pad{11}&\pad{}&\pad{6}\\ &&\pad{1}&\pad{}&\pad{10}&\pad{}&\pad{35}&\pad{}&\pad{50}&\pad{}&\pad{24}\\ &\pad{1}&\pad{}&\pad{15}&\pad{}&\pad{85}&\pad{}&\pad{225}&\pad{}&\pad{274}&\pad{}&\pad{120}\\ \pad{1}&\pad{}&\pad{21}&\pad{}&\pad{175}&\pad{}&\pad{735}&\pad{}&\pad{1624}&\pad{}&\pad{1764}&\pad{}&\pad{720} \end{matrix}$$ $\endgroup$ – Brian J. Fink Feb 5 '14 at 20:32
  • $\begingroup$ @BrianJ.Fink: one of the reasons that I posted my answer was so that one doesn't have to pad everywhere (simplify typing). Your comment quite thoroughly destroyed that. :-) Also look down the middle column, the alignment is not great: all the numbers are right adjusted. As to your first comment, the \mathclap command appears in the mathtools LaTeX package, to which I do not think exist a MathJax equivalent. $\endgroup$ – Willie Wong Feb 6 '14 at 8:31
6
$\begingroup$

If the site honored all of the array column specs, things could be made a bit better. Unfortunately, the only thing you might try is to add some negative \hspace. For example: $$ \begin{array}{c} &&&&&\hspace{-6pt}1\\ &&&&\hspace{-6pt}1\hspace{-6pt}&&\hspace{-6pt}1\\ &&&\hspace{-6pt}1&&\hspace{-6pt}3&&\hspace{-6pt}2\\ &&\hspace{-6pt}1&&\hspace{-6pt}6&&\hspace{-6pt}11&&\hspace{-6pt}6\\ &\hspace{-6pt}1&&\hspace{-6pt}10&&\hspace{-6pt}35&&\hspace{-6pt}50&&\hspace{-6pt}24\\ \hspace{-6pt}1&&\hspace{-6pt}15&&\hspace{-6pt}85&&\hspace{-6pt}225&&\hspace{-6pt}274&&\hspace{-6pt}120 \end{array} $$ Unfortunately, this is still a bit asymmetric.

$\endgroup$
  • 4
    $\begingroup$ We might improve the symmetry by using varying amounts of \hspace. $\endgroup$ – robjohn Feb 5 '14 at 1:26
  • $\begingroup$ I've updated my question with another screenshot. The numbers in each row are so close together that they cut in underneath the numbers above them. That's the look I'm going for. $\endgroup$ – Brian J. Fink Feb 5 '14 at 3:51
  • 2
    $\begingroup$ @BrianJ.Fink If you want to enlarge vertical spaces, you can use something like \\[5pt] at the end of each line. See MathJax vertical space + too thick fraction lines at TeX.SE. $\endgroup$ – Martin Sleziak Feb 5 '14 at 8:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .