使用 tikz 為十幀網格建立新指令

使用 tikz 為十幀網格建立新指令

我正在嘗試創建數十個框架來模擬數字的加法。如果\tensframe{#1}{#2}能顯示 #1 綠色圓圈從左側開始並逐漸增大,然後顯示 #2 藍色圓圈從綠色繼續然後向上,我會很高興。如果#1 或#1+#2 大於5,則需要結轉到第二列。有人知道如何讓它運作嗎?

\documentclass{article}
\usepackage{ifthen}
\usepackage{listofitems}
\usepackage{multicol}
\usepackage{tikz}
\usepackage{pgfplots}


\newcommand{\tensframe}[2]{\begin{tikzpicture}
    [%
        box/.style={rectangle,draw=black, minimum size=10mm},
    ]%

\foreach \x in {1,2}{
    \foreach \y in {1,...,5}
        \node[box] at (\x,\y){};
}

\foreach \x in {1,1}{
    \foreach \y in {1,...,#1}
      \filldraw[green] (\x,\y) circle (8pt);
}

\foreach \x in {1,1}{
   \foreach \y in {#1+1,...,5}
      \filldraw[blue] (\x,\y) circle (8pt);
}


    \foreach \y in {1,...,#2+#1-5}
      \filldraw[blue] (2,\y) circle (8pt);


\end{tikzpicture}}

\begin{document}
\tensframe{1}{5}
\vspace{1cm}

\tensframe{3}{5}

\vspace{1cm}

\tensframe{2}{1}
    \end{document}

\tensframe{1}{5}是完美的 在此輸入影像描述

\tensframe{3}{5}單元格 (2,2) 和 (2,3) 缺少兩個藍色圓圈 在此輸入影像描述

\tensframe{2}{1}甚至沒有定義(我認為因為我沒有 foreach ys 的值範圍),但如果它正常工作,在 (1,1) 和 (1,2) 中會有綠色單元格,在(1,3) 看起來像這樣: 在此輸入影像描述

有誰知道如何編碼?非常感謝

答案1

只有一圈。首先建立節點並命名,並使用該名稱來填入對應的顏色。

\documentclass{article}
\usepackage{tikz}

\newcommand{\tensframe}[2]{\begin{tikzpicture}
    [%
        box/.style={rectangle,draw=black, minimum size=10mm},
    ]%

\foreach \y in {1,...,5}{
    \foreach \x [evaluate=\x as \ni using int(5*(\x-1)+\y)] in {1,2}{
            \node[box] (\ni) at (\x,\y){};
            \ifnum\ni<\numexpr#1+1\relax
                     \filldraw[green] (\ni.center) circle (8pt);
            \else
                \ifnum\ni<\numexpr#1+#2+1\relax
                     \filldraw[blue] (\ni.center) circle (8pt);
                \fi
            \fi
            
    }
}
\end{tikzpicture}}

\begin{document}
\tensframe{1}{5}\ \tensframe{3}{5}\ \tensframe{2}{1}\ \tensframe{6}{2}
\end{document}

在此輸入影像描述

更新:解決評論問題的新程式碼

以下程式碼顯示了透過註解解決更新問題的建議:水平方案和定位它們。

在本例中,foreach循環已更改為兩個不同的命令,一個將繪製網格 ( tensh, tensv),另一個將填充它 ( filltens)。所有這些命令都應該位於tikzpicture不屬於它們的一部分的內部。

tens{v|h}命令有一個強制參數,即節點的名稱matrix。由於網格現在是node,因此可以引用它來定位其他節點或進行填充。可選參數將用於定位。

filltens指令有三個強制參數,第一個是要填滿圓圈的矩陣的名稱,第二個和第三個是裡面綠色和藍色圓圈的數量。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix, positioning}

\newcommand{\tensh}[2][]{%
    \matrix[matrix of nodes, inner sep, nodes in empty cells, 
        nodes={draw, minimum size=1cm, inner sep=.3333em},
        row sep=-\pgflinewidth, column sep=-\pgflinewidth, 
        ampersand replacement=\&, #1] (#2) {
    |(#2-1)| \& |(#2-2)| \& |(#2-3)| \& |(#2-4)| \& |(#2-5)|\\
    |(#2-6)| \& |(#2-7)| \& |(#2-8)| \& |(#2-9)| \& |(#2-10)|\\};
}

\newcommand{\tensv}[2][]{%
    \matrix[matrix of nodes, inner sep, nodes in empty cells, 
        nodes={draw, minimum size=1cm, inner sep=.3333em},
        row sep=-\pgflinewidth, column sep=-\pgflinewidth, 
        ampersand replacement=\&, #1] (#2){
    |(#2-5)| \& |(#2-10)| \\ |(#2-4)| \& |(#2-9)| \\ 
    |(#2-3)| \& |(#2-8)|  \\ |(#2-2)| \& |(#2-7)| \\ 
    |(#2-1)| \& |(#2-6)|\\};
}

\newcommand{\filltens}[3]{%
    \foreach \i in {1,...,#2}
        \filldraw[green] (#1-\i.center) circle (8pt);
    \foreach \i [evaluate=\i as \ni using int(#2+\i)] in {1,...,#3}
        \filldraw[blue] (#1-\ni.center) circle (8pt);
}

\begin{document}

\begin{tikzpicture}
\tensh{a}
\tensv[above = 5mm of a.north east, anchor=south east]{b}
\tensh[right = 5mm of b.south east, anchor=south west] {c}
\tensv[right = 5mm of a.north east, anchor=north west]{d}
\filltens{a}{2}{4}
\filltens{b}{5}{3}
\filltens{c}{7}{2}
\filltens{d}{6}{1}
\end{tikzpicture}

\end{document}

在此輸入影像描述

答案2

編輯:錯誤修復

對於expl3,我不知道你是否對此感興趣

%https://tex.stackexchange.com/questions/661181/creating-newcommand-for-a-tens-frame-grid-using-tikz
\documentclass{article}
\usepackage{tikz}
\ExplSyntaxOn
\NewDocumentCommand{\tensframe}{ m m }
{
\begin{tikzpicture}
  [%
  box/.style={rectangle,draw=black, minimum~size=10mm},
  ]%
  \foreach \x in {1,2}
    {
      \foreach \y in {1,...,5}
      \node[box] at (\x,\y){};
    }
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \int_step_inline:nn { #1 }
    {
      \int_compare:nNnTF {##1} < {6}
        {
          \int_set:Nn \l_tmpa_int { 1 }
          \int_set:Nn \l_tmpb_int { ##1 }
        }
        {
          \int_set:Nn \l_tmpa_int { 2 }
          \int_set:Nn \l_tmpb_int { \int_eval:n { ##1 - 5 } }
        }
      \filldraw[green] (\l_tmpa_int,\l_tmpb_int) circle (8pt);
    }
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \int_step_inline:nn { #2 }
    {
      \int_compare:nNnTF {#1+##1} < {6}
      {
        \int_set:Nn \l_tmpa_int { 1 }
        \int_set:Nn \l_tmpb_int { \int_eval:n { #1+##1 } }
      }
      {
        \int_set:Nn \l_tmpa_int { 2 }
        \int_set:Nn \l_tmpb_int { \int_eval:n { #1+##1 - 5 } }
      }
      \filldraw[blue] (\l_tmpa_int,\l_tmpb_int) circle (8pt);    
    }
\end{tikzpicture}
}
\ExplSyntaxOff
\begin{document}
\tensframe{1}{5}
\vspace{1cm}

\tensframe{3}{5}

\vspace{1cm}

\tensframe{2}{1}

\vspace{1cm}

\tensframe{6}{4}

\end{document}

在此輸入影像描述

相關內容