Graphics, Figures & TablesLooking for help with pgfmathsetmacro in pgfplotsinvokeforeach loop in axis environment

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
simiusatheus
Posts: 5
Joined: Mon Feb 19, 2024 7:11 pm

Looking for help with pgfmathsetmacro in pgfplotsinvokeforeach loop in axis environment

Post by simiusatheus »

Greetings,

I'm having trouble with a variable set with \pgfmathsetmacro in a loop within an axis environment. Only the last value of the variable is being printed during each loop iteration. In the code below, the values in the rectangles are supposed to be displaying the distance between the upper and lower graphs, but all the values are showing the distance between the graphs in the last rectangle. I've searched extensively online for a solution but can't determine how to implement any of them successfully. Hence, I'm hoping some kind soul can show me the path forward. Below is my code:

Code: Select all

Code, edit and compile here:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\pgfplotsset{
tick label style={font=\scriptsize},
minor x grid style={gray, opacity=0.25}
}
\tikzstyle{every node}=[font=\scriptsize]
\begin{document}
\begin{tikzpicture}[
declare function={
f(\x)=0.73*pow(\x,3)-2*pow(\x,2)+\x+0.6;
g(\x)=0.17*pow(\x,2)-0.5*\x+1.1;},
]
\begin{axis}[
grid=both,
xmin=0, xmax=2, xtick distance=0.2,
minor x tick num=1,
ymin=0, ymax=1.2, ytick distance=0.1,
xlabel={$t$ (hours)}, ylabel={$r$ (inches/hour)}]
\addplot[thick, smooth, domain=0:2]
{0.73*x^3-2*x^2+x+0.6}
node[below left, pos=0.45] {$r = f(t)$};
\addplot[thick, smooth, domain=0:2]
{0.17*x^2-0.5*x+1.1}
node[above, pos=0.7] {$r = g(t)$};
\pgfplotsinvokeforeach{0.2,0.6,...,1.8}
{\pgfmathparse{g(#1+0.1)-f(#1+0.1)};
\pgfmathsetmacro{\ans}{\pgfmathresult};
\draw[thick]
(#1,{f(#1+0.1)}) rectangle
node[anchor=center,rotate=90,
font=\small\sffamily\bfseries]
{\pgfmathprintnumber[precision=4]{\ans}} (#1+0.2,{g(#1+0.1)});};
\end{axis}
\end{tikzpicture}
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Bartman
Posts: 369
Joined: Fri Jan 03, 2020 2:39 pm

Looking for help with pgfmathsetmacro in pgfplotsinvokeforeach loop in axis environment

Post by Bartman »

A solution approach is available in the same section of the pgfplots manual where the loop command already used is explained.

Code: Select all

Code, edit and compile here:
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}% loads tikz
\pgfplotsset{
compat=1.18,
tick label style={font=\scriptsize},
minor x grid style={gray, opacity=0.25}
}
\tikzset{every node={font=\scriptsize}}
\begin{document}
\begin{tikzpicture}[
declare function={
f(\x)=0.73*pow(\x,3)-2*pow(\x,2)+\x+0.6;
g(\x)=0.17*pow(\x,2)-0.5*\x+1.1;
}
]
\begin{axis}[
grid=both,
xmin=0, xmax=2, xtick distance=0.2,
minor x tick num=1,
ymin=0, ymax=1.2, ytick distance=0.1,
xlabel={$t$ (hours)}, ylabel={$r$ (inches/hour)}]
\addplot[thick, smooth, domain=0:2]
{0.73*x^3-2*x^2+x+0.6}
node[below left, pos=0.45] {$r = f(t)$};
\addplot[thick, smooth, domain=0:2]
{0.17*x^2-0.5*x+1.1}
node[above, pos=0.7] {$r = g(t)$};
\pgfplotsinvokeforeach{0.2,0.6,...,1.8}{
\pgfmathsetmacro{\ans}{g(#1+0.1)-f(#1+0.1)}
\edef\temp{
\noexpand\draw[thick]
(#1,{f(#1+0.1)}) rectangle
node[
anchor=center,
rotate=90,
font=\noexpand\small
]{\pgfmathprintnumber[precision=4]{\ans}}
(#1+0.2,{g(#1+0.1)});
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
I left out the settings for a bold and sans serif font for the numbers because these font changes don't work in your example and you didn't complain about it.
simiusatheus
Posts: 5
Joined: Mon Feb 19, 2024 7:11 pm

Looking for help with pgfmathsetmacro in pgfplotsinvokeforeach loop in axis environment

Post by simiusatheus »

I did see that in the manual, and tried implementing the \edef function but it was unclear to me exactly which part of the code should be \edef'ed as a temporary function and where it should be placed in the code. Also, the use of \noexpand in different places is mysterious to me. I have basic familiarity with LaTeX but am new to Tikz and pgfplots, hence my confusion on some of these constructs. The manuals, though helpful, aren't completely transparent on some topics.

I've worked my way through most of the "LaTeX, Graphics with TikZ", but wish there was a sequel to it covering these more advanced topics in a clear way.

Thanks again for your help.
Post Reply