2

I can create a \tikzset function with 9 parameters by writing a code like the one below:

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

\begin{document}
\begin{tikzpicture}

%--->Function definition
\tikzset
{
    declare function=
    {
        P(\x,\azero,\aone,\atwo,\athree,\afour,\afive,\asix,\aseven)=\azero+\aone*\x+\atwo*\x^2+\athree*\x^3+\afour*\x^4+\afive*x^5+\asix*x^6+\aseven*x^7; 
    }
}

%--->Plot function
\begin{axis}
\addplot 
[
]
{P(x,1,1,1,1,1,1,1,1)}; 
\end{axis}

\end{tikzpicture}
\end{document}

It works as expected, producing the following figure:

enter image description here

But if I add another argument to the function, say \aeight, an error happens after compilation:

! Illegal parameter number in definition of \pgfmathP@.

It seems that there's a limitation in the number of parameters which can't surpass 9. Is there any way to overcome this problem?

[Note: I need the function to have more than 9 arguments (actually 10), because it will be used several times in the code with a total different set of values for those arguments. Because I don't want to define a new function for each time it will be called (it would make the code big and unappealing), a function with 10 arguments would be preferable.]

3
  • That limitation comes from TeX itself. Macros cannot have more than 9 parameters. – Phelype Oleinik Nov 15 '19 at 22:32
  • It seems so. But I thought that maybe there's a trick to overcome the problem, for instance using an array as argument ... But I just don't know how to it. – Élio Pereira Nov 15 '19 at 22:34
  • 1
    @Schrödinger'scat. I see your point. I will add an explanation to the text – Élio Pereira Nov 15 '19 at 23:00
3

The answer to the question

Can one declare functions that take more than 9 arguments?

is Yes since on p. 1032 of the pgf manual v3.1.4 it is written that

enter image description here

However, in your concrete case I would suggest another route: pgf keys. It seems to me that all but one of these arguments are some parameters, which will assume fixed values in the plots. (If the function has more than one "truly varying" variables, the analogous statements apply.) So I would store them in pgf keys. This has, apart from solving your problem, the advantage that you can assign these parameters some initial (or default) values, and need only to change those which which differ from these defaults. This is illustrated in the following example.

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\newcommand\pv[1]{\pgfkeysvalueof{/tikz/mypars/a#1}}
%--->Function definition
\tikzset{
    declare function=
    {
        P(\x)=\pv{0}+\pv{1}*\x+\pv{2}*\x^2+\pv{3}*\x^3+\pv{4}*\x^4+
        \pv{5}*\x^5+\pv{6}*\x^6+\pv{7}*\x^7+\pv{8}*\x^8+\pv{9}*\x^9; 
    },mypars/.cd,a0/.initial=1,a1/.initial=1,a2/.initial=1,
    a3/.initial=1,a4/.initial=1,a5/.initial=1,a6/.initial=1,
    a7/.initial=1,a8/.initial=1,a9/.initial=1,
}

%--->Plot function
\begin{axis}
\addplot[/tikz/mypars/.cd,a8=0,a9=0]{P(x)}; 
\addplot[color=blue,/tikz/mypars/.cd,a8=1,a9=0]{P(x)}; 
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

(Of course, you can make these plots smooth and so on, but this is not what this Q & A is about.)

5
  • Of course, if you have functions that decompose in sums of other functions, you could declare P1 and P2 with up to 9 variables in the usual way, and plot the sum of the two. – user194703 Nov 15 '19 at 23:36
  • 1
    @SC: I got an error with this code. Why is that? \begin{tikzpicture} \draw[/tikz/mypars/.cd,a3=1,a9=0] plot[domain=-1:1] (\x,{P(\x)}); \end{tikzpicture} – Black Mild Apr 13 '20 at 3:31
  • @BlackMild This is because declare function was only written for pgfplots since it uses a plain x. I changed this now. Thanks for the heads up! – user194703 Apr 13 '20 at 3:38
  • oh! it works now with normal plot of TikZ \documentclass[tikz]{standalone} \begin{document} \newcommand\pv[1]{\pgfkeysvalueof{/tikz/mypars/a#1}} \tikzset{ declare function= {P(\x)=\pv{0}+\pv{1}*\x+\pv{2}*\x*\x+\pv{3}*\x*\x*\x;}, mypars/.cd, a0/.initial=0, a1/.initial=0, a2/.initial=0, a3/.initial=0} \begin{tikzpicture} \draw[violet!20] (-1,-4) grid (5,3); \draw[blue,smooth,/tikz/mypars/.cd,a3=-1/3,a2=2,a1=-3] plot[domain=-.5:4.5] (\x,{P(\x)}); \fill[red] circle(2pt); \end{tikzpicture} \end{document} – Black Mild Apr 13 '20 at 3:58
  • 1
    @BlackMild Sure. I just had forgotten a few backslashes, and pgfplots did not complain. – user194703 Apr 13 '20 at 3:59

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.