<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">%%% ----------------------------------------------------------------------------
%%% pascaltriangle: Typeset Pascal triangle figure with LaTeX3
%%% Author    : Nan Geng &lt;nangeng@nwafu.edu.cn&gt;
%%% Repository: https://github.com/registor/pascaltriangle or https://gitee.com/nwafu_nan/pascaltriangle
%%% License   : The LaTeX Project Public License 1.3c
%%% ----------------------------------------------------------------------------

\NeedsTeXFormat{LaTeX2e}
\RequirePackage{expl3}
\ProvidesExplPackage{pascaltriangle}{2022-01-28}{v1.0.1}
  {Typeset Pascal triangle figure with LaTeX3}

\RequirePackage{xparse}

%% \tl_if_eq:NnTF 荳師exlive 2020逧��螳ｹ諤ｧ隶ｾ鄂ｮ
\cs_if_exist:NF \tl_if_eq:NnTF
  {
    \tl_new:N \l__tblr_backport_b_tl
    \prg_new_protected_conditional:Npnn \tl_if_eq:Nn #1 #2 { T, F, TF }
      {
        \group_begin:
          \tl_set:Nn \l__tblr_backport_b_tl {#2}
          \exp_after:wN
        \group_end:
        \if_meaning:w #1 \l__tblr_backport_b_tl
          \prg_return_true:
        \else:
          \prg_return_false:
        \fi:
      }
    \prg_generate_conditional_variant:Nnn \tl_if_eq:Nn { c } { TF, T, F }
  }

\cs_if_exist:NF \seq_map_indexed_function:NN
  {
    \cs_set_eq:NN \seq_map_indexed_function:NN \seq_indexed_map_function:NN
  }

%% 隗｣蜀ｳTikZ蠎捺裏豕募惠Expl3荳ｭ霓ｽ蜈･髣ｮ鬚�
\RequirePackage {amsmath, tikz, etoolbox}
\ExplSyntaxOff
\patchcmd
{\tcb@input@library@in}
  {%
   \input\tcbpkgprefix#1\relax%
  }
  {%
    \@pushfilename
    \input\tcbpkgprefix#1\relax%
    \@popfilename
  }
  {}{}

\patchcmd
{\pgfutil@InputIfFileExists}
  {\input #1}
  {%
    \@pushfilename
    \xdef\@currname{#1}%
    \input #1 %
    \@popfilename
  }
  {}{}
\ExplSyntaxOn
\usetikzlibrary{shapes.geometric}

%% 螳壻ｹ牙序驥�
\bool_new:N  \l__pascal_num_cell_bool   % 譏ｯ蜷ｦ扈伜宛陦悟�蜿ｷ
\bool_new:N  \l__pascal_binom_cell_bool % 譏ｯ蜷ｦ莉�ｻ伜宛莠碁｡ｹ蠑剰｡ｨ霎ｾ蠑�

\tl_new:N    \l__pascal_cell_color_tl   % 蜊募�譬ｼ蝪ｫ蜈�｢懆牡
\tl_new:N    \l__pascal_shift_col_pt_tl % 蛻怜序謐｢蝮先��
\tl_new:N    \l__pascal_shift_row_pt_tl % 陦悟序謐｢蝮先��
\tl_new:N    \l__pascal_font_size_tl    % 譁�ｻｶ螟ｧ蟆�

\int_new:N    \l__pascal_shape_type_int % 蠖｢迥ｶ(1---遲芽���2---逶ｴ隗�)
\int_new:N    \l__pascal_row_idx_int    % 陦檎ｴ｢蠑�(莉�0隶｡謨ｰ)
\int_new:N    \l__pascal_col_idx_int    % 蛻礼ｴ｢蠑�(莉�0隶｡謨ｰ)
\int_new:N    \l__pascal_row_top_int    % 蜷��譛螟ｧ陦梧焚
\int_new:N    \l__pascal_total_idx_int  % 邏｢蠑墓ｻ謨ｰ
\int_new:N    \l__pascal_fill_row_int   % 蝪ｫ蜈�｡悟捷(譛螟ｧ蛟ｼ荳主ｽ灘燕蛻玲焚譛牙��御ｻ朱｡ｶ蜷台ｸ倶ｻ�0隶｡謨ｰ)
\int_new:N    \l__pascal_fill_col_int   % 蝪ｫ蜈��蜿ｷ(莉�0隶｡謨ｰ)

\dim_new:N    \l__pascal_cell_radius_dim % 蜊募�譬ｼ螟ｧ蟆�(荳ｭ蠢�芦鬘ｶ轤ｹ逧�濠蠕�)

%% 騾蛾｡ｹ隶ｾ隶｡
\keys_define:nn { pascal }
  {
    % 譏ｯ蜷ｦ髴隕∬｡悟�郛門捷
    withnum .bool_set:N = \l__pascal_num_cell_bool,
    withnum .default:n = true,
    withnum .initial:n = false,

    % 譏ｯ蜷ｦ莉�ｻ伜宛莠碁｡ｹ蠑剰｡ｨ霎ｾ蠑�
    binom .bool_set:N = \l__pascal_binom_cell_bool,
    binom .default:n = true,
    binom .initial:n = false,

    % 蠖｢迥ｶ騾画叫(iso---遲芽�荳芽ｧ貞ｽ｢, rt---逶ｴ隗剃ｸ芽ｧ貞ｽ｢)
    shape .choice:,
    shape .value_required:n = true,
    shape .choices:nn =
      { iso, rt }
      { \int_set_eq:NN \l__pascal_shape_type_int \l_keys_choice_int },
    shape .initial:n = iso,

    % 蜊募�譬ｼ蜊雁ｾ�(荳ｭ蠢�芦鬘ｶ轤ｹ)
    radius .dim_set:N  = \l__pascal_cell_radius_dim,
    radius .initial:n = 0.5cm,

    % 譁�ｭ怜､ｧ蟆�
    fontsize .tl_set:N  = \l__pascal_font_size_tl,
    fontsize .initial:n = \small,

    % 隕∝｡ｫ蜈�黒蜈��ｼ陦梧焚(謖牙�莉朱｡ｶ蜷台ｸ具ｼ御ｻ�0蠑蟋玖ｮ｡謨ｰ)
    fillr .int_set:N = \l__pascal_fill_row_int,
    fillr .initial:n = 1 ,
    % 隕∝｡ｫ蜈�黒蜈��ｼ蛻玲焚(莉主ｷｦ蜷大承�御ｻ�0蠑蟋玖ｮ｡謨ｰ)
    fillc .int_set:N = \l__pascal_fill_col_int,
    fillc .initial:n = 1 ,
    fillrc  .meta:n = { fillr = #1 , fillc = #1 },

    unknown .code:n = { \__pascal_error:n { unknown-option } }
  }

\msg_new:nnn { pascal } { unknown-option }
  { package~ option~ "\l_keys_key_tl"~ is~ unknown. }

%% 蜿よ焚隶ｾ鄂ｮ逕ｨ謌ｷ謗･蜿｣
\NewDocumentCommand \pascalset { m }
  { \keys_set:nn { pascal } {#1} }

%% 蛻ｩ逕ｨLaTeX3逧�fact髦ｶ荵倩ｮ｡邂怜�謨ｰ隶｡邂礼ｻ�粋謨ｰ
\cs_set:Npn \__pascal_binomcoeff:nn #1#2
  {
    \fp_eval:n { fact(#1) / (fact(#2) * fact(#1 - #2)) }
  }

%% 扈�粋謨ｰ隶｡邂礼畑謌ｷ謗･蜿｣
\NewDocumentCommand{\binomc}{m m}
  {
    \__pascal_binomcoeff:nn{#1}{#2}
  }

%% 扈伜宛Pascal荳芽ｧ貞ｽ｢
%% 豕ｨ諢�":"譌�豕穂ｽｿ逕ｨ騾�謌先栫蝮先��ｸ崎�逕ｨ逧�琉鬚假ｼ御ｻ･荳狗ｽ醍ｫ咏ｻ吝�莠�ｧ｣蜀ｳ譁ｹ譯�
%% https://tex.stackexchange.com/questions/501333/how-to-use-colon-as-part-of-tikz-syntax-while-in-explsyntaxon-environment
%% 蟆�":"逕ｨ\c_colon_str莉｣譖ｿ

%% 譬ｹ謐ｮ蠖｢迥ｶ邀ｻ蝙玖ｮ｡邂怜�蜿俶困蝮先��(譫∝攝譬�)
\cs_set:Npn \__pascal_shift_col_pt:n #1
  {
    \int_case:nn { \l__pascal_shape_type_int }
      {
        {1}{
          \tl_set:Nn \l__pascal_shift_col_pt_tl { -60 \c_colon_str
              \fp_eval:n {sqrt(3) * \l__pascal_cell_radius_dim * #1}pt
            }
        }
        {2}{
          \tl_set:Nn \l__pascal_shift_col_pt_tl { -45 \c_colon_str
              \fp_eval:n {2 * \l__pascal_cell_radius_dim * #1}pt
            }
        }
      }
  }

%% 譬ｹ謐ｮ蠖｢迥ｶ邀ｻ蝙玖ｮ｡邂苓｡悟序謐｢蝮先��(譫∝攝譬�)
\cs_set:Npn \__pascal_shift_row_pt:n #1
  {
    \int_case:nn { \l__pascal_shape_type_int }
      {
        {1}{
          \tl_set:Nn \l__pascal_shift_row_pt_tl { -120 \c_colon_str
              \fp_eval:n {sqrt(3) * \l__pascal_cell_radius_dim * #1}pt
            }
        }
        {2}{
          \tl_set:Nn \l__pascal_shift_row_pt_tl { -90 \c_colon_str
              \fp_eval:n {sqrt(2) * \l__pascal_cell_radius_dim * #1}pt
            }
        }
      }
  }

%% 豁｣蜈ｭ霎ｹ蠖｢髣ｭ蜷郁ｷｯ蠕�ｻ｣遐�
\cs_set:Npn \__pascal_hex_cell:
  {
    (30  \c_colon_str \fp_eval:n{\l__pascal_cell_radius_dim}pt) --
    (90  \c_colon_str \fp_eval:n{\l__pascal_cell_radius_dim}pt) --
    (150 \c_colon_str \fp_eval:n{\l__pascal_cell_radius_dim}pt) --
    (210 \c_colon_str \fp_eval:n{\l__pascal_cell_radius_dim}pt) --
    (270 \c_colon_str \fp_eval:n{\l__pascal_cell_radius_dim}pt) --
    (330 \c_colon_str \fp_eval:n{\l__pascal_cell_radius_dim}pt) --
    cycle (90 \c_colon_str 0)
  }

%% 豁｣譁ｹ蠖｢髣ｭ蜷郁ｷｯ蠕�ｻ｣遐�
\cs_set:Npn \__pascal_square_cell:
  {
    (45  \c_colon_str \fp_eval:n{\l__pascal_cell_radius_dim}pt) --
    (134 \c_colon_str \fp_eval:n{\l__pascal_cell_radius_dim}pt) --
    (225 \c_colon_str \fp_eval:n{\l__pascal_cell_radius_dim}pt) --
    (-45 \c_colon_str \fp_eval:n{\l__pascal_cell_radius_dim}pt) --
    cycle (90 \c_colon_str 0)
  }

%% 扈伜宛陦悟�郛門捷譬�ｮｰ蜊募�譬ｼ
\cs_set:Npn \__pascal_num_draw:nnn #1#2#3
  {
    \int_case:nn { \l__pascal_shape_type_int }
      {
        {1}{% 豁｣蜈ｭ霎ｹ蠖｢
          \int_compare:nNnT { #1 } = {0}% 譛蟾ｦ蛻�
            {
              \int_compare:nNnT { #2 } = { #3 - 1 }% 譛蠎戊｡�
                {
                  \begin{scope}[shift={(-150\c_colon_str {\fp_eval:n{3*\l__pascal_cell_radius_dim}pt})}]
                    \draw[dashed, top~color=gray!10,bottom~color=gray!20]
                      \__pascal_hex_cell:
                      node[rotate=-45] {$m,n$};
                  \end{scope}
                }
            }
          \int_compare:nNnT { #1 } = { 0 }% 譛蟾ｦ蛻�
            {
              \begin{scope}[shift={(-180\c_colon_str {\fp_eval:n{sqrt(3)*\l__pascal_cell_radius_dim}pt})}]
                \draw[dashed, top~color=gray!10,bottom~color=gray!20]
                  \__pascal_hex_cell:
                  node{$(\int_eval:n {#2})$};
              \end{scope}
            }
          \int_compare:nNnT { #2 } = { #3 - 1 }% 譛蠎戊｡�
            {
              \begin{scope}[shift={(-120\c_colon_str {\fp_eval:n{sqrt(3)*\l__pascal_cell_radius_dim}pt})}]
                \draw[dashed, top~color=gray!10,bottom~color=gray!20]
                  \__pascal_hex_cell:
                  node {$(\int_eval:n {#1})$};
              \end{scope}
            }
        }
        {2}{% 豁｣譁ｹ蠖｢
          \int_compare:nNnT { #1 } = {0}% 譛蟾ｦ蛻�
            {
              \int_compare:nNnT { #2 } = { #3 - 1 }% 譛蠎戊｡�
                {
                  \begin{scope}[shift={(-135\c_colon_str {\fp_eval:n{2*\l__pascal_cell_radius_dim}pt})}]
                    \draw[dashed, top~color=gray!10,bottom~color=gray!20]
                      \__pascal_square_cell:
                      node[scale=0.8,rotate=-45] {$m,n$};
                  \end{scope}
                }
            }
          \int_compare:nNnT { #1 } = { 0 }% 譛蟾ｦ蛻�
            {
              \begin{scope}[shift={(-180\c_colon_str {\fp_eval:n{sqrt(2)*\l__pascal_cell_radius_dim}pt})}]
                \draw[dashed, top~color=gray!10,bottom~color=gray!20]
                  \__pascal_square_cell:
                  node{$(\int_eval:n {#2})$};
              \end{scope}
            }
          \int_compare:nNnT { #2 } = { #3 - 1 }% 譛蠎戊｡�
            {
              \begin{scope}[shift={(-90\c_colon_str {\fp_eval:n{sqrt(2)*\l__pascal_cell_radius_dim}pt})}]
                \draw[dashed, top~color=gray!10,bottom~color=gray!20]
                  \__pascal_square_cell:
                  node {$(\int_eval:n {#1})$};
              \end{scope}
            }
        }
      }
  }

%% 扈伜宛荳芽ｧ貞ｽ｢蜊募�譬ｼ
\cs_set:Npn \__pascal_cell_draw:nn #1#2
  {
    \int_case:nn { \l__pascal_shape_type_int }
      {
        {1}{% 豁｣蜈ｭ霎ｹ蠖｢
          \draw[top~color=\l__pascal_cell_color_tl!20,bottom~color=\l__pascal_cell_color_tl!60]
            \__pascal_hex_cell:
            \bool_if:NTF \l__pascal_binom_cell_bool
              {
                node {$\binom{\int_eval:n {#1}}{\int_eval:n {#2}}$};
              }
              {
                node {$\__pascal_binomcoeff:nn {#1}{#2}$};
              }
        }
        {2}{% 豁｣譁ｹ蠖｢
          \draw[top~color=\l__pascal_cell_color_tl!20,bottom~color=\l__pascal_cell_color_tl!60]
            \__pascal_square_cell:
            \bool_if:NTF \l__pascal_binom_cell_bool
              {
                node {$\binom{\int_eval:n {#1}}{\int_eval:n {#2}}$};
              }
              {
                node {$\__pascal_binomcoeff:nn {#1}{#2}$};
              }
        }
      }
  }

%% 扈伜宛Pascal荳芽ｧ貞ｽ｢
% 遲芽�荳芽ｧ貞ｽ｢逧�ｻ伜宛蜴溽炊蜿り�ｺ� https://texample.net/tikz/examples/pascal-triangle/
% Author: M.H. Ahmadi
\cs_set:Npn \__pascal_triangle_draw:n #1
  {
    % 隶ｾ鄂ｮ豁｣蟶ｸ蜊募�譬ｼ荳ｺ轣ｰ濶ｲ
    \tl_set:Nn \l__pascal_cell_color_tl {gray}
    % 隶ｰ蠖慕ｻ伜宛蛻玲焚
    \int_set:Nn \l_tmpa_int {#1}

    % 菴ｿ逕ｨTikZ扈伜宛
    \begin{tikzpicture}[font=\l__pascal_font_size_tl]
      % 蛻怜ｾｪ邇ｯ
      \int_step_inline:nn {\l_tmpa_int}
        {
          % 隹�紛荳ｺ莉�0隶｡謨ｰ�御ｻ･萓ｿ莠主錘扈ｭ隶｡邂�
          \int_set:Nn \l__pascal_col_idx_int { ##1 - 1 }

          % 隶｡邂怜�蜿俶困蝮先��(譫∝攝譬�)
          \__pascal_shift_col_pt:n {\l__pascal_col_idx_int}

          % 逕ｨscope邇ｯ蠅�序謐｢蜷��蛻ｰ謖�ｮ壻ｽ咲ｽｮ
          \begin{scope}[shift={(\l__pascal_shift_col_pt_tl)}]
            % 隶｡邂玲ｯ丞�譛螟夊｡梧焚
            \int_set:Nn \l__pascal_row_top_int {\l_tmpa_int - ##1 + 1}

            % 陦悟ｾｪ邇ｯ
            \int_step_inline:nn { \l__pascal_row_top_int }
            {
              % 隹�紛荳ｺ莉�0隶｡謨ｰ�御ｻ･萓ｿ莠主錘扈ｭ隶｡邂�
              \int_set:Nn \l__pascal_row_idx_int { ####1 - 1 }

              % 隶｡邂礼ｻ�粋謨ｰ隶｡邂嶺ｸｭ逧�｡梧焚
              \int_zero:N \l__pascal_total_idx_int
              \int_add:Nn \l__pascal_total_idx_int { \l__pascal_row_idx_int }
              \int_add:Nn \l__pascal_total_idx_int { \l__pascal_col_idx_int }

              % 荳ｺ謖�ｮ夊｡瑚ｮｾ鄂ｮ扈伜宛鬚懆牡
              \int_case:nn { \l__pascal_col_idx_int }
                {
                  {0}{
                    \tl_set:Nn \l__pascal_cell_color_tl { pink }
                  }
                  {1}{
                    \tl_set:Nn \l__pascal_cell_color_tl { yellow }
                  }
                  {2}{
                    \tl_set:Nn \l__pascal_cell_color_tl { blue }
                  }
                  {3}{
                    \tl_set:Nn \l__pascal_cell_color_tl { green }
                  }
                  {\l__pascal_fill_col_int - 1}{
                    \int_compare:nNnTF{\l__pascal_row_idx_int} &lt; { \l__pascal_fill_row_int + 1 }
                      {\tl_set:Nn \l__pascal_cell_color_tl { purple }}
                      {\tl_set:Nn \l__pascal_cell_color_tl { gray }}
                  }
                  {\l__pascal_fill_col_int}{
                    \int_compare:nNnTF{\l__pascal_row_idx_int} = { \l__pascal_fill_row_int }
                      {\tl_set:Nn \l__pascal_cell_color_tl { purple }}
                      {\tl_set:Nn \l__pascal_cell_color_tl { gray }}
                  }
                }

              % 隶｡邂苓｡悟序謐｢蝮先��(譫∝攝譬�)
              \__pascal_shift_row_pt:n {\l__pascal_row_idx_int}
              % 逕ｨscope邇ｯ蠅�序謐｢蜷�｡悟芦謖�ｮ壻ｽ咲ｽｮ
              \begin{scope}[shift={(\l__pascal_shift_row_pt_tl)}]
                % 譏ｯ蜷ｦ扈伜宛陦悟�郛門捷
                \bool_if:NT \l__pascal_num_cell_bool
                  {
                    \__pascal_num_draw:nnn { \l__pascal_col_idx_int }
                                           { \l__pascal_row_idx_int }
                                           { \l__pascal_row_top_int }
                  }
                % 扈伜宛荳荳ｪ蜊募�譬ｼ
                \__pascal_cell_draw:nn{\l__pascal_total_idx_int}{\l__pascal_col_idx_int}
              \end{scope}
            }
          \end{scope}
        }
    \end{tikzpicture}
  }

%% 扈伜宛Pascal荳芽ｧ貞ｽ｢逕ｨ謌ｷ謗･蜿｣
\NewDocumentCommand{\pascal}{O{} m}
  {
    \group_begin:
      \keys_set:nn { pascal } {#1}
      \__pascal_triangle_draw:n{#2}
    \group_end:
  }

\endinput
</pre></body></html>