# Using \foreach loop for negative values with decimal points

Since \foreach loop does not accept values with decimal point; I faked the placing of decimal point in the x axis.

I could not accomplish that in the y axis since it uses negative values, in which case the negative sign is placed after the decimal point.

To fake using the negative values with decimal point I used the following code from the answer to this question

Is there a simpler way to automate this without installing additional packages (the computer in my workplace does not allow installing any program for security reason).

\documentclass{beamer}
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[t]
\frametitle{}
\begin{tikzpicture}[scale=.9, transform shape]
\draw [thick] (0,0) -- (7,0);
\draw [thick] (0,0) -- (0,5);
%begin x/y tickmarks
\pgfmathsetmacro{\incrmnty}{.6}
\pgfmathsetmacro{\incrmntx}{1}
%begin y tickmarks
\begin{scope}[xshift=0, yshift=-.6cm]
\foreach[count=\j] \i in {-6,...,2}{
\ifnum\i=0
\draw[thick] (-.14,\j*\incrmnty) node[xshift=-.2cm]{0}coordinate(\i) -- +(0:.2);
\else
\ifnum\i=-6
\draw[thick] (-.2,\j*\incrmnty) node[xshift=-.28cm]{-.6}coordinate(\i) -- +(0:.2);
\else
\ifnum\i=-4
\draw[thick] (-.2,\j*\incrmnty) node[xshift=-.28cm]{-.4}coordinate(\i) -- +(0:.2);
\else
\ifnum\i=-2
\draw[thick] (-.2,\j*\incrmnty) node[xshift=-.28cm]{-.2}coordinate(\i) -- +(0:.2);
\else
\ifnum\i=2
\draw[thick] (-.2,\j*\incrmnty) node[xshift=-.28cm]{.2}coordinate(\i) -- +(0:.2);
\fi \fi \fi \fi\fi}
\end{scope}
%end y tick marks
%begin x tickmarks
\begin{scope}[xshift=-1cm, yshift=0cm]
\foreach[count=\j] \i in {0,...,6}{
\ifnum\i=0
\draw[thick] (\j*\incrmntx,0) -- +(-90:.2) node[yshift=-.26cm]{0}coordinate(\i);
\else
\draw [thick] (\j*\incrmntx,0) -- +(-90:.2) node[yshift=-.26cm]{.\i}coordinate(\i);
\fi}
\end{scope}
%end x tick marks
%end x/y tickmarks
\end{tikzpicture}
\end{frame}
\end{document}


In case you don't want to load any other package and with so easy list of numbers I think following solution is easier than yours. I've written all numbers I want as tick labels, they can be decimals.

\documentclass{beamer}
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[t]
\frametitle{}
\begin{tikzpicture}[scale=.9, transform shape]
\draw [thick] (0,0) -- (7,0);
\draw [thick] (0,0) -- (0,5);
%begin x/y tickmarks
\pgfmathsetmacro{\incrmnty}{.6}
\pgfmathsetmacro{\incrmntx}{1}
%begin y tickmarks
\foreach\i [count=\j from 0] in {-.6,-.4,-.2,0,.2}
\draw[thick] (0,\j*\incrmnty) coordinate(\i) --++(180:.2) node[left]{\i};
\foreach \i [count=\j from 0] in {0,.1,.2,.3,.4,.5,.6}
\draw[thick] (\j*\incrmntx,0) coordinate(\i) -- ++(-90:.2) node[below]{\i};
%end x tick marks
%end x/y tickmarks
\end{tikzpicture}
\end{frame}
\end{document}


• Thank you very very much for your answer. I just want to know what does [count=\j from 0] mean, and how can I use it for different situations. – Hany Dec 18 '18 at 5:25
• @Hany Please look at this. They explain it better than me. – Ignasi Dec 18 '18 at 8:07