# How to make Loop?

I need to make for loop analog in LaTeX. My code has following form

\documentclass[9pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}[scale=1.2, font=\tiny]
\begin{axis}[
axis x line=middle,
axis y line=middle,
domain=-5:5,
restrict y to domain=-5:5,
xmin=-5,    xmax=5,
ymin=-5,    ymax=5,
x=0.5cm,
y=0.5cm,
grid=both,
xtick={-5,...,5},
ytick={-5,...,5},
xlabel=$x$,
ylabel=$y$,
every axis x label/.style={
at={(ticklabel* cs:1)},
anchor=west,},
every axis y label/.style={
at={(ticklabel* cs:1)},
anchor=south,},
]
\end{axis}
\end{tikzpicture}
\end{document}


Is it possible to write for loop to automatically make "n" lines?

• Do you want an example of the syntax? Look here for a simple case: texample.net/tikz/examples/cycle That website also has more complicated cases... – darthbith Dec 19 '14 at 14:05
• If you specify in your question how to make 1 line, then we can show you how to make "n" lines. If the lines are really all the same, then it's very simple. – musarithmia Dec 19 '14 at 14:10
• texample.net/tikz/examples/cycle this example was very helpful. – Giorgi Dec 19 '14 at 14:39

Use two nested \foreach loops. Since \k depends on \n you could probably reduce it to one loop. I'm not a mathematician but I think the TeX part is correct here.

\documentclass[9pt]{article}
\usepackage{tikz,pgfplots}

\newcommand{\nMAX}{20}
\newcommand{\kMAX}{19}

\begin{document}
\begin{tikzpicture}[scale=1.2, font=\tiny]
\begin{axis}[
axis x line=middle,
axis y line=middle,
domain=-5:5,
restrict y to domain=-5:5,
xmin=-5,    xmax=5,
ymin=-5,    ymax=5,
x=0.5cm,
y=0.5cm,
grid=both,
xtick={-5,...,5},
ytick={-5,...,5},
xlabel=$x$,
ylabel=$y$,
every axis x label/.style={
at={(ticklabel* cs:1)},
anchor=west,},
every axis y label/.style={
at={(ticklabel* cs:1)},
anchor=south,},
]

\foreach \n in {1, ..., \nMAX}
\foreach \k in {0, ..., \kMAX}
{

• I get some errors from your code; perhaps you're using an earlier version of pgfplots? – musarithmia Dec 19 '14 at 14:47