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.

I need a loop that ends with a test, why this one doesn't work and how use an index in this kind of loop ?

\documentclass[tikz]{standalone}
\usepackage{}
\usetikzlibrary{}

\begin{document}

\begin{tikzpicture}
\count255 = 0
\loop
\draw (0,0) -- (\count225,1) ;  
\advance\count255 by 1
\ifnum\count255 < 4
\repeat
\end{tikzpicture}

\end{document}
share|improve this question
2  
Regardless of the specific issue, is there a reason for using the low-level \loop instead of PGF's \foreach? Since you're using TikZ already then it would seem more natural to use \foreach. –  Loop Space May 9 at 8:49
1  
My problem is that I don't know how much times the loop must operate, I need a test to stop it. I don't post the test because it is MWE, Of course foreach would be an evidence in this case that draws 3 times a vertical line. If there is another solution I'll take it, maybe a break foreach but it's not clean ... ? –  Tarass May 9 at 8:55
    
Sounds reasonable! I don't think \foreach can handle emulate \while loops so that makes sense. –  Loop Space May 9 at 8:57
    
Thanks, I put back my question as it was initialy. –  Tarass May 9 at 9:25

2 Answers 2

up vote 6 down vote accepted

You need a \the before the \count255 in the coordinate.

\documentclass{article}
%\url{http://tex.stackexchange.com/q/176229/86}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
\count255 = 1
\loop
\draw (0,0) -- (0,\the\count255) ;
\advance\count255 by 1
\ifnum\count255 < 3
\repeat
\end{tikzpicture}

\end{document}
share|improve this answer
    
Still have a problem with the stepping of the counter. –  Tarass May 9 at 9:09

A possible solution wit LuaLaTeX:

\documentclass{article}
\usepackage{luacode}
\usepackage{tikz}


\begin{document}
\luaexec{
tp = tex.print
count = 1
while count < 3 do
tp("\\begin{tikzpicture}")
tp("\\draw (0,0) -- (0,"..count..");")
tp("\\end{tikzpicture}");
count = count + 1
end
}

\end{document}
share|improve this answer

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.