製作一個在頂點上有數字的格子

製作一個在頂點上有數字的格子

任何熟悉計算矩形中從左下角到右上角的向上/向右路徑問題的人都知道,這樣做的一種方法是透過遞歸來完成。我想將其視覺化,因此我需要製作一個在其頂點上有數字的格子。在下面的矩陣中,數字在正方形內,但我希望它們在頂點上,或者更好的是在四條線的交點上有一個間隙,數字可以在該間隙中。

$\begin{array}{*{20}{c}}
\hline
  | & 1&| & 1&| & 1&| & 1&| & 1&| & 1&| & 1| &  \\ 
\hline
  | & 7&| & 6&| & 5&| & 4&| & 3&| & 2&| & 1| &  \\ 
\hline
  | & {28}&| & {21}&| & {15}&| & {10}&| & 6&| & 3&| & 1| &  \\ 
\hline
  | & {84}&| & {56}&| & {35}&| & {20}&| & {10}&| & 4&| & 1| & \\ 
\hline
\end{array}$

像這樣的東西: 在此輸入影像描述

重要提示:在正確答案中的圖像回答問題後,圖像已被編輯。

答案1

這是一種方法:

在此輸入影像描述

代碼:

\documentclass{article}
\usepackage{xstring}
\usepackage{tikz}
\usepackage{collcell}

