# How can I write a loop that increments a node position and its label within a certain range?

Here's the code I have so far. I'm using tikzpicture:

\begin{tikzpicture}
\draw[->,very thick] (-5, -5) -- (5, -5); %x-axis
\draw[->,very thick] (-4, -5.5) -- (-4, 6); %y-axis
\draw [step=0.5cm] (-4.9, -5.9) grid (4.9, 5.9);
\node[thick] () at (-4,6.2) {$\textbf{y}$};
\node[thick] () at (5.2,-5) {$\textbf{x}$};
\node[very thick, rotate=90] at (-5.3,0) {\LARGE{\textit{Cost}}};
\node[very thick] at (0,-6.7) {\LARGE{\textit{Months}}};
\node[very thick] at (-4,-6.1) {$0$};
\node[very thick] at (-3.5,-6.1) {$1$};
\node[very thick] at (-3,-6.1) {$2$};
\node[very thick] at (-2.5,-6.1) {$3$};
\node[very thick] at (-2,-6.1) {$4$};
\node[very thick] at (-1.5,-6.1) {$5$};
\node[very thick] at (-1,-6.1) {$6$};
\node[very thick] at (-.5,-6.1) {$7$};
\node[very thick] at (0,-6.1) {$8$};
\node[very thick] at (.5,-6.1) {$9$};
\node[very thick] at (1,-6.1) {$10$};
\node[very thick] at (1.5,-6.1) {$11$};
\node[very thick] at (2,-6.1) {$12$};
\node[very thick] at (2.5,-6.1) {$13$};
\node[very thick] at (-5.3,-5) {$0$};
\node[very thick] at (-5.3,-4.5) {$5$};
\node[very thick] at (-5.3,-4) {$10$};
\node[very thick] at (-5.3,-3.5) {$15$};
\node[very thick] at (-5.3,-3) {$20$};
\node[very thick] at (-5.3,-2.5) {$25$};
\node[very thick] at (-5.3,-2) {$30$};
\filldraw (-4,-4) circle (0.1cm);
\draw[<->,very thick] (-4,-4) -- (4.5,4.5);%steps are 0.5cm, so path is (-4,-4) to (4,4)
\end{tikzpicture}

• Welcome to TeX.SX! You might want to add a bit more information about the expected result. It's hard to understand what exactly you are asking for. – siracusa Nov 16 at 12:54
• Thanks! I'm trying to find a simple way to number the grid lines in a tikz xy coordinate plane. I'm making linear function problems for my 8th grade Algebra class and I need some coordinate planes. JPi solved the problem! – Glenn Blakney Nov 16 at 17:04

I'm not entirely sure what you're asking, but my guess is that this covers it.

EDIT In response to OP's question, the syntax is as follows. Consider \foreach \y in {0,5,...,30} \node at (-5.3,{-5+0.1*\y}) {$\y$}; What that means is create a variable \y and make it run from 0 to 30 in steps of 5. The -5+0.1*\y business is the y-coordinate of the node, so the first one is at -5. the second one at -4.5, etcetera.

\documentclass{article}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\draw[->,very thick] (-5, -5) -- (5, -5); %x-axis
\draw[->,very thick] (-4, -5.5) -- (-4, 6); %y-axis
\draw [step=0.5cm] (-4.9, -5.9) grid (4.9, 5.9);
\node[thick] () at (-4,6.2) {$\textbf{y}$};
\node[thick] () at (5.2,-5) {$\textbf{x}$};
\node[very thick, rotate=90] at (-5.3,0) {\LARGE{\textit{Cost}}};
\node[very thick] at (0,-6.7) {\LARGE{\textit{Months}}};
\foreach \y in {0,5,...,30}
\node at (-5.3,{-5+0.1*\y}) {$\y$};
\foreach \x in {0,...,13}
\node[very thick] at ({-4+0.5*\x},-6.1) {$\x$};
\filldraw (-4,-4) circle (0.1cm);
\draw[<->,very thick] (-4,-4) -- (4.5,4.5);%steps are 0.5cm, so path is (-4,-4) to (4,4)
\end{tikzpicture}

\end{document}

• JPi: thank you very much! this does exactly what I'm looking for! It's too bad that the \draw[step= ] doesn't include some automatic numbering command. If you have a spare moment, would you indulge a NOOB with a brief explanation of the \foreach and \x, \y commands? Is \x and \y naming that variable so that I can give it a value later? What does the * do in the increment command {-4+0.5*\x}? Thanks again! – Glenn Blakney Nov 16 at 16:53
• See my edit: hope that explains it. – JPi Nov 16 at 19:46

A small variation of the @JPi answer (+1)...

Edit: I don't see any reasonable reason to draw axes from (-5, -5) to (5, -5) and from (-4, -5.5) to (-4, 6) and than point at (-4,-4) designate as diagram axis origin. It is much easier to draw axes from the origin at (0,0).

So the difference in my approach and your MWE are in determinations of coordinates of all image elements. Also I remove all not needed nodes options and for x and y nodes styles use syntax more consistent with tikz.

\documentclass[tikz, margin=3mm]{standalone}

\begin{document}

\begin{tikzpicture}
\draw[->,very thick] (-0.8,0) -- node[below=12mm,font=\LARGE\itshape] {Months}
++ ( 9.8,0) node[right] {$\mathbf{x}$};%x-axis
\draw[->,very thick] (0,-0.8) -- node[above=9mm,sloped,font=\LARGE\itshape] {Cost}
++ (0,10.8) node[above] {$\mathbf{y}$};%y-axis
\draw [step=5mm] (-0.9,-0.9) grid (8.9, 9.9);
%
\foreach \i [count=\ii] in {0,0.5,...,6}
\node[below] at (\i,-0.9) {\ii};
\foreach \i in {0,5,...,30}
\node[left] at (-0.9,\i/10) {\i};
\filldraw (0,1) circle (1mm);
\draw[->,very thick] (0,1) -- (8.5,9.5);
\end{tikzpicture}
\end{document}


• useful addition +1 – JPi Nov 16 at 16:26