Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

What I've been trying to do, futilely, is to use the foreach[counter=\xi] x in {1,2,3} statement in tikz/pgf with LaTeX. This fails, every time. Whatever. So I moved on to trying to use a counter, surely that would work. Here's a MWE that fails unexpectedly.

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
\newcommand{\test}[1]{
\newcounter{i}
\setcounter{i}{0}
\foreach \x in #1
{
\draw (0,\i) -- (1,0);
\setcounter{i}{\i+1}
}
}

\begin{tikzpicture}
\test{{1,2,3}}
\end{tikzpicture}
\end{document}

This code, at least for me, produces

alt text

Can anyone explain why? How I would fix this, or how I could actually get foreach[counter=\xi] to work without returning an \inaccessible exception upon compilation in pdfLaTeX

share|improve this question
    
Using \xi as counter variable is not a good idea, at least if you also want to use the Greek letter ξ in your document (which becomes inaccessible after running the loop). –  Caramdir Nov 26 '10 at 23:44
add comment

2 Answers 2

up vote 5 down vote accepted

Check the use of the counter macro. You may access its value by \value{i} instead of \i, you can increase it by \stepcounter{i} or \addtocounter{i}{1}. On the other hand you could use \x directly.

So try for example:

\setcounter{i}{0}
\foreach \x in #1
{
\draw (0,\value{i}) -- (1,0);
\stepcounter{i}
}

alt text

And without the additional counter it's just:

\foreach \x in #1
{
\draw (0,\x) -- (1,0);
}
share|improve this answer
add comment

Which version of TikZ are you using? The count (not counter) option was introduced in version 2.10 (sorry for not adding this info to my answer to your other question). If you are using TeX Live 2010, then a simple tlmgr update --all will (amongst others) update TikZ to the current version.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.