8

When plotting a \pgfmathdeclarefunctioned function the graph gets an offset to the right that is proportional to the number of times the function is called in the formula.

I have boiled my initial code down to following example, which exhibits the behaviour:

\documentclass[12pt]{scrbook}
\usepackage{tikz,pgfplots}
\begin{document}
    \pgfmathdeclarefunction{f}{1}{ \pgfmathparse{#1 * #1} }
    \begin{figure}
        \centering
        \begin{tikzpicture}
            \begin{axis}
                \addplot {x + 2 * x * x}; %no shift (clearly centered
                %\addplot {x + 2 * f(x)}; %little shift to the right
                %\addplot {x + f(x) + f(x)}; %shifted nearly off the sheet
            \end{axis}
        \end{tikzpicture}
    \end{figure}
\end{document}

In this example it is obviously easy to write the function inside the \addplot command. But I have a more complex function which is used all over the place and shifts my plots off the page.

I am pretty new to LaTex and probably did an error somewhere, but I can't find it. So what is wrong with this?

  • 2
    Welcome to TeX.sx! That's because you have spurrious spaces in your function definition. Write \pgfmathdeclarefunction{f}{1}{\pgfmathparse{#1*#1}} instead. – jub0bs Mar 26 '13 at 17:18
7

That's because you have spurrious spaces in your function definition. Get rid of them and the problem is solved:

enter image description here

\documentclass[12pt]{scrbook}
\usepackage{tikz,pgfplots}
\begin{document}
    \pgfmathdeclarefunction{f}{1}{%
        \pgfmathparse{#1*#1}%
    }
    % or \pgfmathdeclarefunction{f}{1}{\pgfmathparse{#1*#1}}
    \begin{figure}
        \centering
        \begin{tikzpicture}
            \begin{axis}
                \addplot {x + 2 * x * x}; %no shift (clearly centered
                \addplot {x + 3 * f(x)}; %little shift to the right
                \addplot {x + f(x) + f(x) + f(x) + f(x)}; %shifted nearly off the sheet
            \end{axis}
        \end{tikzpicture}
    \end{figure}
\end{document}

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.