# Two problems with foreach - TikZ

Note: This is a major edit to my original question. I intended to ask another question for the second problem separately, but it has already been initiated in the comment and probably already answered here. So I have edited this question instead to cover all two of them.

FIRST Problem: Illegal unit of measure: (pt inserted)

I am facing a similar problem as "Illegal unit of measure (pt inserted)" using arithmetic in TikZ foreach. Except that I am using negative decimal numbers as well as positive decimal numbers and I have used three dots here for the interval. The MWE is as below:

\documentclass[border=3mm,tikz]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x/\y in {-1.4/-1.4,-1.2/-1.2,...,1.6/1.6}{
\end{tikzpicture}
\end{document}


How do I fix this? I know there is an alternative way to get the same picture. But I am experimenting with this for some purpose. I have passed here equal values of x and y. The problem is not that if there is an alternative way to make the same picture. The problem is that if there is a way to workaround with this error and get rid of it. This is because I may change the values of y and may still find the same error lurking in.

SECOND Problem: Using two foreach to make an ordered pair

The second problem is how I can use two foreach commands to make an ordered pair so that one doesn't iterate over the iteration of the other, but forms a one-to-one correspondence or something like that. Some suggests to use evaluate in foreach. This is okay when I know y depends on x. But how about when y doesn't depend on x at all? When y has some random values for example? The MWE is

\documentclass[border=3mm,tikz]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x  in {1,...,9}
\foreach \y  in {2,5,4,6,9,1,3,8,7}
\end{tikzpicture}
\end{document}


which gives:

What I want is:

• You cannot use the triple dots syntax for multiple variables I believe (e.g. tex.stackexchange.com/questions/37601). Jul 2, 2021 at 20:55
• Oh! that depressed me. Jul 2, 2021 at 21:05
• As mentioned in the answer to that question though, and as SebGlav mentions in his comment, you may be able to use evaluate. Jul 2, 2021 at 21:08

Maybe something like this as a workaround...

\documentclass[border=3mm,tikz]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x [count=\xcnt] in {-1.4,-1.2,...,1.6}{%
\foreach \y [count=\ycnt] in {-1.4,-1.2,...,1.6}{%
}}
\end{tikzpicture}
\begin{tikzpicture}
\foreach \x [count=\xcnt] in {-1.4,-1.2,...,1.6}{%
\foreach \y [count=\ycnt] in {-2.8,-2.4,...,3.2}{%
}}
\end{tikzpicture}
\end{document}


• I have updated the question. But you already answered what I needed haha. So thanks. It works. Could you explain count and ifs\xcnt and '\ycnt' named arbitrarily? Jul 3, 2021 at 3:10
• Since I don't understand how this answer properly your question (points are aligned here, and you want \y to be randomly chosen, I'll give another try. See my new answer. Jul 3, 2021 at 7:48
• Sorry, I misread this answer in the first place, its fine and works properly (the aligned points gave me the impression of the contrary). Jul 3, 2021 at 7:55
• @M.A.Bromuela When one says \foreach \x [count=\xcnt] in {-1.4,-1.2,...,1.6}, the first time through the loop, \x equals -1.4; however, \xcnt=1, meaning the first item in the list (etc for subsequent loop circuits). I use a comparison of the x and y counts (not substitution values), so that I only execute the \filldraw when \xcnt = \ycnt. Jul 3, 2021 at 20:03

Why don't you just do this?

\documentclass[border=3mm,tikz]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {-1.4,-1.2,...,1.6}{
\end{tikzpicture}
\end{document}

• That's because I will change the y-values to some other values. to form some ordered-pairs. I am actually trying to make some ordered-pairs using two foreach if possible. Jul 2, 2021 at 20:59
• The only problem is if I use two foreach one after the other, the first one iterates over the iteration of the second one. Jul 2, 2021 at 21:00
• So you will have to type them all, unless \y can be computed from \x. In this case, you can use the following syntax: \foreach[evaluate=\y using <your formula>] \x in {<your series>}. Jul 2, 2021 at 21:01
• Please complete your question with an actual list that you want to loop in, so that we can help you out. Jul 2, 2021 at 21:02
• I have updated the question. But before I updated it looks like @Steven B. Segletes already answered what I needed. Thanks for your effort. Jul 3, 2021 at 3:07

Just mentioning this as well. If you have a list of values that you want to place at regular intervals, one loop with a counter is enough:

\documentclass[border=3mm,tikz]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach [count=\x] \y  in {2,5,4,6,9,1,3,8,7}
\end{tikzpicture}
\end{document}


If the values should start at some specific value, you can say e.g. count=\x from 10, and if the \x should have a different spacing, you can probably use evaluate, e.g. \foreach [count=\x, evaluate={\NewX=2*\x + 4;}] which will make \NewX 6, 8, 10, ...

\documentclass[border=3mm,tikz]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach [count=\x, evaluate={\NewX=2*\x+4;}] \y  in {2,5,4,6,9,1,3,8,7}
\end{tikzpicture}
\end{document}


Now that you edited your question, it seems that you want to draw points at every \x integer coordinates but with randomly chosen \y coordinates.

In my opinion, this is what [count=\x from 1] is made for:

\documentclass[border=3.14mm,tikz]{standalone}

\begin{document}
\begin{tikzpicture}
\foreach \y [count=\x from 1] in {2,5,4,6,9,1,3,8,7}

\begin{tikzpicture}
`