0

I am using the following code to display a number line, but I have had to write in the equation for each line, can you help me to make this equation automated?

\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}  

The lines of code I have indicated are L21 and L42. Thanks

1 Answer 1

1

In \addsubnumlinetoright (sign tex-sx doesn't support diff highlighting),

% 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$};

In \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$};

Full example:

\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}

enter image description here

1
  • Thanks, and more thanks for the explanation!
    – Paul A
    Jun 22 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.