1
$\begingroup$

I tried to use a matrix method to write Pascal's triangle using MathJax, using the code;

$$
\begin{matrix}
1 \\
1 & 1 \\
1 & 2 & 1 \\
1 & 3 & 3 & 1 \\
1 & 4 & 6 & 4 & 1 \\
\end{matrix}
$$

but results the following:

$$ \begin{matrix} 1 \\ 1 & 1 \\ 1 & 2 & 1 \\ 1 & 3 & 3 & 1 \\ 1 & 4 & 6 & 4 & 1 \\ \end{matrix} $$

which as you see, is not centered like in this image for example;

enter image description here

So what is the correct method to write Pascal's triangle using MathJax?

$\endgroup$
1

2 Answers 2

7
$\begingroup$

With something as simple as

$$
1\\
1\quad 1\\
1\quad 2\quad 1\\
1\quad 3\quad 3\quad 1\\
1\quad 4\quad 6\quad 4\quad 1\\
1\quad 5 \quad 10\quad 10 \quad 5\quad 1
$$

You get $$ 1\\ 1\quad 1\\ 1\quad 2\quad 1\\ 1\quad 3\quad 3\quad 1\\ 1\quad 4\quad 6\quad 4\quad 1\\ 1\quad 5 \quad 10 \quad 10\quad 5\quad 1 $$

The \quad is a quad of space. You can make bigger space by using \qquad; the \\ marks an end of line. Displayed math equations are always centered in each line if there are no alignment characters or commands.

With extra space (using \qquad instead of \quad): $$ 1\\ 1\qquad 1\\ 1\qquad 2\qquad 1\\ 1\qquad 3\qquad 3\qquad 1\\ 1\qquad 4\qquad 6\qquad 4\qquad 1\\ 1\qquad 5\qquad 10\qquad 10\qquad 5\qquad 1 $$ Not perfect, but reasonably good. You could tweak it by hand with some negative spaces, but it would be more work than it's worth. Other solutions are described in the post linked to by Peter Phipps in the comments.

$\endgroup$
1
  • $\begingroup$ +1 for introducing me to \quad. $\endgroup$
    – JonathanZ
    Commented Nov 6, 2022 at 22:48
4
$\begingroup$

An option is to use an array environment with all the columns centered and odd/even numbered columns empty on alternate rows. If you do this, there will be several ampersands :-). Also, to make the spacing perfect you need to make sure that all the columns will have the same width. You can use well placed \hphantom{99} entries on unoccupied slots in the table to make this happen (once per column suffices).

\begin{array}{ccccccccccccc} &&&&&&1\\ &&&&&1&&1\\ &&&&1&&2&&1\\ &&&1&&3&&3&&1\\ &&1&&4&&6&&4&&1\\ \hphantom{99}&1&\hphantom{99}&5&&10&&10&&5&\hphantom{99}&1&\hphantom{99}\\ 1&\hphantom{99}&6&\hphantom{99}&15&&20&&15&\hphantom{99}&6&\hphantom{99}&1 \end{array}

I could not make this trick from TeX.StackExchange work here (undoubtedly it works in LaTeX). May be MathJax does not support \settowidth in the array preamble, or I just couldn't figure it out.

My MathJax source below. I have horizontal phantoms on the first four and the last four columns. If you add enough rows to get triple digit entries, then you need more phantoms.

\begin{array}{ccccccccccccc}
&&&&&&1\\
&&&&&1&&1\\
&&&&1&&2&&1\\
&&&1&&3&&3&&1\\
&&1&&4&&6&&4&&1\\
\hphantom{99}&1&\hphantom{99}&5&&10&&10&&5&\hphantom{99}&1&\hphantom{99}\\
1&\hphantom{99}&6&\hphantom{99}&15&&20&&15&\hphantom{99}&6&\hphantom{99}&1
\end{array}

Not sure you want to go to this much trouble :-)

$\endgroup$
6
  • 1
    $\begingroup$ With the recent worldwide shortages of ampersands, and the associated increases in their price, this solution is less practical than it once was. $\endgroup$
    – MJD
    Commented Nov 7, 2022 at 17:17
  • $\begingroup$ LOL @MJD. This triangle is too wide for my taste (and I'm sure I'm not the only one). A way to reduce the space between the columns would make it better, but I don't know how to do that. $\endgroup$ Commented Nov 7, 2022 at 18:23
  • 1
    $\begingroup$ The additional space between columns is mainly why I avoided the array environment, but my solution stops being visually pleasing once you get to rows that have much wider entries... In regular $\LaTeX$ I would use the array package to allow me to control the inter-column space much more precisely and make it look more balanced, but that's more work than it is worth for MathJax and the site, to be honest. $\endgroup$ Commented Nov 7, 2022 at 18:25
  • $\begingroup$ +1. I think this answer is best one here. What makes this answer special, is that the lines are not broken. The other answer looks broken on the phone. $\endgroup$
    – User
    Commented Nov 7, 2022 at 19:01
  • 1
    $\begingroup$ No argument, @ArturoMagidin. I quite understand why one would not want to spend so much time on this here. May depend on how many rows you want to write, I guess. At some point the disturbance from multidigit entries may become disturbing enough. $\endgroup$ Commented Nov 7, 2022 at 19:05
  • 1
    $\begingroup$ @User Thanks for your support. Do check out the linked threads for more ideas! I somewhat regret posting this, but I'm also fond of using those phantoms to adjust spacing. With a modest number of rows I would rather make the columns with multidigit numbers narrower as opposed to making the single digit columns wider (which is what I did here). $\endgroup$ Commented Nov 7, 2022 at 19:08

You must log in to answer this question.

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