数学错误:函数“f”已存在

数学错误:函数“f”已存在

我正在尝试@Jake 的答案在 TikZ 中自动进行衍生表示,即

\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}

但是我遇到了错误Math Error: The function 'f' already exists。有人知道为什么会出现这个错误吗?我在 TexStudio 和 Overleaf 中都试过了

答案1

这是 的另一种定义\derivative。它在某些方面与 Jake 的原定义不同,用法如下:

\derivative[<optional function name, default f>]
           {<mandatory function definition>}
           {<mandatory function label>}
           {<mandatory x_1>}{<mandatory x_2>}
           [<optional position of legend, default rel axis cs:1,1>]

第一个可选参数可用于避免“函数已存在”错误。如果未指定,例如\derivative{sin(\x)}...,则f使用。第二次\derivative在中调用时axis,请指定其他名称,例如\derivative[g]{cos(\x)}...

最后一个可选参数用于定义图例的位置。如果不指定,图例将放置在 的右上角axis

我删除了原始宏的最后一个参数(图例条目被写入其中),而是将它们作为宏定义的一部分。

在此处输入图片描述

\documentclass{article}
\usepackage{xparse} % for \NewDocumentCommand
\usepackage{pgfplots}
\usetikzlibrary{matrix}

\pgfplotsset{point legend/.style={},
    point 1/.style={anchor=south},
    point 2/.style={anchor=south}
}

\NewDocumentCommand{\derivative}{O{f} m m m m O{rel axis cs:1,1}}{
\tikzset{declare function={#1(\x)=#2;}}
    \addplot [thick, red, latex-latex] {#1(x)} node [anchor=west] {#3};
    \addplot [black, mark=*] coordinates {(#4,{#1(#4)}) (#5,{#1(#5)})}
        node [pos=0,/pgfplots/point 1] {$P_1$}
        node [pos=1,/pgfplots/point 2] {$P_2$};
    \pgfplotsextra{
        \pgfmathsetmacro\first{#1(#4)}
        \pgfmathsetmacro\second{#1(#5)} % removed first (
        \pgfmathsetmacro\xdiff{#5-#4}
        \pgfmathsetmacro\ydiff{#1(#5)-#1(#4)}
        \draw (axis cs:#4,\first) -| (axis cs:#5,\second);
        \draw [|-|,yshift=-2ex] (axis cs:#4,\first) -- node [inner sep=1pt,fill=white] {\pgfmathprintnumber{\xdiff}} (axis cs:#5,\first);
        \draw [|-|,xshift=2ex] (axis cs:#5,\first) -- node [inner sep=1pt, fill=white] {\pgfmathprintnumber{\ydiff}} (axis cs:#5,\second);
        \matrix at (#6) [matrix of math nodes,/pgfplots/point legend] {
         P_1=(#4\,,\,\pgfmathparse{#1(#4)}\pgfmathprintnumber{\pgfmathresult})\\
         P_2=(#5\,,\,\pgfmathparse{#1(#5)}\pgfmathprintnumber{\pgfmathresult})\\};
    }
}

\begin{document}



\pgfplotsset{
    azetinaplot/.style={
        width=10cm,
        height=8cm,
        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}
        [axis cs:150,-1] % position for the legend


    % above we used the default function name f
    % here we use the first optional argument to give the function the name g instead
    \derivative[g]{cos(\x)+2}
        {$f(x)=\cos(x)$}
        {200}{300}
        [axis cs:170,2.5] % position for the legend

    \end{axis}

\end{tikzpicture}
\end{document}

相关内容