# What's wrong with my tikz code?

For some reason the code below is giving me the errors

Package pgfplots Error: Sorry, I could not read the plot coordinates near ') '. Please check for format mistakes.

But I just don't understand why or how to fix it. It seems that I cannot specify math expression in the y coordinates?!

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}[line/.style={black, thick},
declare function={foo(\r,\x) = (\x)^(\r); },
]
\begin{axis}[
width=8cm, height=8cm, %
xmin=0, xmax=1, %
ymode=log, axis on top]

\foreach \r in {1,2,3,4} %
{
coordinates {
(2^(-\r), 1)
(2^(-\r), foo(\r, x))
%(2^(-\r), x^(3)) %% Same error
% (2^(-\r), 0.002) %% This works
};
}
\end{axis}
\end{tikzpicture}
\end{document}


You need to add braces. Otherwise the parser is confused and does not know when the brackets delimit coordinates or arguments of a function.

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}[line/.style={black, thick},
declare function={foo(\r,\x) = (\x)^(\r); },
]
\begin{axis}[
width=8cm, height=8cm, %
xmin=0, xmax=1, %
ymode=log, axis on top]

\foreach \r in {1,2,3,4} %
{
coordinates {
(2^(-\r), 1)
(2^(-\r),{ foo(\r, x)})
({2^(-\r)},{ x^(3)}) %%
({2^(-\r)}, 0.002) %% This works
};
}
\end{axis}
\end{tikzpicture}
\end{document}


• Thanks! I will accept this once I can. I don't understand why the x-coordinates work with braces but not the y-coordinates Jun 10, 2019 at 14:37
• @Tohiko The problem is that the closing ) may either delimit the coordinate or the function. We have to help the parser a little bit. On the other hand, the x coordinate gets delimited by a comma, not the same delimiter as the function.
– user121799
Jun 10, 2019 at 14:39