I would like to define new commands and be able to change the output from a csv-file. Let's say the csv-file is semicolon seperated:
Name; Score
Max; 12
Peter; 32
Anna; 27
Now I would like to have the following in LaTeX:
\newcommand{\Max}{12}
\newcommand{\Peter}{32}
\newcommand{\Anna}{27}
1. Basic soulution: Only the numbers (= 12
, 32
, and 27
) should be read from the csv-file. The command names (= \Max
, \Peter
, and \Anna
) can be hardcoded.
2. Advanced solution: Loop through all rows of the csv-file (optional: beginning with the second row) and create new commands based on the names in the first column (= command name) and the score in the second column (= desired output of each command name).
How do I achieve any of the solutions above? There are a lot of examples of reading csv-files and creating a table out of them in LaTeX, but I haven't found anything that solves my problem.
Thanks in advance!
Update: Added minimal working example (MWE) inspired by the following website: https://www.uweziegenhagen.de/?p=3100
The following code loops through my csv file and gives me the names and the scores. It is not a solution to my problem, but it could be part of it:
\documentclass{article}
\usepackage{datatool}
\DTLsetseparator{;}
\DTLloaddb{scores}{test.csv}
\begin{document}
\DTLforeach{scores}{%
\name=Name,\score=Score}{%
\name\score\\}
\end{document}
Now when I try to solve my problem, I fail to add the new command names programatically (\Max
, \Peter
, and \Anna
). I get the error: Command \textbackslash already defined. \newcommand{\textbackslash \name}{\score}}
. If this would work it would be the "advanced solution" which would have been very nice:
\documentclass{article}
\usepackage{datatool}
\DTLsetseparator{;}
\DTLloaddb{scores}{test.csv}
\DTLforeach{scores}{%
\name=Name,\score=Score}{%
\newcommand{\textbackslash \name}{\score}}
\begin{document}
\Max
\Peter
\Anna
\end{document}
csvsimple
package, but maybe my problem could be solved using it. I would have to take a deeper look into it..cvs
file by using macros bound to the column's value. But I suspect this is a XY problem --- why don't you put a simple minimal working example (MWE) showing what you want to achieve?