Graphics, Figures & TablesLooking for help with Tikz/pgfplot issue about "declare function" and 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 Tikz/pgfplot issue about "declare function" and loop in axis environment

Post by simiusatheus »

Greetings,

I'm new to Tikz/pgfplots and am trying to understand something about the code snippet below. It works BUT Overleaf shows the brace after f(#1+0.1); in the line just above the commented out code to be unmatched; it doesn't seem like it should be unmatched to me.

What I'm actually trying to achieve is to have filled rectangles between the two curves as indicated in the commented out code block, but that doesn't seem to work at all, though it seems reasonable to me that it should.

My questions are:

1. Is there some way to make the method I'm attempting to achieve this result work?
2. If not, what would be the best way to achieve the result?

Thanks alot
Gary

Code: Select all

\documentclass[border=10pt]{standalone}

\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.5] {$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}
            {\draw[thick,fill=gray,opacity=0.5] (#1,0) rectangle (#1+0.2,f(#1+0.1);}

        % \pgfplotsinvokeforeach{0.2,0.4,...,1.8}
        %     {\draw[thick,fill=gray,opacity=0.5] 
        %     (#1,f(#1+0.1)) rectangle (#1+0.2,g(#1+0.1));
        %     }
    \end{axis}
\end{tikzpicture}
\end{document}

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 Tikz/pgfplot issue about "declare function" and loop in axis environment

Post by Bartman »

In general, you need to add braces around components of coordinates when these components contain parentheses.
This insight can be found in section 2.16 "Intersecting Paths" of the TikZ manual.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10321
Joined: Mon Mar 10, 2008 9:44 pm

Looking for help with Tikz/pgfplot issue about "declare function" and loop in axis environment

Post by Stefan Kottwitz »

Hi Gary,

welcome to the forum!

Apart from the braces, the parentheses already don't match. This should work:

Code: Select all

\pgfplotsinvokeforeach{0.2,0.6,...,1.8}
  {\draw[thick,fill=gray,opacity=0.5] (#1,0) rectangle (#1+0.2,{f(#1+0.1)});}
Btw. a direct link to what Bartman said, is here: 2.16 Intersecting Paths.

Stefan
LaTeX.org admin
simiusatheus
Posts: 5
Joined: Mon Feb 19, 2024 7:11 pm

Looking for help with Tikz/pgfplot issue about "declare function" and loop in axis environment

Post by simiusatheus »

Thanks for the help. Your corrections work great, though Overleaf still indicates that that last brace is unmatched; maybe a problem with Overleaf?
Post Reply