summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/dataref/tex/dataref.sty
diff options
context:
space:
mode:
Diffstat (limited to 'macros/latex/contrib/dataref/tex/dataref.sty')
-rw-r--r--macros/latex/contrib/dataref/tex/dataref.sty116
1 files changed, 100 insertions, 16 deletions
diff --git a/macros/latex/contrib/dataref/tex/dataref.sty b/macros/latex/contrib/dataref/tex/dataref.sty
index bb6f013032..2eb4931c17 100644
--- a/macros/latex/contrib/dataref/tex/dataref.sty
+++ b/macros/latex/contrib/dataref/tex/dataref.sty
@@ -1,4 +1,4 @@
-%% Copyright 2013-2017 Christian Dietrich
+%% Copyright 2013-2022 Christian Dietrich
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
@@ -15,7 +15,7 @@
%% This work consists of the files dataref.dtx and dataref.ins
%% and the derived file dataref.sty.
\NeedsTeXFormat{LaTeX2e}[1999/12/01]
-\ProvidesPackage{dataref}[2017/01/06 v0.6 dataref]
+\ProvidesPackage{dataref}[2022/03/25 v0.7 dataref]
\ifx\drefloaded\undefined
\let\drefloaded=\relax
@@ -33,6 +33,9 @@
\RequirePackage{iftex}
\RequirePackage{kvoptions}
\RequirePackage{etoolbox}
+\RequirePackage{import}
+
+
\SetupKeyvalOptions{
family=dref,
@@ -49,11 +52,14 @@
% Load & Store Layer
+% \dref@xdef is only used in \dref@set
+\let\dref@xdef=\xdef % \dref@xdef is overridden in case of listadd
+
\def\dref@set#1#2#3{%
\edef\dref@set@path{#3}%
\edef\dref@set@value{#2}%
\expandafter\pgfkeys@temptoks\expandafter{\dref@set@value}%
- \expandafter\xdef%
+ \expandafter\dref@xdef%
\csname pgfk@\dref@datapath\dref@set@path\endcsname%
{\the\pgfkeys@temptoks}%
% Setting the Unit is a little bit more tricky
@@ -85,6 +91,16 @@
}%
}
+% List Looping. We define this upfront, as we have to build a pgfk@
+% name here. The @loop@do macro is a helper to invoke list/do with the
+% list element. dref@loop uses the etoolbox for looping feature.
+\def\dref@loop@do#1{\drefkeys{list/do={#1}}}
+\def\dref@loop#1{%
+ \forlistcsloop{\dref@loop@do}{%
+ pgfk@\dref@datapath #1%
+ }%
+}
+
\def\drefifdefined#1{%
\pgfkeysifdefined{\dref@datapath\drefprefix #1}%{then}{else}
}
@@ -107,7 +123,7 @@
.search also={/pgf/number format},
% The @init action is used to initialize all other actions
@[/.code={\begingroup%
- \pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}% No more dimensions too large
+ % /pgf/fpu: \pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}% No more dimensions too large
\def\drefunit{}% At first we are unitless
\aftergroup\edef%
\aftergroup\drefresult%
@@ -186,7 +202,13 @@
\aftergroup\drefresult%
\aftergroup}}
},
- %
+ % List Keys
+ list/append/.code={\let\dref@xdef=\listxadd},
+ % Apply loop/do for every item
+ list/loop/.code={%
+ \dref@loop{\drefprefix #1}
+ },
+ % Various Keys
debug/.is if=dref@debug,
ignoremissing/.is if=dref@ignoremissing,
defaultvalue/.store in=\dref@defaultvalue,
@@ -300,11 +322,27 @@
\drefkeys{@[, @init, value={#3}, #1, save={#2}, @set, @]}%
}
+\def\dref@splitpath@helper#1/#2\@nnil{%
+ \ifx&#2&% Basename found
+ \def\dref@basename{#1}\let\@next\relax%
+ \else% Another Dirname
+ \edef\dref@dirname{\dref@dirname #1/}%
+ \def\@next{\dref@splitpath@helper#2\@nnil}%
+ \fi%
+ \@next%
+}
+\def\dref@splitpath#1{%
+ % Splits path into \dref@dirname and \dref@basename
+ \def\dref@dirname{}%
+ \dref@splitpath@helper #1/\@nnil%
+}
+
\newcommand{\drefinput}[2][]{%
- \begingroup
- \drefkeys{prefix/.append={#1}}
- \input{data/#2}
- \endgroup
+ \begingroup%
+ \drefkeys{prefix/.append={#1}}%
+ \dref@splitpath{#2}%
+ \subimport{\dref@dirname}{\dref@basename}%
+ \endgroup%
}
@@ -460,6 +498,51 @@
\def\dref@parser@end#1#2\@nnil{}
\csdef{dref@parser@@}{\dref@parser@end}
+\newif\ifdrefbadmath
+\newif\ifdref@xfp
+\IfFileExists{xfp.sty}{\dref@xfptrue}{\dref@xfpfalse}
+
+% For LuaLaTeX: use the lua math engine
+\ifLuaTeX
+ \drefbadmathfalse%
+ \def\drefmathparse#1{%
+ \edef\pgfmathresult{\directlua{%
+ abs=math.abs ;
+ result = (#1);
+ if result == false then
+ tex.print(0);
+ elseif result == true then
+ tex.print(1);
+ else
+ tex.print(string.format(string.char(37).."s", result))
+ end
+ }}}%
+% We use the FP Library of expl3. This seems to work better for
+% floating numbers and for integers
+\else \ifdref@xfp
+ \RequirePackage{xfp}
+ \drefbadmathfalse%
+ \ExplSyntaxOn% we want to do tests % Usepackage xfp
+ \cs_set_eq:NN \dref@fpeval \fp_eval:n%
+ \ExplSyntaxOff%
+ \def\drefmathparse#1{%
+ \edef\pgfmathresult{\dref@fpeval{#1}}%
+ }%
+%% Fallback to PGFMath
+\else
+ \drefbadmathtrue%
+ \def\drefmathparse#1{%
+ \pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
+ \pgfmathparse{#1}%
+ }%
+ \dref@warning{****************************************************}%
+ \dref@warning{Dataref will use bad math library (/pgf/fpu). Be aware%
+ of floating point rounding errors}%
+ \dref@warning{****************************************************}%
+ \fi
+\fi
+
+
\drefkeys{
@init/.append style={%
@calc/.code={},%
@@ -467,19 +550,20 @@
@calc pgf/.style={
@calc/.append code={%
\dref@parser{#1}{\dref@calc@@ready}%
- \pgfmathparse{\dref@calc@@ready}%
+ \drefmathparse{\dref@calc@@ready}%
\dref@debug{#1 -> \dref@calc@@ready = \pgfmathresult}%
\edef\drefresult{\pgfmathresult}%
- }
+ }
}
}
+
\protected\def\drefcalc{\@ifstar\drefcalc@starred\drefcalc@unstarred}
\newcommand{\drefcalc@unstarred}[2][]{% Unstarred
\drefkeys{@[,@init, print=default, @calc pgf={#2}, #1, @calc, @print, @set,%
@annotate={\textbackslash drefcalc[#1]\{#2\}},@]}%
}
-\newcommand{\drefcalc@starred}[2][]{ % Starred
+\newcommand{\drefcalc@starred}[2][]{% Starred
\drefkeys{@[,@init, @calc pgf={#2}, #1, @calc, @set,%
@annotate={\textbackslash \drefcalc*[#1]\{#2\}},@]%
}%
@@ -722,7 +806,7 @@
\else%
\dref@error{Assertion failed: #1}%
\fi%
- \else
+ \else%
\dref@warning{Assertion holds: #1}%
\fi%
\endgroup%
@@ -738,7 +822,7 @@
@calc pgf={100 * (\drefresult)},
},
percent of/.style={
- factor of=#1, percent,
+ factor of={#1}, percent,
@calc/.append code={\def\drefunit{}},
},
scale by/.style={
@@ -904,10 +988,10 @@
\def\do####1{%
\edef\dref@unit@key{/dref/units/\expandafter\detokenize\expandafter{\drefunit} to \detokenize{####1}}%
\pgfkeys{\dref@unit@key/.get=\dref@unit@factor}%
- \pgfmathparse{\@@score{\drefunit@bestvalue} < \@@score{\drefresult \dref@unit@factor}}%
+ \drefmathparse{\@@score{\drefunit@bestvalue} < \@@score{\drefresult \dref@unit@factor}}%
%\typeout{\drefunit \pgfmathresult \detokenize{####1}}%
\ifdim\pgfmathresult pt=1.0 pt%
- \pgfmathparse{(\drefresult) \dref@unit@factor}%
+ \drefmathparse{(\drefresult) \dref@unit@factor}%
\edef\drefunit@bestvalue{\pgfmathresult}%
\def\drefunit@best{####1}%
%\typeout{\expandafter\detokenize\expandafter{\drefunit} to