# Using \foreach to manage ticks in axis, Tikz

I want to add minor distance between y ticks. The default distance is 0.5, I need to set for 0.2. It seems easy to set different space for ticks according to pgfplots, in 4.15, page 271.

I add the suggested ytick={\foreach \y in {.2,.4,.6}{\y}, varying to get results, but I couldn't. Help me, please.

My code graph is, with \usepackage{tikz} and \usepackage{pgfplots}, in the preamble:

\begin{figure}[!h]
\centering
\caption{Rigidez Efectiva para Columnas Rectangulares}
\begin{tikzpicture}[domain=0:.7]
\begin{axis}[xlabel={$P/A_{g}f'{c}$},ylabel={$EI_{ef}/EI_{g}$}, xmin=-.1,xmax=.7,ymin=0,ymax=1.4, ytick={\foreach \y in {.2,.4,.6}{\y}}]
\addplot[domain=-.1:.1,color=red,very thick]{.3};
\addplot[domain=.1:.5,color=red,very thick]{.2+\x};
\addplot[domain=.5:.7,color=red,very thick]{.7};

\end{axis}
\end{tikzpicture}
\end{figure}

-
\foreach is not needed in tick specification. Simply use ytick={0.2,0.4,0.6} and it works as expected. – Paul Gessler Apr 2 '14 at 19:28
It's right, it works, thanks. The pgfplots manual is complicated. But in which cases I must use \foreach command? – Isai Apr 2 '14 at 19:33
It works with dots also: ytick={.2,.4,...,1.2} – Isai Apr 2 '14 at 19:34
\foreach is used very rarely; the mention of \foreach in 4.15.1 of the manual is telling you how the ticks are created internally (details of pgfplots implementation). The examples in the bulleted list show typical usage and the behavior of dots. – Paul Gessler Apr 2 '14 at 19:35
@PaulGessler an answer? – cmhughes Apr 2 '14 at 19:42

## 1 Answer

The mention of \foreach in Section 4.15.1 of the pgfplots manual (version 1.10) is just to explain how the tick list is processed internally; it has nothing to do with how the tick list should be written.

Take the examples in the bullet list immediately following as your guide for tick specification. I have included this list below, for reference:

• {0,1,2,5,8,1e1,1.5e1} (a series of coordinates),
• {0,...,5} (the same as {0,1,2,3,4,5}),
• {0,2,...,10} (the same as {0,2,4,6,8,10}), and
• {9,...,3.5} (the same as {9,8,7,6,5,4}).
• See [Section 83 of the PGF/TikZ manual (version 3.0.0)] for a more detailed definition of the options.

In this particular case, ytick={0.2,0.4,0.6} does the job nicely:

## Code

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\begin{document}
\begin{tikzpicture}[domain=0:.7]
\begin{axis}[
xlabel={$P/A_{g}f'{c}$},
ylabel={$EI_{ef}/EI_{g}$},
xmin=-0.1, xmax=0.7,
ymin=0.0, ymax=1.4,
ytick={0.2,0.4,0.6},
]
\addplot[domain=-.1:.1,color=red,very thick]{.3};
\addplot[domain=.1:.5,color=red,very thick]{.2+\x};
\addplot[domain=.5:.7,color=red,very thick]{.7};
\end{axis}
\end{tikzpicture}
\end{document}


A few notes: pgfplots loads tikz on its own, so a separate \usepackage{} is not necessary. Also, I added pgfplotsset{compat=1.10} (replace with your version if necessary) to get better axis label spacing. Finally, I removed domain=0:.7 from the tikzpicture options because it is overridden by each \addplot command anyway.

## Output

-
Thanks for improving the code. But I have a question about the suggestions of the manual. In which cases the inclusion of \foreach will be useful? – Isai Apr 2 '14 at 21:17
@Isai, the only times I use \foreach in an axis environment are for including plots of multiple functions/data files on a single axis. You can refer to my answer here for a specific use case. – Paul Gessler Apr 2 '14 at 21:49
It's complex. Anyway, thanks to you. – Isai Apr 2 '14 at 22:13