# Sum and Difference on the number line using TikZ

I am trying to replicate the idea of sum and difference of real numbers on the number line and have been successful in some way. For example, the code:

\documentclass[letterpaper]{article}
\usepackage{fullpage}
\usepackage{amsmath,amssymb,amsthm,enumitem}
\usepackage[dvipsnames]{xcolor}
%\usepackage{array,multicol}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[x=0.75cm,>=stealth]
\draw[<->] (-5,0)--(5,0);
\foreach \x in {-4,...,4}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\node[below] at (-5,-5pt) {$\ldots$};
\node[below] at (5,-5pt) {$\ldots$};
\fill (3,0) circle (2pt);
\fill (-4,0) circle (2pt);
\draw[<-,out=45,in=135,color=blue] (-4,0) to (-3,0) to (-2,0) to (-1,0) to (0,0) to (1,0) to (2,0) to (3,0);
\node[color=OrangeRed] at (3,-0.75) {\small Start};
\node[color=Cerulean] at (-4,-0.75) {\small End};
\node at (-0.5,0.5) {\small Move 7 units to the \emph{left}};
%\draw[->,shorten >=5pt,shorten <=5pt,out=45,in=135,distance=0.5cm] (0,0) to (1,0);
\end{tikzpicture}
\end{document}


yeilds:

What I tried to do now was to create a \newcommand in which one can indicate a moving arrow to the right as a sum and left as a difference. This is what I have so far:

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\begin{tikzpicture}[out=45,in=135,relative,>=stealth]
\draw[<->] (-5,0)--(5,0);
\foreach \x in {-4,...,4}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\fill (#1,0) circle (2pt);
\fill (#2,0) circle (2pt);
\foreach \i in {#1,...,#2}% Thinking on using [remember=\i as \lasti (initially #1)]
{
\draw[#3] (\i,0) to (\i+1,0);% Try a \breakforeach but don't know
}
\end{tikzpicture}}

\begin{document}

\end{document}


Which yields:

What I am trying to achieve is the first image but within one command. I know that my error in the code above would be where \draw[#3] (\i,0) to (\i+1,0); is found as the \i+1 gives the extra jump. The other problem would be to determine the text in the middle of the counting and that it automatically determines to the right or left. This can be probably done with a \if and \else statement probably. Note also that in the first example, the arrows are not shown for every jump and in the second yes. I like the first one better. Any assistance it the matter will be highly appreciated.

-

Some explanations :

There are several methods to modify the last argument of the foreach macro like \number\numexpr#2 - 1\relax . If you don't like to use \numexpr you need another way but it's possible.

update

Another possibility without \numexpr from etex is :

\pgfmathsetmacro{\End}{#2-1}
\draw[#3,shorten >=2pt]
\foreach \i in {#1,...,\End}{%
(\i,0) to (\i+1,0)
} ;


For the arrows you need to know exactly what is a path. If you have several arrows it's because you have drawn several paths with \draw inside the loop. You just need to put the command outside the loop and ;outside the loop too. Now you use only one path and you get only one arrow.

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\begin{tikzpicture}[out=45,in=135,relative,>=stealth]
\draw[<->] (-5,0)--(5,0);
\foreach \x in {-4,...,4}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\fill (#1,0) circle (2pt);
\fill (#2,0) circle (2pt);
\draw[#3,shorten >=2pt]
\foreach \i in {#1,...,\number\numexpr#2 - 1\relax}{%
(\i,0) to (\i+1,0)
} ;
\end{tikzpicture}}

\begin{document}

\end{document}


more possibilities

I add some codes to write Start and End and the number of jumps.

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\begin{tikzpicture}[out=45,in=135,relative,>=stealth]
\draw[<->] (-5,0)--(5,0);
\foreach \x in {-4,...,4}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\fill (#1,0) circle (2pt);
\fill (#2,0) circle (2pt);

\draw[#3,shorten >=2pt]
\foreach \i in {#1,...,\number\numexpr#2 - 1\relax}{%
(\i,0) to (\i+1,0)
} ;
\node[color=OrangeRed] at (#2,-0.75) {\small End};
\node[color=Cerulean] at (#1,-0.75) {\small Start};

\node at (\number\numexpr #2/2+#1/2\relax,0.5) {\small Move \number\numexpr#2-#1\relax\ units to the \emph{right}};
\end{tikzpicture}}

\begin{document}

\end{document}


left and Right

I write the code with the first idea through my head. I think it's possible to get a better and shorter method. Be careful, the expressions are wrong to get the number of jumps.

update version 3

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\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,0.5) {\small Move \number\numexpr#2-#1\relax\ units to the \emph{right}};
\end{tikzpicture}}

\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,0.5) {\small Move \number\numexpr-#2+#1\relax\ units to the \emph{left}};
\end{tikzpicture}}

\begin{document}


Probably for the case of the size of the number line, that the \draw[<->] (-5,0)--(5,0); look something like \draw[<->] (#1-1.5,0)--(#2+1.5,0);> Would that be feasible? I guess if the diagram is too large then a scale mechanism can be put into place automatically for aesthetics only. Then why would the expression be wrong if you use only + or - numbers. – azetina May 23 '12 at 19:10
In the your sample \addsubnumlinetoright{-3}{-1}{->,color=MidnightBlue} the text doesn't seem to be centered relative to the units moved from -3 to -1. Why? – azetina May 23 '12 at 19:41
error in the expression. I think it's a bad use of \numexpr. It's more easy to use \pgfmath`. see update 3 – Alain Matthes May 23 '12 at 20:03