9

I have the following code to build the derivates of a given function:

\documentclass[tikz, border=.5cm]{standalone}
\usepackage{tkz-fct}
\usepackage{xfp}
\usetikzlibrary{spy}
\definecolor{vinho}{rgb}{0.0, 0.18, 0.39}
\definecolor{vermelho}{rgb}{0.93, 0.11, 0.14}

\begin{document}

\def\a{2}
\foreach \b in {2.5,2.45,...,2.3,2.2,2.1,2.09,...,2.01}{
\begin{tikzpicture}[scale=2,cap=round,declare function = {
f(\x) = .5*(\x-1.5)*(\x-1.5)+1;},
spy using outlines={circle,magnification=2, height=0.5cm,size=1.5cm, connect spies}]
\clip (-1.8,-0.8) rectangle (5,3.5);
\draw[-latex] (-0.5,0) -- (4,0) node[right]{$x$};
\draw[-latex] (0,-0.5) -- (0,3) node[above]{$y$};
\draw[vinho,thick,domain=1:3.5,samples=200] plot(\x,{f(\x)}) node[right]{$f$};
\draw[vermelho,thick,domain=-1:1.91,samples=200] plot({\a + \x*(\b-\a)/(sqrt( (\b-\a)^2 + (f(\b)-f(\a))^2 ))},{f(\a) + \x*(f(\b)-f(\a))/(sqrt( (\b-\a)^2 + (f(\b)-f(\a))^2 ))}) node[right]{$s$};
\draw[densely dotted] (0,{f(\b)})  -- (\b,{f(\b)}) -- (\b,0) node[below]{$x_0+h$};
\draw[densely dotted] (0,{f(\a)}) -- (\a,{f(\a)}) -- (\a,0) node[below=0.08cm]{$x_0$};
\draw[fill] (\a,{f(\a)}) node[above=0.3cm]{$P$} circle (1pt);
\draw[fill] (\b,{f(\b)}) node[above]{$Q$} circle (1pt);
\draw[decoration={brace, raise=10pt},decorate,vinho!50] (0,{f(\a)}) -- node[left=10pt] {$f(x_0+h)-f(x_0)$} (0,{f(\b)});
\draw[decoration={brace,mirror,raise=5pt},decorate,vinho!50]  ({\a},-.250) -- node[below=6pt] {$h$} ({\b},-.250); 
\spy on ({2*\a},{2*f(\a)}) in node at (0.7,0.7);
\end{tikzpicture}
}

\end{document}

For each \b in the looping, I build a different frame.

However, I would like to draw the spy:

\spy on ({2*\a},{2*f(\a)}) in node at (0.7,0.7);

only for b < 2.1. Tikz does not accept a condition when deals with non integer numbers.

How can I do this?

3
  • Can the last solution of this answer maybe help you? Nov 2 '21 at 20:20
  • Cannot solve. I would like of another solution. Nov 2 '21 at 20:27
  • \fpeval{trunc(\b/2.1)} < 1 maybe this condition is ok
    – polyn
    Nov 2 '21 at 20:47
9

You could apply the last solution of this answer here:

The expression int(ifthenelse(\b<2.1, 1, 0)) will output 1 or 0 (both integers) depending on whether \b is smaller than 2.1 or not.

This solution circumvents the problem that you cannot implement conditionals with non-integers in TikZ directly. It makes use of the ifthenelse function that comes with PGF to convert the non-integer to 0 or 1, which can then easily be tested with \ifnum.

(To decrease compilation time I reduced the sample size.)

\documentclass[tikz, border=.5cm]{standalone}

\usepackage{tkz-fct}
\usepackage{xfp}
\usetikzlibrary{spy}

\definecolor{vinho}{rgb}{0.0, 0.18, 0.39}
\definecolor{vermelho}{rgb}{0.93, 0.11, 0.14}

\begin{document}

\def\a{2}
\foreach \b [evaluate=\b as \z using {int(ifthenelse(\b<2.1, 1, 0))}] in {2.2, 2.1, 2.09, 2.08} {
    \begin{tikzpicture}[scale=2, cap=round, declare function={
            f(\x) = .5*(\x - 1.5)*(\x - 1.5) + 1;
        }, spy using outlines={circle, magnification=2, height=0.5cm, size=1.5cm, connect spies}]

        \clip (-1.8,-0.8) rectangle (5,3.5);

        \draw[-latex] (-0.5,0) -- (4,0) node[right] {$x$};
        \draw[-latex] (0,-0.5) -- (0,3) node[above] {$y$};

        \draw[vinho, thick, domain=1:3.5, samples=200] plot(\x, {f(\x)}) node[right] {$f$};
        \draw[vermelho, thick, domain=-1:1.91, samples=200] plot({\a + \x*(\b - \a)/(sqrt((\b - \a)^2 + (f(\b) - f(\a))^2))}, {f(\a) + \x*(f(\b) - f(\a))/(sqrt((\b - \a)^2 + (f(\b) - f(\a))^2))}) node[right] {$s$};
        
        \draw[densely dotted] (0,{f(\b)}) -- (\b,{f(\b)}) -- (\b,0) node[below] {$x_0 + h$};
        \draw[densely dotted] (0,{f(\a)}) -- (\a,{f(\a)}) -- (\a,0) node[below=0.08cm] {$x_0$};
        
        \draw[fill] (\a,{f(\a)}) node[above=0.3cm] {$P$} circle (1pt);
        \draw[fill] (\b,{f(\b)}) node[above] {$Q$} circle (1pt);
        
        \draw[decoration={brace, raise=10pt}, decorate, vinho!50] (0,{f(\a)}) -- node[left=10pt] {$f(x_0 + h) - f(x_0)$} (0,{f(\b)});
        \draw[decoration={brace, mirror, raise=5pt}, decorate, vinho!50] ({\a},-0.250) -- node[below=6pt] {$h$} ({\b},-0.250); 
    
        \ifnum \z=1 
            \spy on ({2*\a},{2*f(\a)}) in node at (0.7,0.7);
        \fi
    \end{tikzpicture}
}

\end{document}

Output for \b = 2.3, 2.2, 2.1, 2.09, 2.08, 2.07, 2.06, 2.05: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.