Generalreadarray + siunitx and macro expansion fails

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
gallioleo
Posts: 13
Joined: Thu Nov 18, 2021 6:35 pm

readarray + siunitx and macro expansion fails

Post by gallioleo »

Hello Latex Community,

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}
Thanks for your Help!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Stefan Kottwitz
Site Admin
Posts: 10321
Joined: Mon Mar 10, 2008 9:44 pm

readarray + siunitx and macro expansion fails

Post by Stefan Kottwitz »

Hi,

this should work:

Code: Select all

\SI[parse-numbers = false]{\csvvalfield{3,2}}{\volt}
Stefan
LaTeX.org admin
Post Reply