Triangle de Pascal

Bonjour,
J’aimerais reproduire les 2 fichiers joints mais je n’y arrive pas. Merci aux bonnes âmes de m’aider.96008
96010

Réponses

  • Il me semble que les matrices TikZ sont bien indiquées ici. Avec une tikzpicture, toutes les annotations autour de la matrice sont automatiquement prises en compte pour la détermination de la bounding box de la figure. Voici une façon de réaliser la première figure. Il faudrait peut-être songer à lire la documentation disponible sur TikZ (par exemple ici) et s'entraîner en commençant par des choses plus simples.96064
  • Pour la deuxième figure, il faudrait peut-être que tu t'entraînes. Je te propose de lire la documentation de TikZ sur les matrices(*) et de réaliser la partie en noir, à l'exception de la cellule en haut à gauche si tu ne sais pas faire (indices : \raisebox + \hspace ou \kern pour le $n$ et le $k$). Une fois ceci fait, on t'aidera pour le reste, si besoin. En plus de la documentation que je viens de citer, tu peux trouver des exemples simples de matrices TikZ ici (voir à la fin du document .tex — le début utilise nicematrix, c'est assez différent des matrices TikZ).

    (*) Deux chapitres : Matrices and Alignment dans la partie III, TikZ ist kein Zeichenprogramm et Matrix Library dans la partie V, Libraries.
  • Merci. Je vais essayer de potasser les liens pour la 2nde figure.
  • Voilà, je n’arrive pas a faire mieux que cela:
    \documentclass{article}
    \usepackage{xparse}
    \usepackage{etoolbox}
    \usepackage{tikz}
    \usetikzlibrary{arrows.meta, decorations.pathreplacing, calligraphy,
                    matrix, patterns}
    
    \begin{document}
    
    \begin{tikzpicture}
    \matrix [matrix of nodes]
    {
    $n k$ & $0$ & $1$ & $2$ & $3$ & $4$ & $5$ & $\cdots$\\
    $0$ & $1$ &  &  &  &  &&\\
    $1$ & $1$ & $1$ &  &  &  &&\\
    $2$ & $1$ & $2$ & $1$ &  &  &&\\
    $3$ & $1$ & \node{3}; \draw[ultra thick, blue, rounded corners] (-0.2,0.35) --(0.8,0.35) -- (0.8,-0.65) -- (0.3,-0.65) -- (0.3,-0.15) -- (-0.2,-0.15) -- cycle; ; & $3$ & $1$ &  &&\\
    $4$ & $1$ & $4$ & $6$ & $4$ & $1$ &&\\
    $5$ & $1$ & $5$ & $10$ & $10$ & $5$ & $1$&\\
    $\vdots$ & $\vdots$ &  &  &  &  &&$\ddots$\\
    };
    \end{tikzpicture}
    
    \end{document}
    
    

    Merci d’avance pour l’aide
  • Voici une première étape :
    \documentclass{article}
    \usepackage{tikz}
    \usetikzlibrary{matrix}
    
    \newlength{\myrulewidth}
    \setlength{\myrulewidth}{0.4pt} % c'est la valeur par défaut de \pgflinewidth
    \newlength{\myCellSize}
    \setlength{\myCellSize}{0.7cm}
    
    \begin{document}
    
    \begin{tikzpicture}[my rule/.style={line width=\myrulewidth}]
    
    \matrix (mat) [
      my rule, draw, inner sep=0, matrix of math nodes,
      nodes={minimum width=\myCellSize,
             text height=0.6\myCellSize, text depth=0.4\myCellSize},
      ]
      {
        \raisebox{-0.7ex}{$n$}\kern 0.2em\raisebox{0.7ex}{$k$}
               & 0      & 1 & 2 & 3 & 4 & 5 & \cdots   \\
        0      & 1                                     \\
        1      & 1      & 1                            \\
        2      & 1      & 2 & 1                        \\
        3      & 1      & 3 & 3  & 1                   \\
        4      & 1      & 4 & 6  & 4  & 1              \\
        5      & 1      & 5 & 10 & 10 & 5 & 1          \\
        \vdots & \vdots &   &    &    &   &   & \ddots \\
      };
    
    \begin{scope}[my rule]
    \draw (mat-1-1.north east) -- (mat-8-1.south east);
    \draw (mat-1-1.south west) -- (mat-1-8.south east);
    \draw ([shift={(0.5\myrulewidth, -0.5\myrulewidth)}]mat-1-1.north west) --
          (mat-1-1.south east);
    \end{scope}
    
    \end{tikzpicture}
    
    \end{document}
    

    Note que l'utilisation du style '/tikz/matrix of math nodes' a permis de se débarrasser de presque tous les dollars.96122
  • Merci. Un de mes problèmes est que mon encadré bleu décale tout. Comment pallier à cela?
  • Chaque cellule de la matrice contient un nœud TikZ (parce que j'ai utilisé 'matrix of math nodes'). Si tu traces des choses depuis une cellule, TikZ prend en compte ces tracés pour déterminer la taille de la cellule. Ceci peut donc l'agrandir, et avec elle la ligne et la colonne qui contiennent cette cellule. Il faut donc faire ce genre de tracé après avoir terminé la matrice :
    \documentclass{article}
    \usepackage{tikz}
    \usetikzlibrary{matrix, backgrounds}
    
    \newlength{\myrulewidth}
    \setlength{\myrulewidth}{0.4pt} % c'est la valeur par défaut de \pgflinewidth
    \newlength{\myCellSize}
    \setlength{\myCellSize}{0.7cm}
    
    \begin{document}
    
    \begin{tikzpicture}[my rule/.style={line width=\myrulewidth}]
    
    \matrix (mat) [
      my rule, draw, inner sep=0, matrix of math nodes,
      nodes={minimum width=\myCellSize,
             text height=0.6\myCellSize, text depth=0.4\myCellSize},
      ]
      {
        \raisebox{-0.8ex}{$n$}\kern 0.3em\raisebox{0.7ex}{$k$}
               & 0      & 1 & 2 & 3 & 4 & 5 & \cdots   \\
        0      & 1                                     \\
        1      & 1      & 1                            \\
        2      & 1      & 2 & 1                        \\
        3      & 1      & 3 & 3  & 1                   \\
        4      & 1      & 4 & 6  & 4  & 1              \\
        5      & 1      & 5 & 10 & 10 & 5 & 1          \\
        \vdots & \vdots &   &    &    &   &   & \ddots \\
      };
    
    \begin{scope}[my rule]
    \draw (mat-1-1.north east) -- (mat-8-1.south east);
    \draw (mat-1-1.south west) -- (mat-1-8.south east);
    \draw ([shift={(0.5\myrulewidth, -0.5\myrulewidth)}]mat-1-1.north west) --
          (mat-1-1.south east);
    \end{scope}
    
    \begin{scope}[on background layer]
      \draw[blue, fill=blue!15, thick, rounded corners=2pt]
      (mat-5-3.north west) -|
      (mat-6-4.south east) -|
      (mat-5-3.south east) -|
      ([yshift=-2ex]mat-5-3.north west)
      -- cycle;
    \end{scope}
    
    \end{tikzpicture}
    
    \end{document}
    
    96130
  • Merci. J’avance petit à petit. Maintenant c’est l’extrémité de ma flèche qui est loupée et surtout j’ai aucune idée pour la partie droite de la figure.
    \documentclass{article}
    \usepackage{tikz}
    \usetikzlibrary{matrix, backgrounds}
    
    \newlength{\myrulewidth}
    \setlength{\myrulewidth}{0.4pt} % c'est la valeur par défaut de \pgflinewidth
    \newlength{\myCellSize}
    \setlength{\myCellSize}{0.7cm}
    
    \begin{document}
    
    \begin{tikzpicture}[my rule/.style={line width=\myrulewidth}]
    
    \matrix (mat) [
      my rule, draw, inner sep=0, matrix of math nodes,
      nodes={minimum width=\myCellSize,
             text height=0.6\myCellSize, text depth=0.4\myCellSize},
      ]
      {
        \raisebox{-0.8ex}{$n$}\kern 0.3em\raisebox{0.7ex}{$k$}
               & 0      & 1 & 2 & 3 & 4 & 5 & \cdots   \\
        0      & 1                                     \\
        1      & 1      & 1                            \\
        2      & 1      & 2 & 1                        \\
        3      & 1      & 3 & 3  & 1                   \\
        4      & 1      & 4 & 6  & 4  & 1              \\
        5      & 1      & 5 & 10 & 10 & 5 & 1          \\
        \vdots & \vdots &   &    &    &   &   & \ddots \\
      };
    
    \begin{scope}[my rule]
    \draw (mat-1-1.north east) -- (mat-8-1.south east);
    \draw (mat-1-1.south west) -- (mat-1-8.south east);
    \draw ([shift={(0.5\myrulewidth, -0.5\myrulewidth)}]mat-1-1.north west) --
          (mat-1-1.south east);
     \node at (-0.1,-0.05) (debut) {};
         \node at (4,1) (fin) {};
    \draw[->,blue] (debut.east) to[out=40,in=0] (fin.east); 
    \end{scope}
    
    \begin{scope}[on background layer]
      \draw[blue, fill=blue!15, thick, rounded corners=2pt]
      (mat-5-3.north west) -|
      (mat-6-4.south east) -|
      (mat-5-3.south east) -|
      ([yshift=-2ex]mat-5-3.north west)
      -- cycle;
    \end{scope}
    
    \end{tikzpicture}
    
    \end{document}
    
    
  • Il faut changer in=0 en in=180. C'est bizarre la première fois, mais c'est comme ça. 'in' regarde vers le début de la courbe dans la direction indiquée, en quelque sorte. Tu devrais aussi remplacer les trucs du genre
    \node at (4,1) (fin) {};
    
    par des
    \coordinate (fin) at (4,1);
    
    En effet, par défaut, un nœud a un espace intérieur autour du contenu ('inner sep'), même si le contenu est vide, et il y a aussi une 'outer sep' non nulle par défaut, qui est comptée à partir de la bordure du nœud. Cela va créer un petit vide si tu traces des courbes partant du, ou arrivant vers ton nœud vide (mais ça arrive pile poil sur la bordure du nœud si on trace celle-ci avec 'draw'). \coordinate crée un nœud de forme (shape) 'coordinate', ce qui correspond mieux à l'idée mathématique de point. Voir Predefined Shapes dans le manuel de TikZ (p. 225 dans ma version).

    Continue à t'entraîner sur des exemples simples, tu devrais progresser petit à petit. Pour cette figure, les détails tels que « flèche qui part vraiment du bord arrondi » étant un peu délicats à réaliser proprement, voici ma solution :
    \documentclass{article}
    \usepackage{amsmath}
    \usepackage{tikz}
    \usetikzlibrary{babel, backgrounds, calc, intersections, matrix, positioning}
    
    \newlength{\myrulewidth}
    \setlength{\myrulewidth}{0.4pt} % c'est la valeur par défaut de \pgflinewidth
    \newlength{\myCellSize}
    \setlength{\myCellSize}{0.7cm}
    
    \begin{document}
    
    \begin{tikzpicture}[my rule/.style={line width=\myrulewidth},
                        my outline color/.initial=blue!75!black,
                        my text/.style={
                          color/.expanded={\pgfkeysvalueof{/tikz/my outline color}}},
                        my arrow/.style={
                          ->, line width=0.6pt,
                          draw/.expanded={\pgfkeysvalueof{/tikz/my outline color}}},
                        my highlight/.style={
                          draw/.expanded={\pgfkeysvalueof{/tikz/my outline color}},
                          fill=blue!15, line width=0.6pt, rounded corners=2pt}]
    
    \matrix (mat) [
      my rule, draw, inner sep=0, matrix of math nodes,
      nodes={minimum width=\myCellSize,
             text height=0.6\myCellSize, text depth=0.4\myCellSize}]
      {
        \raisebox{-0.8ex}{$n$}\kern 0.3em\raisebox{0.7ex}{$k$}
               & 0      & 1 & 2 & 3 & 4 & 5   &[-2pt] \cdots \\
        0      & 1                                           \\
        1      & 1      & 1                                  \\
        2      & 1      & 2 & 1                              \\
        3      & 1      & 3 & 3  & 1                         \\
        4      & 1      & 4 & 6  & 4  & 1                    \\
        5      & 1      & 5 & 10 & 10 & 5 & 1                \\[-2pt]
        \vdots & \vdots &   &    &    &   &   &       \ddots \\
      };
    
    \begin{scope}[my rule]
    \draw (mat-1-1.north east) -- (mat-8-1.south east);
    \draw (mat-1-1.south west) -- (mat-1-8.south east);
    \draw ([shift={(0.5\myrulewidth, -0.5\myrulewidth)}]mat-1-1.north west) --
          (mat-1-1.south east);
    \end{scope}
    
    \path[my text] node [right=1cm of mat, label=above:{Formule de Pascal}]
      (formula)
      {%
        $\begin{aligned}
          \binom{n}{k} &\mathrel{+} \binom{n}{k+1} \\
                       &=           \binom{n+1}{k+1}
        \end{aligned}$%
      };
    
    \begin{scope}[on background layer]
      \path[name path=p, my highlight]
        (mat-5-3.north west) -| coordinate (A)
        (mat-6-4.south east) -|
        (mat-5-3.south east) --
        (mat-5-3.south west)
        -- cycle;
    
      \pgfmathsetlengthmacro{\myWidth}{width("$\displaystyle \binom{n}{k}$")}
      \pgfmathsetlengthmacro{\myTotalHeight}{
        height("$\displaystyle \binom{n}{k}$") +
        depth("$\displaystyle \binom{n}{k}$")}
      \pgfmathsetlengthmacro{\myXshift}{\pgfkeysvalueof{/pgf/inner xsep} + \myWidth}
      \pgfmathsetlengthmacro{\myYshift}{\myTotalHeight +
                                        2*\pgfkeysvalueof{/pgf/inner ysep}}
    
      \path[my highlight]
        (formula.north west) -|
        (formula.south east) -|
        ([shift={(\myXshift,-\myYshift)}]formula.north west) coordinate (B) --
        (formula.north west |- B) --
        cycle;
      % Pour que la flèche parte vraiment de la bordure arrondie de p, il faut
      % ruser un peu. D'abord, il nous faut le point de départ :
      \path[name path=s] ([shift={(-10pt,-10pt)}]A) -- (A);
      \path[name intersections={of=p and s}];
    \end{scope}
    
    % Ensuite, on peut tracer la flèche.
    \path[my arrow] (intersection-1) to[bend left=10]
                    ($(formula.north west)!0.5!(formula.west)$);
    \end{tikzpicture}
    
    \end{document}
    
    96148
  • Merci mais quand je compile le fichier ça ne marche pas.
  • Tu dois avoir une vieille installation, essaie d'enlever la bibliothèque TikZ babel dans le préambule. Si ça ne suffit pas, donne le message d'erreur.
  • Ok merci pour l’aide et l’accompagnement.
Connectez-vous ou Inscrivez-vous pour répondre.