我怎样才能绘制点的总和?

我怎样才能绘制点的总和?

我使用 Geogebra 手工绘制了 4 个点 + 2 个点 + 1 个点 = 7 个点的总和。我的代码

\documentclass[12pt]{standalone}
\usepackage{pgf,tikz}
\usepackage{mathrsfs}
\usetikzlibrary{arrows}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\clip(-1.3,-1.5) rectangle (17.,1.3);
\draw (3.84,0.2) node[anchor=north west] {$+$};
\draw (6.7,0.2) node[anchor=north west] {+};
\draw (8.75,0.2) node[anchor=north west] {=};
\begin{scriptsize}
\draw [fill=black] (0.,0.) circle (1.5pt);
\draw [fill=black] (1.,0.) circle (1.5pt);
\draw [fill=black] (2.,0.) circle (1.5pt);
\draw [fill=black] (3.,0.) circle (1.5pt);
\draw [fill=black] (5.,0.) circle (1.5pt);
\draw [fill=black] (6.,0.) circle (1.5pt);
\draw [fill=black] (8.,0.) circle (1.5pt);
\draw [fill=black] (10.,0.) circle (1.5pt);
\draw [fill=black] (11.,0.) circle (1.5pt);
\draw [fill=black] (12.,0.) circle (1.5pt);
\draw [fill=black] (13.,0.) circle (1.5pt);
\draw [fill=black] (14.,0.) circle (1.5pt);
\draw [fill=black] (15.,0.) circle (1.5pt);
\draw [fill=black] (16.,0.) circle (1.5pt);
\end{scriptsize}
\end{tikzpicture}
\end{document}

在此处输入图片描述

有没有办法,当我在左侧输入 4 个点 + 2 个点 + 1 个点时,我会自动在右侧得到结果 7 个点?也就是说,当我输入

在此处输入图片描述

我可以得到

在此处输入图片描述

答案1

在第一个点之前和最后一个点之后更新了一些“空格”。

\foreach可用于自动执行某些任务,例如:

\documentclass[12pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \fill[radius=1.5pt]
    \foreach \i in {1, ..., 4} { ++(1, 0) circle[] }
    ++(1, 0) node {$+$}
    \foreach \i in {1, ..., 2} { ++(1, 0) circle[] }
    ++(1, 0) node {$+$}
    ++(1, 0) circle[]
    ++(1, 0) node {$=$}
    \foreach \i in {1, ..., 7} { ++(1, 0) circle[] }
    ++(.5, 0) (.5, 0) % space before/after first/last dot
  ;
\end{tikzpicture}
\end{document}

结果

加上一些宏:

\documentclass[12pt]{standalone}
\usepackage{tikz}
\newcommand*{\myfordots}[1]{%
  \expandafter\myfordotsaux\expandafter{\the\numexpr(#1)\relax}%
}
\newcommand*{\myfordotsaux}[1]{%
  \foreach \i in {1, ..., #1} { ++(1, 0) circle[] }
}
\newcommand*{\myop}[1]{%
  ++(1, 0) node {$#1$}
}
\begin{document}
\begin{tikzpicture}
  \fill[radius=1.5pt]
    \myfordots{4}
    \myop{+}
    \myfordots{2}
    \myop{+}
    \myfordots{1}
    \myop{=}
    \myfordots{4 + 2 + 1}
    ++(.5, 0) (.5, 0) % space before/after first/last dot
  ;
\end{tikzpicture}
\end{document}

答案2

下面是一个计算总和的命令:

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

\newcounter{totalpoints}

\newcommand\AddPoints[2][\enspace$+$\enspace]{%
  \setcounter{totalpoints}{0}%
  \def\nextitem{\def\nextitem{#1}}%
  \renewcommand*{\do}[1]{\nextitem\ifnum##1>0\relax\DrawPoints{##1}\fi\addtocounter{totalpoints}{##1}}% 
  \docsvlist{#2}% 
  \enspace$=$\enspace\DrawPoints{\thetotalpoints}
}

\newcommand\DrawPoints[1]{%
\begin{tikzpicture}[x=0.25cm,baseline=-.6ex]
\foreach \Valor in {1,...,#1}%
{  \filldraw (\Valor,0) circle [radius=1.5pt];}
\end{tikzpicture}%
}

\begin{document}

\AddPoints{4,2,1}

\AddPoints{5,3}

\AddPoints{1,2,3,4,5}

\AddPoints{2,3,0}

\AddPoints{0,0,3}

\AddPoints{0,1,0}

\end{document}

在此处输入图片描述

答案3

与其他答案有点不同,但也有点相似。

\documentclass[tikz,border=5]{standalone} 
\tikzset{dots/.style args={#1 #2 #3}{insert path={[fill, radius=0.1em]
    ++(-#1-#3-2,0)
    \foreach \i in {1,...,#1}{ ++(1, 0) circle [] }
    ++(1.5, 0) node {$\ifx#2*\times\else\ifx#2/\div\else#2\fi\fi$} ++(.5, 0)
    \foreach \i in {1,...,#3}{ ++(1, 0) circle [] }
    ++(1.5, 0) node {$=$} ++(.5, 0)
    \pgfextra{\pgfmathparse{int(#1#2#3)}\let\n=\pgfmathresult}
    \foreach \i in {1,...,\n}{ ++(1, 0) circle [] }
}}}
\begin{document} 
\begin{tikzpicture}[x=1em]
\path (0, 0) [dots={1 + 2}];
\path (0, 1) [dots={7 + 3}];
\path (0, 2) [dots={8 - 4}];
\path (0, 3) [dots={2 * 5}];
\path (0, 4) [dots={8 / 4}];
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容