0

I'm using the TikZ declare function key and getting some weird results with squaring.

\begin{tikzpicture}[declare function={f(\x)=\x*\x;}]
  \node {\pgfmathparse{f(-.5)} \pgfmathresult}; % outputs *positive* 0.25, as expected
\end{tikzpicture}

\begin{tikzpicture}[declare function={f(\x)=\x^2;}] \node {\pgfmathparse{f(-.5)} \pgfmathresult}; % outputs negative -0.25 \end{tikzpicture}

This appears only to affect \pgfmathparse and not \addplot. For example, in the MWE

\documentclass{standalone}
\usepackage{tikz,pgfplots}

\begin{document} \begin{tikzpicture}[declare function={f(\x)=\x^2;}] \begin{axis}[domain=-1:1] \addplot {f(x)}; \pgfmathsetmacro{\y}{f(-0.5)} \addplot coordinates {(-0.5,\y)}; \end{axis} \end{tikzpicture} \end{document}

broken graph

we can see that the point (-0.5,f(-0.5)) is not on the graph of y=f(x) which is definitely not the expected behaviour.

  • 1
    It is the expected output - see e.g. https://tex.stackexchange.com/a/125896/8650. There are several copies of this question. – hpekristiansen Jan 21 '24 at 19:43

0 Answers0