You are forgetting to expand \Value
, which can be easily done by adding a couple of \expandafter
tokens. I prefer to separate things and hide the complicated code into a macro rather than the argument to \csvloop
.
The idea is of exploiting the fact that \csname
does full expansion, so an \expandafter
in front of \endcsname
is expanded, which triggers the expansion of the next one, which finally expands (once) \Value
. Otherwise you'd essentially obtain \newcommand\testNumber{\Value}
, so the current meaning of \Value
would be used, not the one at definition time.
However, there's a small complication: \Value
must be expanded twice, because the first level expansion of \Value
is \csvcolii
(an internal representation of the cell content). Not a problem: use a triple \expandafter
sequence.
\begin{filecontents*}{\jobname.csv}
Key, Value
testNumber, 001
vehicle, Volkswagen Beetle
\end{filecontents*}
\documentclass{scrreprt}
\usepackage{csvsimple}
\newcommand{\definekeytovalue}[2]{%
\expandafter\newcommand
\csname#1\expandafter\expandafter\expandafter\endcsname
\expandafter\expandafter\expandafter{#2}%
}
\csvloop{
file={\jobname.csv},
head to column names,
command=\definekeytovalue{\Key}{\Value}
}
\begin{document}
During test number TR-\testNumber{} a \vehicle{} was subject to etc....
\end{document}
