1

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
Ferb300
  • 11
  • 2
  • Welcome to TeX.se. Please don't post code fragments. Instead show a minimal compilable document (even with error) that people can play with. – Alan Munn Oct 20 '23 at 16:19
  • @AlanMunn thank you for the hint! i added a minimal compatible document to my question :) – Ferb300 Oct 20 '23 at 16:27
  • The problem here is that \DTLfetch isn'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
  • Thank you so much, that solved the issue! – Ferb300 Oct 23 '23 at 12:21
  • It would be helpful to others to add an answer showing what you did. I didn't write one because I wasn't sure if it would meet your needs. – Alan Munn Oct 23 '23 at 13:37

0 Answers0