0

I use datatool to get key-value pairs from a data file into a database. I then retrieve values using a custom command, \varConfig{}.

One of these values is the number 1, saved as a string in the data file.

My command \varConfig{} gives me the number "1" as a string if I use the command in the body of the document.

I want to use that number as an input to another command that generates the path to a file. At the moment I am hard coding it.

\input{\reviewfile{1}{conclusions.tex}}

But simply replacing the 1 with the \varConfig{} command, like this...

\input{\reviewfile{\varConfig{report-number}}{conclusions.tex}}

... gives me the error

illegal parameter number in definition of \reviewfle

How do I avoid this error? I'm guessing it is somehow related to expansion, but I have no idea how to use \expandafter appropriately in this case.

edit: I have tried the solutions at Pass the contents of an \input command to a macro, for example

\expandafter\input\expandafter{\reviewfile{\varConfig{report-number}}{conclusions.tex}}

but that just gives me more errors.

edit: MWE

\documentclass[9pt,notitlepage]{article}

\RequirePackage{datatool} \DTLsetseparator{:}% Set the separator between the columns.

% import data from test.csv into a database called 'config' \DTLloaddb[noheader, keys={key,value}]{config}{config.csv}

\newcommand{\varConfig}[1]{% \DTLgetvalueforkey{\scratchmacro}{value}{config}{key}{#1}% \DTLifnull{\scratchmacro}{\textbf{ERROR!} key #1 is undefined.}{\scratchmacro}% }%

% create new command \newcommand{\reviewfile}[2]{#1/#2}

\begin{document}

\varConfig{block-text}

\begin{table}[!htbp] \caption{\protect\varConfig{short-text}} \begin{tabular}{lr} Name: & \varConfig{block-text} \ \end{tabular} \end{table}

\varConfig{report-number}

\input{\reviewfile{1}{test.tex}}

%\input{\reviewfile{\varConfig{report-number}}{test.tex}}

\end{document}

the data in config.csv look like this:

short-text: "hello world" 
block-text: "this is some long text to see what happens"
report-number: 1

I then have the file 1/test.tex, which is a dummy file containing the text:

THIS IS A TEST

the output should look like this: if it works

Andy Clifton
  • 3,699
  • I think the answer might be related to https://tex.stackexchange.com/questions/88710/pass-the-contents-of-an-input-command-to-a-macro, so will play wiht that a while – Andy Clifton Feb 09 '24 at 15:26
  • Try (can't myself, because there is no MWE; so it's a wild guess because I have no idea if some of the inner macro is not expandable)... \edef\tmp{\varConfig{report-number}} and then use \reviewfile{\tmp}{... – Rmano Feb 09 '24 at 15:39
  • 1
    As far as I know DTL commands are not expandable. Even if it's expandable the outer command might not expand it. (case 2 and 3 in my generic answer) It would be nice if you can send a MWE, so I can minimally modify it to make it work and quickly test it. – user202729 Feb 09 '24 at 15:43
  • 1
    your \varconfig command contains (beside others) a \hphantom{}. That can never work in a place where you expect a number. Use your \varconfig before the \input and then use \scratchmacro inside the input (untested due to the lack of MWE). – Ulrike Fischer Feb 09 '24 at 15:52
  • Sorry about that - I've added the MWE and confirmed that the \hphantom{} is not required. I had a hard time making \varConfig{} fail gracefully so would be willing to rewrite that if required. – Andy Clifton Feb 09 '24 at 16:40
  • @Rmano - still getting an error. This time \@dtl@row -> \db@col@id .... – Andy Clifton Feb 09 '24 at 17:12
  • @UlrikeFischer Thanks! Using \varConfig{report-number} \input{\reviewfile{\scratchmacro}{test.tex}} works, but then I have a random "1" just in the middle of my page from the \scratchmacro call. How would I supress that? – Andy Clifton Feb 09 '24 at 17:15

0 Answers0