TeX - LaTeX Asked by Mark A. Bromuela on July 7, 2021
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}{
filldraw (x,y) circle [radius=2pt];}
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}
filldraw (x,y) circle [radius=10pt];
end{tikzpicture}
end{document}
which gives:
What I want is:
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}{%
ifnumxcnt=ycntfilldraw (x,y) circle [radius=2pt];fi
}}
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}{%
ifnumxcnt=ycntfilldraw (x,y) circle [radius=2pt];fi
}}
end{tikzpicture}
end{document}
Correct answer by Steven B. Segletes on July 7, 2021
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}{
filldraw (x,x) circle [radius=2pt];}
end{tikzpicture}
end{document}
Answered by SebGlav on July 7, 2021
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}
filldraw (x,y) circle [radius=10pt];
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}
filldraw (NewX,y) circle [radius=10pt];
end{tikzpicture}
end{document}
Answered by Torbjørn T. on July 7, 2021
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}
filldraw (x,y) circle [radius=10pt];
end{tikzpicture}
end{document}
Answered by SebGlav on July 7, 2021
For the second problem, this works:
begin{tikzpicture}
foreach coords in {(1,2),(2,5),(3,4),(4,6),(5,9),(6,1),(7,3),(8,8),(9,7)}
filldraw coords circle [radius=10pt];
end{tikzpicture}
See the section "Special handling of pairs" on page 1002 of the PGF manual, under section 88 "Repeating Things: The Foreach Statement".
Answered by m90 on July 7, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP