我正在使用以下代码来显示数字线,但我必须为每条线写出等式,您能帮我使这个等式自动化吗?
\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\newcommand{\addsubnumlinetoright}[3]{%
\begin{tikzpicture}[out=45,in=135,relative,>=stealth]
\draw[<->] (#1-2,0)--(#2+2,0);
\foreach \x in {\number\numexpr#1-1\relax,...,\number\numexpr#2+1\relax}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\fill (#1,0) circle (2pt);
\fill (#2,0) circle (2pt);
\pgfmathsetmacro{\End}{#2-1}
\draw[#3,shorten >=2pt]
\foreach \i in {#1,...,\End}{%
(\i,0) to (\i+1,0)
} ;
\node[color=OrangeRed] at (#2,-0.75) {\small End};
\node[color=Cerulean] at (#1,-0.75) {\small Start};
\pgfmathsetmacro{\xtxt}{(#1+#2)/2}
\node at (\xtxt,1) {$-5 +8 = 3$}; %code I would like to change
\node at (\xtxt,0.5) {\small Move \number\numexpr#2-#1\relax\ units to the \emph{right}};
\end{tikzpicture}}
\newcommand{\addsubnumlinetoleft}[3]{%
\begin{tikzpicture}[out=135,in=45,>=stealth]
\draw[<->] (#2-2,0)--(#1+2,0);
\foreach \x in {\number\numexpr#2-1\relax,...,\number\numexpr#1+1\relax}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\fill (#1,0) circle (2pt);
\fill (#2,0) circle (2pt);
\pgfmathsetmacro{\End}{#2+1}
\draw[#3,shorten >=2pt]
\foreach \i in {#1,...,\End}{%
(\i,0) to (\i-1,0)
} ;
\node[color=OrangeRed] at (#2,-0.75) {\small End };
\node[color=Cerulean] at (#1,-0.75) {\small Start };
\pgfmathsetmacro{\xtxt}{(#1+#2)/2}
\node at (\xtxt,1) {$2 - 6= -4$}; % Code I would like to change
\node at (\xtxt,0.5) {\small Move \number\numexpr-#2+#1\relax\ units to the \emph{left}};
\end{tikzpicture}}
\begin{document}
\begin{center}
\addsubnumlinetoright{3}{7}{->,color=MidnightBlue}
\addsubnumlinetoright{-5}{-3}{->,color=MidnightBlue}
\addsubnumlinetoright{-5}{3}{->,color=MidnightBlue}
\addsubnumlinetoleft{2}{-4}{->,color=MidnightBlue}
\end{center}
\end{document}
我指出的代码行是 L21 和 L42。谢谢
答案1
在\addsubnumlinetoright
(sign tex-sx 不支持diff
高亮显示)中,
% before
\node at (\xtxt,1) {$-5 +8 = 3$}; %code I would like to change
% after
\node at (\xtxt,1) {$#1 + \the\numexpr#2-(#1)\relax = #2$};
在\addsubnumlinetoleft
:
% before
\node at (\xtxt,1) {$2 - 6= -4$}; % Code I would like to change
% after
\node at (\xtxt,1) {$#1 - \the\numexpr#1-(#2)\relax = #2$};
完整示例:
\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\newcommand{\addsubnumlinetoright}[3]{%
\begin{tikzpicture}[out=45,in=135,relative,>=stealth]
\draw[<->] (#1-2,0)--(#2+2,0);
\foreach \x in {\number\numexpr#1-1\relax,...,\number\numexpr#2+1\relax}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\fill (#1,0) circle (2pt);
\fill (#2,0) circle (2pt);
\pgfmathsetmacro{\End}{#2-1}
\draw[#3,shorten >=2pt]
\foreach \i in {#1,...,\End}{%
(\i,0) to (\i+1,0)
} ;
\node[color=OrangeRed] at (#2,-0.75) {\small End};
\node[color=Cerulean] at (#1,-0.75) {\small Start};
\pgfmathsetmacro{\xtxt}{(#1+#2)/2}
\node at (\xtxt,1) {$#1 + \the\numexpr#2-(#1)\relax = #2$}; %code I would like to change
\node at (\xtxt,0.5) {\small Move \number\numexpr#2-#1\relax\ units to the \emph{right}};
\end{tikzpicture}}
\newcommand{\addsubnumlinetoleft}[3]{%
\begin{tikzpicture}[out=135,in=45,>=stealth]
\draw[<->] (#2-2,0)--(#1+2,0);
\foreach \x in {\number\numexpr#2-1\relax,...,\number\numexpr#1+1\relax}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\fill (#1,0) circle (2pt);
\fill (#2,0) circle (2pt);
\pgfmathsetmacro{\End}{#2+1}
\draw[#3,shorten >=2pt]
\foreach \i in {#1,...,\End}{%
(\i,0) to (\i-1,0)
} ;
\node[color=OrangeRed] at (#2,-0.75) {\small End };
\node[color=Cerulean] at (#1,-0.75) {\small Start };
\pgfmathsetmacro{\xtxt}{(#1+#2)/2}
\node at (\xtxt,1) {$#1 - \the\numexpr#1-(#2)\relax = #2$}; % Code I would like to change
\node at (\xtxt,0.5) {\small Move \number\numexpr-#2+#1\relax\ units to the \emph{left}};
\end{tikzpicture}}
\begin{document}
\begin{center}
\addsubnumlinetoright{3}{7}{->,color=MidnightBlue}
\addsubnumlinetoright{-5}{-3}{->,color=MidnightBlue}
\addsubnumlinetoright{-5}{3}{->,color=MidnightBlue}
\addsubnumlinetoleft{2}{-4}{->,color=MidnightBlue}
\end{center}
\end{document}