i try to read values from a csv file and propagate it into the package siunitx. But siunitx expects values und no macro code. How can i expand it to the real value? Below the MWE:
Code: Select all
\documentclass[
fontsize=12pt,% changed because of the package info in the log file
DIV=14
]{scrreprt}
\usepackage[english]{babel}
\usepackage[binary-units=true, detect-all]{siunitx}[=v2]
\usepackage{readarray}
\usepackage{xifthen}
% siunitx settings
\sisetup {
exponent-to-prefix = true, % convert exponents into belonging prefixes
round-mode = places,
scientific-notation = engineering,% try to use next engineering unit instead of '.'
per-mode = symbol,
per-symbol = /
}
% Introduces command \csvvalfile[separator]{file.csv} and stores result in \csvvalfield{row, column}
%
% https://tex.stackexchange.com/questions/562789/how-to-look-up-a-cell-value-of-a-multi-column-csv-file
% https://tex.stackexchange.com/questions/301665/error-when-using-num-command-from-siunitx-package-with-an-array-from-arrayjob-p
\newcommand{\csvvalfile}[2][]{%
% https://stackoverflow.com/questions/2144176/latex-newcommand-default-argument-is-empty
\ifthenelse{\isempty{#1}} {%default separator
\readarraysepchar{,}
} {% else
\readarraysepchar{#1}
}%
\hypercheckbounds
\readdef{#2}\csvvaldata
\readarray\csvvaldata\csvvalfielddata[-,\ncols]
}
\newcommand{\csvvalfield}[1]{%
\arraytomacro\csvvalfielddata[#1]\csvvalfieldval
\csvvalfieldval
}
\begin{document}
\begin{filecontents*}{test.csv}
val11; 0.1
val21; 0.5
val31; 5e-3
val41; 22
\end{filecontents*}
\csvvalfile[;]{test.csv}
OK: \csvvalfield{1,1}
OK: \csvvalfield{3,2}
OK: \csvvalfield{3,2}\SI{\csvvalfieldval}{\volt}
FAIL: \textbackslash SI\{\textbackslash csvvalfield\{3,2\}\}\{\textbackslash volt\}
%\SI{\csvvalfield{3,2}}{\volt}
\end{document}