I'm trying the answer of @Jake from Automate derivative representation in TikZ, which is
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{matrix}
\begin{document}
\pgfplotsset{point legend/.style={},
point 1/.style={anchor=south},
point 2/.style={anchor=south}
}
\newcommand{\derivative}[5]{
\begin{scope}[declare function={f(\x)=#1;}]
\addplot [thick, red, latex-latex] {f(x)} node [anchor=west] {#2};
\addplot [black, mark=*] coordinates {(#3,{f(#3)}) (#4,{f(#4)})}
node [pos=0,/pgfplots/point 1] {$P_1$}
node [pos=1,/pgfplots/point 2] {$P_2$};
\pgfplotsextra{
\pgfmathsetmacro\first{f(#3)}
\pgfmathsetmacro\second{(f(#4)}
\pgfmathsetmacro\xdiff{#4-#3}
\pgfmathsetmacro\ydiff{f(#4)-f(#3)}
\draw (axis cs:#3,\first) -| (axis cs:#4,\second);
\draw [|-|,yshift=-2ex] (axis cs:#3,\first) -- node [inner sep=1pt,fill=white] {\pgfmathprintnumber{\xdiff}} (axis cs:#4,\first);
\draw [|-|,xshift=2ex] (axis cs:#4,\first) -- node [inner sep=1pt, fill=white] {\pgfmathprintnumber{\ydiff}} (axis cs:#4,\second);
\matrix at (rel axis cs:1,1) [matrix of nodes,/pgfplots/point legend] {#5\\};
}
\end{scope}
}
\pgfplotsset{
azetinaplot/.style={
width=7cm,
height=7cm,
axis lines=middle,
xlabel=$x$,
ylabel=$y$,
enlarge y limits,
clip=false
}
}
\begin{tikzpicture}
\begin{axis}[
azetinaplot,
domain=-20:370, samples=100,
xmin=-20, xmax=370,
point 1/.append style={anchor=south east},
point 2/.append style={anchor=east}]
\derivative{sin(\x)}
{$f(x)=\sin(x)$}
{290}{340}
{$P_1=(290\,,\,-0.94)$\\$P_2=(340\,,\,-0.34)$}
\end{axis}
\end{tikzpicture}
\end{document}
But I have error Math Error: The function 'f' already exists. Does anyone know why the error occurs, I tried both in TexStudio and Overleaf

declare functionfrom thescopeand put it as an argument to thetikzpicture(i.e.:\begin{tikzpicture}[declare function={f(\x)=sin(\x);}]) – Marijn Jul 03 '18 at 11:39pgfplotsusesscopeanddeclare functionin a different way compared to the 2012 version from the other question. I'm not sure what the problem is exactly, I would expect it to work, but in this way to issue is avoided. – Marijn Jul 03 '18 at 12:04\newcommand{\derivative}[5]{ \tikzset{declare function={f(\x)=#1;}} \begin{scope} ..... That way you can use the macro the same way as in Jake's answer. I also do not think it has to do with the pgfplots version since the error arises also for older versions of pgfplots, so maybe a TikZ update is responsible instead. – Jul 03 '18 at 14:12\derivativemacro only once pertikzpicture. Use it twice, and you'll get the same error, albeit for a more obvious reason (you actually do declareftwice). – Torbjørn T. Jul 03 '18 at 21:46axisenvironment, but if you have twoaxisin the sametikzpicture, then you can have a\derivativein each. One workaround for that, if you actually need it, is to make the function name an (optional) argument to the\derivativemacro. I can post an answer with that, if you want to. – Torbjørn T. Jul 04 '18 at 07:33axisdoesn't work that well. – Torbjørn T. Jul 04 '18 at 07:36\derivativemacro needs some modifications. – Torbjørn T. Jul 04 '18 at 11:32