更改 tikz 图片中的位置

更改 tikz 图片中的位置

我一直在研究一些代码来帮助小孩子进行计数。

其中有一些点的矩形网格称为十帧

在此处输入图片描述

上图是 3 的十位数框架。

我希望学生们能够数到十以上,并使用如下图所示的图像来显示两位数。请注意,完整的十位数框带有阴影,以帮助强调这是数字的第一位数字。

在此处输入图片描述

上图是 23 的十位数框架。

使用下面丹尼尔的回答,这里有一些允许数字逐渐以十帧形式出现的代码:注意代码中的第一个输入是起始数字,第二个输入是最终数字,第三个输入是数字开始变化的幻灯片。

    \documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{math}
\definecolor{titlebg}{RGB}{231, 182, 13}
\begin{document}

\tikzset{%
  pics/full tens/.style={%
    code={%
      \fill[titlebg!20] (0, 0) rectangle (5, 2);
      \draw[titlebg] (0, 0) grid (5, 2);
      \foreach \j in {0, 1}{%
        \foreach \i in {0, ..., 4}{%
          \filldraw[titlebg] (\i +.5, \j +.5) circle (.28);
        }
      }
    }
  },
  pics/tens/.style={%
    code={%
      \tikzmath{%
        integer \erem;
        if #1>5 then {%
          {%
            \draw (0, 0) grid (5, 2);
          };
          for \i in {0, ..., 4}{
            {%
              \filldraw[titlebg] (\i +.5, 1.5) circle (.28);
            };
          };
          \erem = #1 -6;
          for \i in {0, ..., \erem}{%
            {%
              \filldraw[titlebg] (\i +.5, .5) circle (.28);
            };
          };
        } else {%
          \erem = #1 -1;
          if \erem >= 0 then {%
            {%
              \draw (0, 0) grid (5, 2);
            };
            for \i in {0, ..., \erem}{%
              {%
                \filldraw[titlebg] (\i +.5, 1.5) circle (.28);
              };
            };
          };
        };
      }
    }
  },
  pics/counting/.style 2 args={%  number, scale
    code={%
      \tikzmath{%
        integer \N, \fullTens, \rows;
        \rows = #1/50;
        \fullTens = (#1 -\rows*50)/10;
        \N = #1 -\rows*50 -\fullTens*10;
        if \rows>0 then {%
          for \j in {1, ..., \rows}{%
            for \i in {0, ..., 4}{%
              {%
                \path (\i*6*#2, {(-\j +1)*3*#2}) pic[scale=#2] {full tens};
              };
            };
          };
        };
        if \fullTens>0 then {%
          for \i in {1, ..., \fullTens}{%
            {%
              \path ({(\i -1)*6*#2}, -\rows*3*#2) pic[scale=#2] {full tens};
            };
          };
        };
        {%
          \path (\fullTens*6*#2, -\rows*3*#2) pic[scale=#2] {tens=\N};
        };
      }
    }
  }
}

%%%#1 = first number, #2 = last number, #3 = number the counting starts on
\newcommand{\TFCounting}[3]{\begin{tikzpicture}
\only<1-\fpeval{#3-1}>{\path (0, 0) pic {counting={\fpeval{#1}}{.35}}};
  \foreach \x in {#3,...,\fpeval{#3+#2-#1-1}} \only<\x>{\path (0, 0) pic {counting={\fpeval{#1-#3+1+\x}}{.35}}};
\end{tikzpicture}}


\begin{frame}
\TFCounting{60}{65}{3}
\end{frame}
\end{document}

请注意代码

\TFCounting{60}{65}{3}

给出 60 的十帧表示,直到幻灯片 3 开始逐帧计数到 65。

问题现在有了答案,谢谢丹尼尔!

谢谢,温迪

答案1

在此处输入图片描述

我创建了一个pic名为的对象counting,它接受两个参数,数字和比例。在图中你可以看到 124 的结果。它使用了库math和另外两个pic对象,tensfull tens

代码

\documentclass[11pt, margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{math}
\definecolor{titlebg}{RGB}{231, 182, 13}
\begin{document}

\tikzset{%
  pics/full tens/.style={%
    code={%
      \fill[titlebg!20] (0, 0) rectangle (5, 2);
      \draw[titlebg] (0, 0) grid (5, 2);
      \foreach \j in {0, 1}{%
        \foreach \i in {0, ..., 4}{%
          \filldraw[titlebg] (\i +.5, \j +.5) circle (.28);
        }
      }
    }
  },
  pics/tens/.style={%
    code={%
      \tikzmath{%
        integer \erem;
        if #1>5 then {%
          {%
            \draw (0, 0) grid (5, 2);
          };
          for \i in {0, ..., 4}{
            {%
              \filldraw[titlebg] (\i +.5, 1.5) circle (.28);
            };
          };
          \erem = #1 -6;
          for \i in {0, ..., \erem}{%
            {%
              \filldraw[titlebg] (\i +.5, .5) circle (.28);
            };
          };
        } else {%
          \erem = #1 -1;
          if \erem >= 0 then {%
            {%
              \draw (0, 0) grid (5, 2);
            };
            for \i in {0, ..., \erem}{%
              {%
                \filldraw[titlebg] (\i +.5, 1.5) circle (.28);
              };
            };
          };
        };
      }
    }
  },
  pics/counting/.style 2 args={%  number, scale
    code={%
      \tikzmath{%
        integer \N, \fullTens, \rows;
        \rows = #1/50;
        \fullTens = (#1 -\rows*50)/10;
        \N = #1 -\rows*50 -\fullTens*10;
        if \rows>0 then {%
          for \j in {1, ..., \rows}{%
            for \i in {0, ..., 4}{%
              {%
                \path (\i*6*#2, {(-\j +1)*3*#2}) pic[scale=#2] {full tens};
              };
            };
          };
        };
        if \fullTens>0 then {%
          for \i in {1, ..., \fullTens}{%
            {%
              \path ({(\i -1)*6*#2}, -\rows*3*#2) pic[scale=#2] {full tens};
            };
          };
        };
        {%
          \path (\fullTens*6*#2, -\rows*3*#2) pic[scale=#2] {tens=\N};
        };
      }
    }
  }
}

\begin{tikzpicture}
  \path (0, 3) node[right, scale=2] {124 with \texttt{scale=0.75}};
  \path (0, 0) pic {counting={123}{.75}};
  
  \begin{scope}[yshift=-7.5cm]
    \path (0, 0) node[right, scale=2] {using \texttt{tens=8}:}
    (6, -1) pic {tens=8};
  \end{scope}
  
\end{tikzpicture}
\end{document}

答案2

您可能会发现这很有用。它提供了一个包含 1 到 10 个图像的 PDF。只需使用\includegraphics[page=10]{filename}(例如)。所有工作都是预先计算的。您可以使用 [inner sep] 在每个图像周围创建边框。

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}
\usepackage{xcolor}
\definecolor{titlebg}{RGB}{231, 182, 13}

\newsavebox{\digit}
\savebox\digit{\tikz[baseline=(current bounding box.center)]{\filldraw[color=titlebg] circle (.28);}}

\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes, row sep=-.4pt, column sep=-.4pt, inner sep=0pt,
  nodes={minimum width=1cm, minimum height=1cm, draw=black}]{
  \usebox\digit & \null & \null & \null & \null \\
  \null & \null & \null & \null & \null \\};
\end{tikzpicture}

\begin{tikzpicture}
\matrix[matrix of nodes, row sep=-.4pt, column sep=-.4pt, inner sep=0pt,
  nodes={minimum width=1cm, minimum height=1cm, draw=black}]{
  \usebox\digit & \usebox\digit & \null & \null & \null \\
  \null & \null & \null & \null & \null \\};
\end{tikzpicture}

\begin{tikzpicture}
\matrix[matrix of nodes, row sep=-.4pt, column sep=-.4pt, inner sep=0pt,
  nodes={minimum width=1cm, minimum height=1cm, draw=black}]{
  \usebox\digit & \usebox\digit & \usebox\digit & \null & \null \\
  \null & \null & \null & \null & \null \\};
\end{tikzpicture}

\begin{tikzpicture}
\matrix[matrix of nodes, row sep=-.4pt, column sep=-.4pt, inner sep=0pt,
  nodes={minimum width=1cm, minimum height=1cm, draw=black}]{
  \usebox\digit & \usebox\digit & \usebox\digit & \usebox\digit & \null \\
  \null & \null & \null & \null & \null \\};
\end{tikzpicture}

\begin{tikzpicture}
\matrix[matrix of nodes, row sep=-.4pt, column sep=-.4pt, inner sep=0pt,
  nodes={minimum width=1cm, minimum height=1cm, draw=black}]{
  \usebox\digit & \usebox\digit & \usebox\digit & \usebox\digit & \usebox\digit \\
  \null & \null & \null & \null & \null \\};
\end{tikzpicture}

\begin{tikzpicture}
\matrix[matrix of nodes, row sep=-.4pt, column sep=-.4pt, inner sep=0pt,
  nodes={minimum width=1cm, minimum height=1cm, draw=black}]{
  \usebox\digit & \usebox\digit & \usebox\digit & \usebox\digit & \usebox\digit \\
  \usebox\digit & \null & \null & \null & \null \\};
\end{tikzpicture}

\begin{tikzpicture}
\matrix[matrix of nodes, row sep=-.4pt, column sep=-.4pt, inner sep=0pt,
  nodes={minimum width=1cm, minimum height=1cm, draw=black}]{
  \usebox\digit & \usebox\digit & \usebox\digit & \usebox\digit & \usebox\digit \\
  \usebox\digit & \usebox\digit & \null & \null & \null \\};
\end{tikzpicture}

\begin{tikzpicture}
\matrix[matrix of nodes, row sep=-.4pt, column sep=-.4pt, inner sep=0pt,
  nodes={minimum width=1cm, minimum height=1cm, draw=black}]{
  \usebox\digit & \usebox\digit & \usebox\digit & \usebox\digit & \usebox\digit \\
  \usebox\digit & \usebox\digit & \usebox\digit & \null & \null \\};
\end{tikzpicture}

\begin{tikzpicture}
\matrix[matrix of nodes, row sep=-.4pt, column sep=-.4pt, inner sep=0pt,
  nodes={minimum width=1cm, minimum height=1cm, draw=black}]{
  \usebox\digit & \usebox\digit & \usebox\digit & \usebox\digit & \usebox\digit \\
  \usebox\digit & \usebox\digit & \usebox\digit & \usebox\digit & \null \\};
\end{tikzpicture}

\begin{tikzpicture}
\matrix[matrix of nodes, row sep=-.4pt, column sep=-.4pt, inner sep=0pt,
  nodes={minimum width=1cm, minimum height=1cm, draw=titlebg, fill=titlebg!20}]{
  \usebox\digit & \usebox\digit & \usebox\digit & \usebox\digit & \usebox\digit \\
  \usebox\digit & \usebox\digit & \usebox\digit & \usebox\digit & \usebox\digit \\};
\end{tikzpicture}
\end{document}

相关内容