4

I want to define a command with key arguments, which stores something in a table with help of the package datatool. My attempt is :

\documentclass[12pt]{scrartcl}
\usepackage{keycommand}
\RequirePackage{datatool}
\begin{document}
  % declare a new table
\DTLnewdb{table}
  % define the command
\newkeycommand{\myCom}[op=1]{%
\DTLnewrow{table}
\DTLnewdbentry{table}{option}{\commandkey{op}}
}
%call 4 times the command
\myCom[op=2] 
\myCom[op=3]
\myCom[op=4]
\myCom[op=5]
% display the content of the table
\DTLdisplaydb{table}

I would expect "2 3 4 5", but i get "5 5 5 5". This is actually a common problem that i encounter regularly with datatools (that somehow "only the last operation is taken into account"), which shows probably that i still don't know how to use it properly... What am i doing wrong here ?

0

1 Answer 1

8

I checked the datatool manual and found the following note about \DTLnewdbentry (page 32):

The value isn’t expanded by default, but you can change this using the declaration: \dtlexpandnewvalue. This can be localised by placing it in a group, or you can switch back using: \dtlnoexpandnewvalue.

I simply rewrote \myCom:

\newkeycommand{\myCom}[op=1]{%
\DTLnewrow{table}
\dtlexpandnewvalue
\DTLnewdbentry{table}{option}{\commandkey{op}}%
\dtlnoexpandnewvalue
}

Now the output is correct:

Datatool output

3
  • great ! i think, this solves some of my other problems with datatool ! :) Jan 9, 2012 at 13:25
  • @nicolasroy: glad to hear! :) I updated the code with \dtlnoexpandnewvalue right after the insertion. Jan 9, 2012 at 13:29
  • yes, without \dtlnoexpandnewvalue, one gets some bad errors... Jan 9, 2012 at 14:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .