2

I'm writing a MatLab-LaTeX "interface", which includes a LaTeX command \data[options]{tag}. It reads the value named tag from a .csv file, and outputs it through siunitx.

I want the user to be able to call the command with a key filename to pick a .csv file ("data.csv" default), and pass any and all other options on to siunitx, preferably with defaults.

Right now, hard-coding in a couple of options works, but it gives me no control in-document.

My code follows:

% datax.sty
\ProvidesPackage{datax}

\RequirePackage{csvsimple}
\RequirePackage{siunitx}
\RequirePackage{pgfkeys}

\makeatletter
\pgfqkeys{/data}%
{filename/.estore in = \@filename,%
filename/.default = data.csv%
}

\newcommand{\data}[2][]{%
  \pgfqkeys{/siunitx}{/remainingkeys/.style={}}%
  \pgfqkeys{/data}{filename}%
  \pgfqkeys{/data}{#1}%
  \csvreader[filter strcmp={\@datatag}{#2}]%
  {\@filename}{1=\@datatag,2=\@datavalue,3=\@dataunit}%
  {%
    \SI[round-mode=figures,round-precision=3,scientific-notation=true] {\@datavalue}{\@dataunit}%
  }%
}

\makeatother

,

% Inside document
The length was measured to \data[filename=newdata.csv,scientific-notation=false]{length}.

and

% newdata.csv
tag,value,unit
length,23.45,\meter

1 Answer 1

4

Here is a solution:

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{\jobname.csv}
tag,value,unit
length,23.45,m
\end{filecontents*}

% pseudo package 'datax'
\makeatletter
\RequirePackage{csvsimple}
\RequirePackage{siunitx}
\RequirePackage{pgfkeys}
\pgfkeys{/datax/.is family}
\def\dataxkeys#1{\pgfkeys{/datax,#1}}
\dataxkeys{
  filename/.estore in=\datax@filename,
  siunitx options/.store in=\datax@siunitxoptions,
  % defaults values,
  filename=data.csv,
  siunitx options=,
}
\newcommand{\data}[2][]{%
  \dataxkeys{#1}%
  \csvreader[filter strcmp={\datax@tag}{#2}]%
  {\datax@filename}{1=\datax@tag,2=\datax@value,3=\datax@unit}
  {%
    \bgroup% to keep \sisetup change local 
    \sisetup{round-mode=figures,round-precision=3,scientific-notation=true}%
    \expandafter\SI\expandafter[\datax@siunitxoptions]%
    {\datax@value}{\datax@unit}%
    \egroup%
  }%
}
\makeatother
% end of pseudo package 'datax'

\begin{document}
The length was measured to
\data[filename=\jobname.csv,
siunitx options={scientific-notation=false},
]{length}.

The length was measured to
\data[filename=\jobname.csv,
siunitx options={round-precision=2},
]{length}.
\end{document}
1
  • This technically solves the question, but leaves some room for improvement. For one thing, it's a bit clunky. I'd prefer if I could just write the option directly, without the "siunitx options"-part. Another thing is that this would overwrite all the datax defaults, not just the option changed, right?
    – Gustaphe
    Commented Feb 3, 2017 at 9:36

You must log in to answer this question.

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