4

The Pascal triangle for binomial coefficients starts with 1 in line 1 and goes on with 1 1 in line 2, 1 2 1 in line 3, and so on.

The answers to this question

Pascal's triangle in tikz

show how one may write up solutions in tikz.

Here is the adapted version which I prefer:

\begin{tikzpicture}[rotate=-90]
\foreach \x in {0,1,...,3}
{
    \foreach \y in {0,...,\x}
    {
        \pgfmathsetmacro\binom{factorial(\x)/(factorial(\y)*factorial(\x-\y))}
        \pgfmathsetmacro\shift{\x/2}
        \node[xshift=-\shift cm] at (\x,\y) {\pgfmathprintnumber\binom};
    }
}
\end{tikzpicture}

How do I write up a variant which has 0 in the first line, 1 in the second line, 1 1 in the third line and thereafter goes on as the usual Pascal's triangle?

JeT
  • 3,020

2 Answers2

7

You can just add

\node at (-1,0) {0};

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[rotate=-90] \node at (-1,0) {0}; \foreach \x in {0,1,...,3} { \foreach \y in {0,...,\x} { \pgfmathsetmacro\binom{factorial(\x)/(factorial(\y)*factorial(\x-\y))} \pgfmathsetmacro\shift{\x/2} \node[xshift=-\shift cm] at (\x,\y) {\pgfmathprintnumber\binom}; } } \end{tikzpicture}

\end{document}

Torbjørn T.
  • 206,688
5

Just a small alternative to @Torbjorn's answer (a trick he taught me here ) to avoid using (annoying IMHO, \pgfmathsetmacro), include the variables to evaluate directly in the loop.

More details in pfg manual v3.1.5.b p1003, section 89 Repeating Things: The Foreach Statement.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[rotate=-90] \node at (-1,0) {0}; \foreach \x in {0,1,...,3} { \foreach [evaluate ={ \binom = factorial(\x)/(factorial(\y)*factorial(\x-\y)); \shift = \x/2 ; }] \y in {0,...,\x} {\node[xshift=-\shift cm] at (\x,\y) {\pgfmathprintnumber\binom};} } \end{tikzpicture}

\end{document}

JeT
  • 3,020
  • +1 Where is documented evaluate ={...} as you used it in the manual? – AndréC Aug 02 '20 at 06:09
  • @AndréC PGF manual 3.1.5.b p. 1003. section 89 Repeating Things: The Foreach Statement – JeT Aug 02 '20 at 19:54
  • The problem is that it looks more like the syntax of the math library on page 704 since it says evaluate={...}. But you haven't loaded this library. And yet with the syntax of this library it works. The syntax of the foreach is not the same as the one you wrote. Maybe it's a syntax initiated on an old version of tikz? – AndréC Aug 02 '20 at 19:58
  • @AndréC Etrange ! Trying a \pgfversion, it tells me
    tikz.sty 2020/01/08 v3.1.5b (3.1.5b) pgf.sty 2020/01/08 v3.1.5b (3.1.5b) which does not seem too old.
    – JeT Aug 02 '20 at 20:03
  • No, it's not strange, there's a lot of code in the latest manual that comes from old manuals and is not documented. And since the code you used is not documented in this manual I wonder which manual it is documented in. It's classic look here: What do “-to” and “pre=moveto” do in TikZ arrows? – AndréC Aug 02 '20 at 20:10
  • @AndréC I use this one https://mirror.ibcp.fr/pub/CTAN/graphics/pgf/base/doc/pgfmanual.pdf.I must be missing something. – JeT Aug 02 '20 at 20:13
  • I too use the 3.1.5b manual, but it contains old code that is documented in older manuals. evaluate={...} is used since manual 3.0 but according to my research is not clearly documented in any manual! – AndréC Aug 02 '20 at 20:20