继承 pgfkeys

继承 pgfkeys

我正在编写一个 MatLab-LaTeX “界面”,其中包括一个 LaTeX 命令。它从 .csv 文件中\data[options]{tag}读取命名的值,并通过 输出它。tagsiunitx

我希望用户能够使用键调用命令filename来选择一个.csv 文件(默认值为“data.csv”),并将任何和所有其他选项传递给siunitx,最好使用默认值。

现在,对几个选项进行硬编码是可行的,但它却无法让我控制文档。

我的代码如下:

% 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}.

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

答案1

这是一个解决方案:

\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}

相关内容