I am importing key-value pairs from a dat file using the package datatool like this:
\DTLloaddb[noheader, keys={k,v}]{data}{data/data.dat}
\newcommand{\var}[1]{\DTLfetch{data}{k}{#1}{v}}
After that, the data becomes available using the \var{key} command. This works everywhere in the document with all of the key/value pairs from the file. Now I want to use some of this data to configure a pgfplots axis. This is my attempt:
\begin{axis}[
% a lot of other settings...
xmax=\var{xmax}
]
% ...
\end{axis}
But this fails, giving me this error message: Illegal parameter number in definition of \pgfmath@bgroup@strip@last. and a bunch of Undefined control sequences
How can I use the imported data to configure pgfplots?
Thank you so much for your help!
Minimal compatible document
main.tex
\documentclass{report}
\usepackage{datatool}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{units}
\DTLsetseparator{=}% Set the separator between the columns.
\DTLloaddb[noheader, keys={k,v}]{data}{data.dat}
\newcommand{\var}[1]{\DTLfetch{data}{k}{#1}{v}}
\begin{document}
\var{xmax} % this works
\begin{tikzpicture}
\begin{axis}[
xmax=\var{xmax} % this doesnt
]
\end{axis}
\end{tikzpicture}
\end{document}
data.dat
k=v
xmax=350
\DTLfetchisn't expandable. A solution would be get the data outside of the plot using the technique here rather than trying to do everything inside the plot itself. – Alan Munn Oct 20 '23 at 18:47