\newcommand{\MakeBox}[1]{\makebox[2.0em][c]{#1}}%
\newcommand*{\MyBox}[1]{%
    \phantom{\MakeBox{#1}}%
    \IfStrEq{#1}{}{}{%
        \begin{tikzpicture}[overlay, draw=red, line width=1.0pt, text=blue]
            \node [draw=none, inner sep=2pt] (Node) {\MakeBox{#1}};
            \draw (Node.north) -- ([yshift=2.0ex]Node.north);
            \draw (Node.south) -- ([yshift=-2.0ex]Node.south);
            \draw (Node.west) -- ([xshift=-1.0em]Node.west);
            \draw (Node.east) -- ([xshift=1.0em]Node.east);
        \end{tikzpicture}%
    }%
}

\newcolumntype{C}{>{\collectcell\MyBox}c<{\endcollectcell}}

\begin{document}
$\begin{array}{*{20}{C}}
   & 1 &  1 &  1 &  1 &  1 &  1 &  1 &  \\[2.0ex]
   & 7 &  6 &  5 &  4 &  3 &  2 &  1 &  \\[2.0ex] 
   & 28 &  21 &  15 &  10 &  6 &  3 &  1 &  \\[2.0ex] 
   & 84 &  56 &  35 &  20 &  10 &  4 &  1 & \\ [2.0ex]
\end{array}$
\end{document}

如果您想抑制外部行,則需要透過呼叫 來指示最後一行\ThisIsLastRow。第一列的檢測是透過使用F列類型來處理的。

在此輸入影像描述

進一步增強

  • 由於我使用 a\makebox[2.0em][c]{#1}來放置文字以使其保持居中,因此只有單寬度字元的最後一列看起來是錯誤的。解決此問題的一種方法是定義兩種列類型:一種用於兩位數,一種用於單位數。或者,可以在文字中繪製線條,然後將節點文字覆蓋在其上,然後刪除數字周圍的線條。

代碼:

\documentclass{article}
\usepackage{xstring}
\usepackage{etoolbox}
\usepackage{tikz}
\usepackage{collcell}

\newtoggle{IsFirstColumn}\togglefalse{IsFirstColumn}%%
\newtoggle{IsLastRow}\togglefalse{IsLastRow}%
\newcommand{\ThisIsLastRow}{\global\toggletrue{IsLastRow}}%

\newcommand{\MakeBox}[1]{\makebox[2.0em][c]{#1}}%
\newcommand*{\MyBox}[1]{%
    \phantom{\MakeBox{#1}}%
    \IfStrEq{#1}{}{}{%
        \begin{tikzpicture}[overlay, draw=red, line width=1.0pt, text=blue]
            \node [draw=none, inner sep=2pt] (Node) {\MakeBox{#1}};
            \iftoggle{IsLastRow}{}{%
                \draw (Node.south) -- ([yshift=-2.0ex]Node.south);
            }%
            \iftoggle{IsFirstColumn}{}{%
                \draw (Node.west) -- ([xshift=-1.0em]Node.west);
            }%
        \end{tikzpicture}%
    }%
}

\newcommand*{\MyBoxFirstColumn}[1]{%
    \global\toggletrue{IsFirstColumn}%
    \MyBox{#1}%
    \global\togglefalse{IsFirstColumn}%
}%

\newcolumntype{C}{>{\collectcell\MyBox}c<{\endcollectcell}}
\newcolumntype{F}{>{\collectcell\MyBoxFirstColumn}c<{\endcollectcell}}

\begin{document}
$\begin{array}{F*{20}{C}}
    1 &   1 &   1 &   1 &   1 &  1 &  1 \\[2.0ex]
    7 &   6 &   5 &   4 &   3 &  2 &  1 \\[2.0ex] 
   28 &  21 &  15 &  10 &   6 &  3 &  1 \\[2.0ex] \ThisIsLastRow
   84 &  56 &  35 &  20 &  10 &  4 &  1 \\
\end{array}$
\end{document}

答案2

鑑於您想要的輸出,我想我會使用matrix of nodes

螢幕截圖

完整程式碼

% arara: pdflatex
% !arara: indent: {overwrite: true}
\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}[thick]
    \matrix (mylattice)[
        matrix of nodes,
        row sep=.5cm,
        column sep=.5cm,  
        nodes in empty cells,
        execute at empty cell={\node[draw=none]{\phantom{X}};},
    ]{
        &    &  &  &  &  &  &  &\\ 
        & 1 &  1 &  1 &  1 &  1 &  1 &  1  & \\
        & 7 &  6 &  5 &  4 &  3 &  2 &  1  & \\
        & 28 &  21 &  15 &  10 &  6 &  3 &  1 &  \\
        & 84 &  56 &  35 &  20 &  10 &  4 &  1 & \\ 
        &    &      &    &      &   &     &  & \\ 
    };
    \foreach \i [evaluate=\i as \y using int(\i+1)] in {1,...,5}
    {
        \foreach \j [evaluate=\j as \x using int(\j+1)]in {1,...,8}
        {
            \ifnum\j>1
            \draw[red] (mylattice-\i-\j)--(mylattice-\y-\j);
            \fi
            \ifnum\i>1
            \draw[blue] (mylattice-\i-\j)--(mylattice-\i-\x);
            \fi
        }
    }
\end{tikzpicture}
\end{document}

根據註釋,如果您不需要外部零件,則可以使用以下稍加修改的程式碼。

螢幕截圖

% arara: pdflatex
% !arara: indent: {overwrite: true}
\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}[thick]
    \matrix (mylattice)[
        matrix of nodes,
        row sep=.5cm,
        column sep=.5cm,  
        nodes in empty cells,
        execute at empty cell={\node[draw=none]{\phantom{X}};},
    ]{
         1 &  1 &  1 &  1 &  1 &  1 &  1   \\
         7 &  6 &  5 &  4 &  3 &  2 &  1   \\
         28 &  21 &  15 &  10 &  6 &  3 &  1   \\
         84 &  56 &  35 &  20 &  10 &  4 &  1  \\ 
    };
    \foreach \i [evaluate=\i as \y using int(\i+1)] in {1,...,4}
    {
        \foreach \j [evaluate=\j as \x using int(\j+1)]in {1,...,7}
        {
            \ifnum\i<4
            \draw[red] (mylattice-\i-\j)--(mylattice-\y-\j);
            \fi
            \ifnum\j<7
            \draw[blue] (mylattice-\i-\j)--(mylattice-\i-\x);
            \fi
        }
    }
\end{tikzpicture}
\end{document}

相關內容