TeX - LaTeX Asked by Excelsior on March 11, 2021
I have a problem using foreach loop
and intersection
.
My MWE looks like this:
documentclass[border=5pt,tikz]{standalone}
usepackage{pgfplots}
usepackage{tikz}
usepackage{pgfplotstable}
pgfplotsset{compat=newest}
usetikzlibrary{calc}
usetikzlibrary{intersections}
begin{document}
defheight{10cm}
defwidth{10cm}
defsc{0.8}
defxmin{0}
defxmax{5}
defymin{0}
defymax{40000}
defenlarge{0.05}
defGda{12244}
defGdb{4773.48}
defGdc{1861} % Gdb/Gda*Gdb
defGpa{15526.95}
defGpb{6053.38} % Gdb/Gda*Gpb
defaa{1.87}
defab{1.76}
defac{1.66} % ab/aa*ab
defreqa{2.67}
defreqb{2.97}
defreqc{3.30} % reqb/reqa*reqb
begin{tikzpicture}
begin{axis}[
height=height,
width=width,
scale only axis=true,
scale=sc,
enlarge x limits=enlarge,
enlarge y limits=enlarge,
samples=500,
domain=xmin:xmax,
restrict y to domain=ymin:ymax,
xlabel=$mathrm{R}$,
ylabel=$mathrm{E(R)}$,
]
addplot[
name path global=A,
color=black,
smooth,
]
(x,{Gda*(1-exp(-aa*(x-reqa)))^2});
addplot[
color=black,
smooth,
]
(x,{Gpa+Gdb*(1-exp(-ab*(x-reqb)))^2});
addplot[
color=black,
smooth,
]
(x,{Gpa+Gpb+Gdc*(1-exp(-ac*(x-reqc)))^2});
foreach [evaluate=i as n using 216.92*(i + 0.5) - 2.07*(i + 0.5)^2] i in {0,4,...,20} {%
edeftemp{noexpand%
% path[name path global=B] (axis cs:xmin,n) -- (axis cs:xmax,n); % <- here is my problem
% path[name intersections={of=A and B},];
% coordinate (A1) at (intersection-1);
% coordinate (B1) at (intersection-2);
% draw[line width=0.5pt] (A1) -- (B1);
draw (axis cs:xmin,n) -- (axis cs:xmax,n);
}temp
}
end{axis}
end{tikzpicture}
end{document}
I want to compute in the foreach loop the y value and draw a horizontal line in-between the plotted potential. My guess is, that there is a problem with the name of the coordinates and the paths, since I can draw the lines without errors, but unfortunately I could not find a solution.
The current plot looks like this:
Amusingly enough, when I copied your code to save as a TeX document to play around with then I tried to save it as pgfplotsforeach.tex
only to discover that I already have a file with that name as the answer to pgfmathsetmacro isn't setting the macro. I'm guessing that you have also seen that question because you have the edeftemp{...}temp
in your foreach
which looks very like it came from that answer.
The issue is actually the same as in that answer: since pgfplots
saves up commands until the end of the axis
environment, you have to think very carefully about what will be known at that time. Expanding macros is only part of it, you also have to think about coordinate and path names. Fortunately, in a foreach loop then there is an easy way to make things unique - simply append the loop counter to the name (with one caveat: if looping over decimals then one should use the count
key on the loop to get an integer and append that since coordinate names with periods in them are not a good idea).
You also need to noexpand
all the path construction commands inside your edef
, not just the first.
So here's a working version of your code (I think!):
documentclass[border=5pt,tikz]{standalone}
%url{https://tex.stackexchange.com/q/586094/86}
usepackage{pgfplots}
usepackage{tikz}
usepackage{pgfplotstable}
pgfplotsset{compat=newest}
usetikzlibrary{calc}
usetikzlibrary{intersections}
begin{document}
defheight{10cm}
defwidth{10cm}
defsc{0.8}
defxmin{0}
defxmax{5}
defymin{0}
defymax{40000}
defenlarge{0.05}
defGda{12244}
defGdb{4773.48}
defGdc{1861} % Gdb/Gda*Gdb
defGpa{15526.95}
defGpb{6053.38} % Gdb/Gda*Gpb
defaa{1.87}
defab{1.76}
defac{1.66} % ab/aa*ab
defreqa{2.67}
defreqb{2.97}
defreqc{3.30} % reqb/reqa*reqb
begin{tikzpicture}
begin{axis}[
height=height,
width=width,
scale only axis=true,
scale=sc,
enlarge x limits=enlarge,
enlarge y limits=enlarge,
samples=500,
domain=xmin:xmax,
restrict y to domain=ymin:ymax,
xlabel=$mathrm{R}$,
ylabel=$mathrm{E(R)}$,
]
addplot[
name path global=A,
color=black,
smooth,
]
(x,{Gda*(1-exp(-aa*(x-reqa)))^2});
addplot[
color=black,
smooth,
]
(x,{Gpa+Gdb*(1-exp(-ab*(x-reqb)))^2});
addplot[
color=black,
smooth,
]
(x,{Gpa+Gpb+Gdc*(1-exp(-ac*(x-reqc)))^2});
foreach [evaluate=i as n using 216.92*(i + 0.5) - 2.07*(i + 0.5)^2] i in {0,4,...,20} {%
edeftemp{noexpand%
path[name path global=B-i] (axis cs:xmin,n) -- (axis cs:xmax,n); % <- here is my problem
noexpandpath[name intersections={of=A and B-i},];
noexpandcoordinate (A1-i) at (intersection-1);
noexpandcoordinate (B1-i) at (intersection-2);
noexpanddraw (axis cs:xmin,n) -- (axis cs:xmax,n);
noexpanddraw[red,line width=0.5pt] (A1-i) -- (B1-i);
}temp
}
end{axis}
end{tikzpicture}
end{document}
I've zoomed in on the relevant part, and coloured the lines in red to make them obvious.
Correct answer by Andrew Stacey on March 11, 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