3

I would like to use an array in a PGF axis environment. This array would have the parameters values of a function to be plotted. I tried writing the following code:

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

\def\a{{1,2}}  %Array definition

\begin{tikzpicture}
\begin{axis}[]

\addplot [
    domain=0:10,
    samples=100, 
    color=blue,
]
{(\a[0])+(\a[1])*x}; %Function to plot

\end{axis}
\end{tikzpicture}
\end{document} 

I got an error in Texmaker, that says the following:

! Package PGF Math Error: Sorry, an internal routine of the floating point unit
got an ill-formatted floating point number `0.0'. The unreadable part was near
'0.0'. (in '({1,2}[0])+({1,2}[1])*x').

It seems that the function environment doesn't allow the use of arrays. There are some tikz commands that accept it, as is the case of \filldraw (you may see this). Is there any way to do this still using arrays and the axis environment?

  • 1
    Sorry! I had a range which I tried to change here for the sake of simplicity. I ended forgetting the comma in the process. – Élio Pereira Sep 3 '19 at 14:32
6

The problem is that pgfplots switches on fpu and uses it, hence the error message. (You'd need {...} but this does not solve the problem here.) One way to make it work is to store the array entries in macros, which you use.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\def\myarray{{1,2}}  %Array definition
\begin{tikzpicture}
\begin{axis}[]
\pgfmathsetmacro{\mya}{\myarray[0]}
\pgfmathsetmacro{\myb}{\myarray[1]}
\addplot [domain=0:10,color=blue] {\mya+\myb*x}; %Function to plot
\end{axis}
\end{tikzpicture}
\end{document} 

enter image description here

| improve this answer | |

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.