pgfplots, with its companion package pgfplotstable, already has the ability to read data from file. (And I guess/think datatool has some compatibility issue with pgfplots.)
In the following example,
- Data is written in
mydata.tex by row, and separated by comma.
- Data is stored in command
\mydata.
- Since
pgfplots reads data by column, \pgfplotstabletranspose is used to transpose the data matrix.
- By default,
\pgfplotstabletranspose creates new column names. Options colnames from={xticklabels}, input colnames to={xticklabels} make it use xticklabels as column name.
- Option
xticklabels from table={\mydata}{xticklabels} uses data from table \mydata, column xticklabels as xticklabels list.
Related questions:
\begin{filecontents}[force, noheader]{mydata.tex}
xticklabels,-5,-4,-3,-2,-1,0,1,2,3,4,5
\end{filecontents}
\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
\pgfplotstableset{col sep=comma}
\begin{document}
\begin{tikzpicture}
% transpose data table
\pgfplotstabletranspose[
colnames from={xticklabels},
input colnames to={xticklabels}
]{\mydata}{mydata.tex}
\begin{axis}[
xtick = {-5,-4,-3,-2,-1,0,1,2,3,4,5},
xticklabels from table={\mydata}{xticklabels},
]
\addplot {x^2};
\end{axis}
\end{tikzpicture}
\end{document}

In case one wants to have syntax xticklabels=<macro storing a list>, see the following patch
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\makeatletter
% redefine pgfplots key "xticklabels", expand its value once before parsing
\pgfkeys{
/pgfplots/xticklabels/.code={%
\expandafter\pgfplotslistnew\expandafter\pgfplots@xticklabels\expandafter{#1}%
\let\pgfplots@xticklabel=\pgfplots@user@ticklabel@list@x
}
}
\makeatother
\newcommand\xticklabels{-5,-4,-3,-2,-1,0,1,2,3,4,5}
\begin{tikzpicture}
\begin{axis}[
xtick = {-5,-4,-3,-2,-1,0,1,2,3,4,5},
xticklabels=\xticklabels,
]
\addplot {x^2};
\end{axis}
\end{tikzpicture}
\end{document}
pgfplotstable. I was aware of this package but actually interested in a solution without using a different package. Appearantly evenxticklabels = \xticklabelswith\newcommand\xticklabels{-5,-4,-3,-2,-1,0,1,2,3,4,5}does not work. Why not? – Carucel Aug 04 '20 at 11:25xticklabelsis defined such that its value is not expanded before being parsed as a list. – muzimuzhi Z Aug 04 '20 at 12:09xticklabels=<macro storing a list>, I've added a new example to my answer. – muzimuzhi Z Aug 04 '20 at 12:17\newcommand\xticklabelsB{-5,-4,-3,-2,-1,0,1,2,3,4,5}\newcommand\xticklabels{\xticklabelsB}does not work using your solution. – Carucel Aug 04 '20 at 17:33\expandafter\pgfplotslistnew\expandafter\pgfplots@xticklabels\expandafter{\expanded{#1}}. Be care because this requires the ticklabel list being fully expandable. Alternatively, you can expand\xticklabelsBwhen defining\xticklabels,\expandafter\newcommand\expandafter\xticklabels\expandafter{\xticklabelsB}. – muzimuzhi Z Aug 04 '20 at 19:04\expandeddoesn't work with\DTLfetchthough, because it is not expandable. – Carucel Aug 05 '20 at 07:44