summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/non-decimal-units/latex/non-decimal-units.sty
diff options
context:
space:
mode:
Diffstat (limited to 'macros/latex/contrib/non-decimal-units/latex/non-decimal-units.sty')
-rw-r--r--macros/latex/contrib/non-decimal-units/latex/non-decimal-units.sty331
1 files changed, 188 insertions, 143 deletions
diff --git a/macros/latex/contrib/non-decimal-units/latex/non-decimal-units.sty b/macros/latex/contrib/non-decimal-units/latex/non-decimal-units.sty
index 5358a0acdb..2cd0bd9377 100644
--- a/macros/latex/contrib/non-decimal-units/latex/non-decimal-units.sty
+++ b/macros/latex/contrib/non-decimal-units/latex/non-decimal-units.sty
@@ -1,5 +1,5 @@
-\RequirePackage{expl3}
-\ProvidesExplPackage{non-decimal-units}{2023/10/10}{1.0}{Macros for displaying and manipulating historical non-decimal units}
+\RequirePackage{expl3}[2024/01/25]
+\ProvidesExplPackage{non-decimal-units}{2024/01/25}{1.0.1}{Macros for displaying and manipulating historical non-decimal units}
% License: CC-BY-SA 4.0
% Author: Mikkel Eide Eriksen <mikkel.eriksen@gmail.com>
@@ -11,37 +11,37 @@
\keys_define:nn { non-decimal-units }
{
% nil/zero
- replace~nil~with .tl_set:N = \l_ndu_replace_nil_with_tl ,
- treat~zero~as~nil .bool_set:N = \l_ndu_treat_zero_as_nil_bool ,
+ replace~nil~with .tl_set:N = \l__ndu_replace_nil_with_tl ,
+ treat~zero~as~nil .bool_set:N = \l__ndu_treat_zero_as_nil_bool ,
treat~zero~as~nil .default:n = true ,
% formatting
- unit~separator .tl_set:N = \l_ndu_unit_separator_tl ,
+ unit~separator .tl_set:N = \l__ndu_unit_separator_tl ,
unit~separator .default:n = { \nobreakspace } ,
- default~format .tl_set:N = \l_ndu_default_format_tl ,
+ default~format .tl_set:N = \l__ndu_default_format_tl ,
default~format .default:n = { \VALUE\nobreakspace\SYMBOL } ,
display .choice: ,
display .choices:nn =
- { values~only, symbols~only, formatted, numprint }
- { \tl_set_eq:NN \l_ndu_display_choice_tl \l_keys_choice_tl } ,
+ { values~only, symbols~only, formatted }
+ { \tl_set_eq:NN \l__ndu_display_choice_tl \l_keys_choice_tl } ,
display .default:n = { formatted } ,
- unit~depth .tl_set:N = \l_ndu_unit_depth_tl ,
- use~numprint .bool_set:N = \l_ndu_use_numprint_bool ,
+ unit~depth .tl_set:N = \l__ndu_unit_depth_tl ,
+ use~numprint .bool_set:N = \l__ndu_use_numprint_bool ,
use~numprint .default:n = true ,
% tabular alignment
- aligned .bool_set:N = \l_ndu_aligned_bool ,
+ aligned .bool_set:N = \l__ndu_aligned_bool ,
aligned .default:n = true ,
cell~widths .code:n = {
- \seq_set_from_clist:Nn \l_ndu_cell_widths_seq {#1}
+ \seq_set_from_clist:Nn \l__ndu_cell_widths_seq {#1}
} ,
cell~widths .default:n = 3em ,
% math
- normalize .bool_set:N = \l_ndu_normalize_bool ,
+ normalize .bool_set:N = \l__ndu_normalize_bool ,
normalize .default:n = true ,
- current~variable .tl_set:N = \l_ndu_current_variable_tl ,
- current~operator .tl_set:N = \l_ndu_current_operator_tl ,
+ current~variable .tl_set:N = \l__ndu_current_variable_tl ,
+ current~operator .tl_set:N = \l__ndu_current_operator_tl ,
add~to~variable .meta:n = {
current~variable = {#1} ,
current~operator = {+}
@@ -70,14 +70,23 @@
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% Errors
+% Errors and log messages
%
\msg_new:nnnn
+{ non-decimal-units } { base-unit-exists }
+{ A ~ base ~ unit ~ named ~ #1 ~ already ~ exists }
+{ You ~ will ~ need ~ to ~ give ~ it ~ another ~ name ~ or ~ load ~ less ~ units. }
+
+\msg_new:nnnn
+{ non-decimal-units } { unit-group-exists }
+{ A ~ unit ~ group ~ named ~ #1 ~ already ~ exists }
+{ You ~ will ~ need ~ to ~ give ~ it ~ another ~ name ~ or ~ load ~ less ~ units. }
+
+\msg_new:nnnn
{ non-decimal-units } { missing-base-unit }
{ No ~ base ~ unit ~ exists ~ named ~ #1 }
{ You ~ will ~ need ~ to ~ add ~ it ~ with ~ nduNewBaseUnit. }
-%
\msg_new:nnnn
{ non-decimal-units } { missing-unit-group }
@@ -89,47 +98,58 @@
{ No ~ conversion ~ factor ~ exists ~ between ~ #1 ~ and ~ #2 }
{ You ~ will ~ need ~ to ~ add ~ it ~ when ~ creating ~ the ~ base ~ unit ~ or ~ via ~ nduKeys. }
+\msg_new:nnn
+{ non-decimal-units } { calculating-factor }
+{ Calculating ~ conversion ~ factor ~ between ~ #1 ~ and ~ #2 ~ via ~ #3 }
+
+\msg_new:nnn
+{ non-decimal-units } { found-factor }
+{ Found ~ conversion ~ factor ~ between ~ #1 ~ and ~ #2: ~ #3 }
+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Unit setup
%
\NewDocumentCommand { \nduNewBaseUnit } { m m } {
- \prop_clear_new:c { l_ndu_factors_#1_prop }
+ \tl_if_exist:cT { l__ndu_factors_#1_prop }
+ {
+ \msg_error:nnn { non-decimal-units } { base-unit-exists } { #1 }
+ }
+ \prop_clear_new:c { l__ndu_factors_#1_prop }
\keys_define:nn { non-decimal-units / units / #1 } {
factor .code = {
\regex_extract_once:nnN { (\d+)\ (\D+) } { ##1 } \l_tmpa_seq
- \prop_put:cxx { l_ndu_factors_#1_prop } { \seq_item:Nn \l_tmpa_seq 3 } { \seq_item:Nn \l_tmpa_seq 2 }
+ \prop_put:cee { l__ndu_factors_#1_prop } { \seq_item:Nn \l_tmpa_seq 3 } { \seq_item:Nn \l_tmpa_seq 2 }
} ,
- symbol .tl_set:c = { l_ndu_symbol_#1_tl } ,
- format .tl_set:c = { l_ndu_format_#1_tl } ,
+ symbol .tl_set:c = { l__ndu_symbol_#1_tl } ,
+ format .tl_set:c = { l__ndu_format_#1_tl } ,
}
\keys_set:nn { non-decimal-units / units / #1 } { #2 }
}
-\NewDocumentCommand { \nduNewUnitGroup } { m o m o }
+\NewDocumentCommand { \nduNewUnitGroup } { m O{} m o }
{
- \seq_set_from_clist:cn { l_ndu_group_#1_seq } {#3}
+ \tl_if_exist:cT { l__ndu_group_#1_seq }
+ {
+ \msg_error:nnn { non-decimal-units } { unit-group-exists } { #1 }
+ }
+ \seq_set_from_clist:cn { l__ndu_group_#1_seq } {#3}
% set base unit to rightmost item
- \seq_get_right:cN { l_ndu_group_#1_seq } \l_tmpa_tl
- \tl_set_eq:cN { l_ndu_base_unit_#1_tl } \l_tmpa_tl
+ \seq_get_right:cN { l__ndu_group_#1_seq } \l_tmpa_tl
+ \tl_set_eq:cN { l__ndu_base_unit_#1_tl } \l_tmpa_tl
% store keys for unit group
- \tl_clear_new:c { l_ndu_options_#1_tl }
+ \tl_clear_new:c { l__ndu_options_#1_tl }
\IfValueT {#2} {
- \tl_set:cn { l_ndu_options_#1_tl } {#2}
+ \tl_set:cn { l__ndu_options_#1_tl } {#2}
}
% optionally create macro
\IfValueT {#4} {
\NewDocumentCommand { #4 } { O{} m } {
\group_begin:
- \IfValueTF {#2} {
- \nduValue{#1}[#2,##1]{##2}
- }
- {
- \nduValue{#1}[##1]{##2}
- }
+ \nduValue{#1}[#2,##1]{##2}
\group_end:
}
}
@@ -145,11 +165,11 @@
% #1: output result (int)
% #2: from unit (tl)
% #3: to unit (tl)
- \tl_if_exist:cF { l_ndu_factors_#2_prop }
+ \tl_if_exist:cF { l__ndu_factors_#2_prop }
{
\msg_error:nnn { non-decimal-units } { missing-base-unit } { #2 }
}
- \tl_if_exist:cF { l_ndu_factors_#3_prop }
+ \tl_if_exist:cF { l__ndu_factors_#3_prop }
{
\msg_error:nnn { non-decimal-units } { missing-base-unit } { #3 }
}
@@ -158,20 +178,22 @@
\int_set:Nn #1 { 1 }
}
{
- \prop_if_in:cnTF { l_ndu_factors_#2_prop } { #3 }
+ \prop_if_in:cnTF { l__ndu_factors_#2_prop } { #3 }
{
- \prop_get:cnN { l_ndu_factors_#2_prop } { #3 } \l_tmpa_tl
+ \prop_get:cnN { l__ndu_factors_#2_prop } { #3 } \l_tmpa_tl
+% \msg_term:nnnee { non-decimal-units } { found-factor } { #2 } { #3 } { \tl_use:N \l_tmpa_tl }
\int_set:Nn #1 { \l_tmpa_tl }
}{
\int_zero_new:N \l__ndu_factor_product_int
- \prop_map_inline:cn { l_ndu_factors_#2_prop } {
+ \prop_map_inline:cn { l__ndu_factors_#2_prop } {
\str_if_eq:nnF {#3} {##1}
{
+% \msg_term:nnnee { non-decimal-units } { calculating-factor } { #2 } { #3 } { ##1 }
\int_zero_new:N \l__ndu_sub_factor_int
\ndu_get_factor:Nnn \l__ndu_sub_factor_int { ##1 } { #3 }
\int_set:Nn \l__ndu_factor_product_int { ##2 * \l__ndu_sub_factor_int }
\int_set:Nn #1 { \l__ndu_factor_product_int }
- \prop_put:cnx { l_ndu_factors_#2_prop } { #3 } { \int_use:N \l__ndu_factor_product_int }
+ \prop_put:cnV { l__ndu_factors_#2_prop } { #3 } \l__ndu_factor_product_int
}
}
}
@@ -200,7 +222,7 @@
% #2: unit group (seq)
% #3: base unit (tl)
% #4: value (int)
- % #5: unit (tl)
+ % #5: unit depth (tl)
\seq_clear:N #1
\int_zero_new:N \l__ndu_remainder_int
@@ -220,11 +242,11 @@
% while (remainder >= factor_to_base):
\int_while_do:nn {\l__ndu_remainder_int >= \l__ndu_factor_int} {
% remainder -= factor_to_base
- \int_sub:Nn \l__ndu_remainder_int \l__ndu_factor_int
+ \int_sub:Nn \l__ndu_remainder_int { \l__ndu_factor_int }
% result[i] += 1
\int_incr:N \l__ndu_sum_int
}
- \seq_put_right:Nx #1 {\int_use:N \l__ndu_sum_int}
+ \seq_put_right:NV #1 \l__ndu_sum_int
% stop when we hit the desired unit depth
\str_if_eq:NNT {##1} {#5} {
@@ -234,9 +256,11 @@
}
\cs_generate_variant:Nn \ndu_repr_to_seq:NNNNN {
NccNc,
- Ncccc
+ Ncccc,
+ NccNN
}
+% convenience function
\cs_new_protected:Nn \ndu_repr_to_seq:NnN
{
% parameters
@@ -245,10 +269,10 @@
% #3: value (int)
\ndu_repr_to_seq:NccNc
#1
- { l_ndu_group_#2_seq }
- { l_ndu_base_unit_#2_tl }
+ { l__ndu_group_#2_seq }
+ { l__ndu_base_unit_#2_tl }
#3
- { l_ndu_base_unit_#2_tl }
+ { l__ndu_base_unit_#2_tl }
}
\cs_generate_variant:Nn \ndu_repr_to_seq:NnN {
@@ -267,7 +291,7 @@
\seq_map_indexed_inline:Nn #2
{
- \tl_set:Nx \l__ndu_value_tl { \seq_item:Nn #4 {##1} }
+ \tl_set:Ne \l__ndu_value_tl { \seq_item:Nn #4 {##1} }
\tl_if_blank:VF \l__ndu_value_tl {
\ndu_get_factor:NnV \l_tmpa_int { ##2 } { #3 }
\int_add:Nn #1 { \l__ndu_value_tl * \l_tmpa_int }
@@ -278,6 +302,7 @@
NccN
}
+% convenience function
\cs_new_protected:Nn \ndu_seq_to_repr:NnN
{
% parameters
@@ -286,8 +311,8 @@
% #3: values (seq)
\ndu_seq_to_repr:NccN
#1
- { l_ndu_group_#2_seq }
- { l_ndu_base_unit_#2_tl }
+ { l__ndu_group_#2_seq }
+ { l__ndu_base_unit_#2_tl }
#3
}
\cs_generate_variant:Nn \ndu_seq_to_repr:NnN {
@@ -308,53 +333,48 @@
\tl_if_blank:nTF {#2}
{
% only output something if we are replacing nil
- \tl_if_empty:NTF {\l_ndu_replace_nil_with_tl}
+ \tl_if_empty:NTF {\l__ndu_replace_nil_with_tl}
{
\prg_return_false:
}
{
- \tl_set_eq:NN #1 \l_ndu_replace_nil_with_tl
+ \tl_set_eq:NN #1 \l__ndu_replace_nil_with_tl
\prg_return_true:
}
}
{
\bool_if:nTF {
- \l_ndu_treat_zero_as_nil_bool &&
+ \l__ndu_treat_zero_as_nil_bool &&
\int_compare_p:n { #2 = 0 }
} {
% only output something if we are replacing nil
- \tl_if_empty:NTF {\l_ndu_replace_nil_with_tl}
+ \tl_if_empty:NTF {\l__ndu_replace_nil_with_tl}
{
\prg_return_false:
}
{
- \tl_set_eq:NN #1 \l_ndu_replace_nil_with_tl
+ \tl_set_eq:NN #1 \l__ndu_replace_nil_with_tl
\prg_return_true:
}
}
{
% else output directly
- \tl_set:Nx #1 { #2 }
+ \tl_set:Ne #1 { #2 }
\prg_return_true:
}
}
}
\prg_generate_conditional_variant:Nnn \ndu_if_not_nil_or_zero:Nn
{
- Nx
+ Ne
}
{
T,TF
}
-% placeholder tokens for \ndu_format_display_helper
-\tl_new:N \VALUE
-\tl_new:N \SYMBOL
-
\tl_const:Nn \c__ndu_values_only_tl { values~only }
\tl_const:Nn \c__ndu_symbols_only_tl { symbols~only }
\tl_const:Nn \c__ndu_formatted_tl { formatted }
-\tl_const:Nn \c__ndu_numprint_tl { numprint }
\cs_new_protected:Nn \ndu__numprint_helper:nn
{
@@ -368,13 +388,13 @@
% #2: format (tl)
% #3: symbol (tl)
% #4: value (int)
- \tl_case:Nn \l_ndu_display_choice_tl {
+ \tl_case:Nn \l__ndu_display_choice_tl {
\c__ndu_symbols_only_tl {
\tl_set:Nn #1 { #3 }
}
\c__ndu_values_only_tl {
\tl_set:Nn #1 {
- \bool_if:NTF \l_ndu_use_numprint_bool
+ \bool_if:NTF \l__ndu_use_numprint_bool
{ \ndu__numprint_helper:nn {} {#4} }
{ #4 }
}
@@ -383,19 +403,15 @@
\tl_set:Nn #1 { #2 }
\tl_replace_all:Nnn #1 { \SYMBOL } { #3 }
\tl_replace_all:Nnn #1 { \VALUE } {
- \bool_if:NTF \l_ndu_use_numprint_bool
+ \bool_if:NTF \l__ndu_use_numprint_bool
{ \ndu__numprint_helper:nn {} {#4} }
{ #4 }
}
}
- \c__ndu_numprint_tl {
- \tl_set:Nn #1 { \ndu__numprint_helper:nn {#3} {#4} }
- }
}
}
\cs_generate_variant:Nn \ndu_format_display_helper:Nnnn {
- NVVn,
- NVVx
+ NVVV
}
\cs_new_protected:Nn \ndu_format_values:NNNN
@@ -411,36 +427,40 @@
\seq_map_indexed_inline:Nn #3
{
\tl_clear_new:N \l__ndu_formatted_segment
+
+ % get symbol
\tl_clear_new:N \l__ndu_symbol_tl
- \tl_set:Nx \l__ndu_symbol_tl { \tl_use:c { l_ndu_symbol_ \seq_item:Nn #2 {##1} _tl } }
+ \tl_set:Ne \l__ndu_symbol_tl { \tl_use:c { l__ndu_symbol_ \seq_item:Nn #2 {##1} _tl } }
+ % get format
\tl_clear_new:N \l__ndu_format_tl
- \tl_if_empty:cTF { l_ndu_format_ \seq_item:Nn #2 {##1} _tl }
+ \tl_if_empty:cTF { l__ndu_format_ \seq_item:Nn #2 {##1} _tl }
{
- \tl_set_eq:NN \l__ndu_format_tl \l_ndu_default_format_tl
+ \tl_set_eq:NN \l__ndu_format_tl \l__ndu_default_format_tl
}
{
- \tl_set_eq:Nc \l__ndu_format_tl { l_ndu_format_ \seq_item:Nn #2 {##1} _tl }
+ \tl_set_eq:Nc \l__ndu_format_tl { l__ndu_format_ \seq_item:Nn #2 {##1} _tl }
}
+ % build formatted segment and append to putput
\ndu_if_not_nil_or_zero:NnT \l_tmpa_tl { ##2 }
{
- \ndu_format_display_helper:NVVx
+ \ndu_format_display_helper:NVVV
\l__ndu_formatted_segment
\l__ndu_format_tl
\l__ndu_symbol_tl
- { \l_tmpa_tl }
- \seq_put_right:Nx \l__ndu_formatted_segments_seq \l__ndu_formatted_segment
+ \l_tmpa_tl
+ \seq_put_right:NV \l__ndu_formatted_segments_seq \l__ndu_formatted_segment
}
% stop when we hit the desired unit depth
- \tl_set:Nx \l__ndu_unit_tl { \seq_item:Nn #2 {##1} }
- \tl_if_eq:NNT \l__ndu_unit_tl \l_ndu_unit_depth_tl
+ \tl_set:Ne \l__ndu_unit_tl { \seq_item:Nn #2 {##1} }
+ \tl_if_eq:NNT \l__ndu_unit_tl \l__ndu_unit_depth_tl
{
\seq_map_break:
}
}
- \tl_set:Nn #1 { \seq_use:Nn {\l__ndu_formatted_segments_seq} {#4} }
+ \tl_set:Nn #1 { \seq_use:Nn \l__ndu_formatted_segments_seq {#4} }
}
\cs_generate_variant:Nn \ndu_format_values:NNNN {
NcNN
@@ -470,15 +490,18 @@
% parameter
% #1: output result (box)
% #2: contents (tl)
- % #3: box index
+ % #3: unit index
\box_clear_new:N #1
+
+ % find configured width for index
\dim_zero_new:N \l__ndu_cell_width_dim
- \tl_set:Nx \l_tmpa_tl { \seq_item:Nn \l_ndu_cell_widths_seq { #3 } }
+ \tl_set:Ne \l_tmpa_tl { \seq_item:Nn \l__ndu_cell_widths_seq { #3 } }
\ndu_if_valid_dim:VF \l_tmpa_tl
{
- \seq_get_right:NN \l_ndu_cell_widths_seq \l_tmpa_tl
+ \seq_get_right:NN \l__ndu_cell_widths_seq \l_tmpa_tl
}
\dim_set:Nn \l__ndu_cell_width_dim { \l_tmpa_tl }
+
\hbox_set_to_wd:Nnn #1 { \l__ndu_cell_width_dim } { #2 }
}
\cs_generate_variant:Nn \ndu_make_box:Nnn {
@@ -497,7 +520,7 @@
\seq_map_indexed_inline:Nn #2
{
\tl_clear_new:N \l__ndu_value_tl
- \ndu_if_not_nil_or_zero:NxTF \l__ndu_value_tl { \seq_item:Nn #3 ##1 }
+ \ndu_if_not_nil_or_zero:NeTF \l__ndu_value_tl { \seq_item:Nn #3 ##1 }
{
\ndu_make_box:cnn
{ l__ndu_cell_ \int_to_alph:n {##1} _box }
@@ -511,10 +534,10 @@
{ ##1 }
}
- \tl_put_right:Nx #1 { \box_use:c { l__ndu_cell_ \int_to_alph:n {##1} _box } }
+ \tl_put_right:Ne #1 { \box_use:c { l__ndu_cell_ \int_to_alph:n {##1} _box } }
% stop when we hit the desired unit depth
- \tl_if_eq:NnT \l_ndu_unit_depth_tl {##2}
+ \tl_if_eq:NnT \l__ndu_unit_depth_tl {##2}
{
\seq_map_break:
}
@@ -531,21 +554,19 @@
\cs_new_protected:Nn \ndu_set_options:nn
{
- % set keys for unit group
- \tl_if_eq:cnF { l_ndu_options_#1_tl } { \c_novalue_tl }
+ \tl_if_exist:cF { l__ndu_group_#1_seq }
{
- \keys_set:nv { non-decimal-units } { l_ndu_options_#1_tl }
+ \msg_error:nnn { non-decimal-units } { missing-unit-group } { #1 }
}
+ % set keys for unit group
+ \keys_set:nv { non-decimal-units } { l__ndu_options_#1_tl }
+
% set keys provided by command
- \IfValueT {#2} { \keys_set:nn { non-decimal-units } { #2 } }
+ \keys_set:nn { non-decimal-units } { #2 }
}
-\NewDocumentCommand \nduValue { m o m } {
- \tl_if_exist:cF { l_ndu_group_#1_seq }
- {
- \msg_error:nnn { non-decimal-units } { missing-unit-group } { #1 }
- }
+\NewDocumentCommand \nduValue { m O{} m } {
\group_begin:
\ndu_set_options:nn {#1} {#2}
@@ -554,7 +575,7 @@
\seq_set_split:Nnn \l_tmpa_seq { . } {#3}
% should we normalize the input?
- \bool_if:nT \l_ndu_normalize_bool
+ \bool_if:nT \l__ndu_normalize_bool
{
\ndu_seq_to_repr:NnN
\l__ndu_normalized_int
@@ -567,25 +588,70 @@
}
% are we outputting aligned cells?
- \bool_if:NTF \l_ndu_aligned_bool
+ \bool_if:NTF \l__ndu_aligned_bool
{
\ndu_format_values_aligned:NcN
\l__ndu_result_tl
- { l_ndu_group_#1_seq }
+ { l__ndu_group_#1_seq }
\l_tmpa_seq
}
{
\ndu_format_values:NcNN
\l__ndu_result_tl
- { l_ndu_group_#1_seq }
+ { l__ndu_group_#1_seq }
\l_tmpa_seq
- \l_ndu_unit_separator_tl
+ \l__ndu_unit_separator_tl
}
% should we do any math?
- \tl_if_empty:NF \l_ndu_current_variable_tl
+ \tl_if_empty:NF \l__ndu_current_variable_tl
{
- \nduMath{#1}[#2]{\l_ndu_current_variable_tl}{\l_ndu_current_operator_tl}{#3}
+ \nduMath{#1}[#2]{\l__ndu_current_variable_tl}{\l__ndu_current_operator_tl}{#3}
+ }
+
+ % output
+ \tl_use:N \l__ndu_result_tl
+ \group_end:
+}
+
+\NewDocumentCommand \nduNormalize { m O{} m m } {
+ \group_begin:
+ \ndu_set_options:nn {#1} {#2}
+
+ \tl_clear_new:N \l__ndu_result_tl
+
+ \int_zero_new:N \l__ndu_amount_int
+ \int_set:Nn \l__ndu_amount_int {#3}
+ \tl_clear_new:N \l__ndu_unit_tl
+ \tl_set:Nn \l__ndu_unit_tl {#4}
+
+ \ndu_repr_to_seq:NccNN
+ \l_tmpa_seq
+ { l__ndu_group_#1_seq }
+ { l__ndu_base_unit_#1_tl }
+ \l__ndu_amount_int
+ \l__ndu_unit_tl
+
+ % are we outputting aligned cells?
+ \bool_if:NTF \l__ndu_aligned_bool
+ {
+ \ndu_format_values_aligned:NcN
+ \l__ndu_result_tl
+ { l__ndu_group_#1_seq }
+ \l_tmpa_seq
+ }
+ {
+ \ndu_format_values:NcNN
+ \l__ndu_result_tl
+ { l__ndu_group_#1_seq }
+ \l_tmpa_seq
+ \l__ndu_unit_separator_tl
+ }
+
+ % should we do any math?
+ \tl_if_empty:NF \l__ndu_current_variable_tl
+ {
+ \nduMath{#1}[#2]{\l__ndu_current_variable_tl}{\l__ndu_current_operator_tl}{#3}
}
% output
@@ -596,16 +662,12 @@
\tl_const:Nn \c__ndu_plus_tl { + }
\tl_const:Nn \c__ndu_minus_tl { - }
-\NewDocumentCommand \nduMath { m o m m m } {
- \tl_if_exist:cF { l_ndu_group_#1_seq }
- {
- \msg_error:nnn { non-decimal-units } { missing-unit-group } { #1 }
- }
+\NewDocumentCommand \nduMath { m O{} m m m } {
\group_begin:
\ndu_set_options:nn {#1} {#2}
- \int_if_exist:cF { g_ndu_variable_#3_int } {
- \int_zero_new:c { g_ndu_variable_#3_int }
+ \int_if_exist:cF { g__ndu_variable_#3_int } {
+ \int_zero_new:c { g__ndu_variable_#3_int }
}
\int_zero_new:N \l__ndu_operand_int
@@ -631,24 +693,20 @@
\int_set:Nn \l__ndu_operand_int { #5 }
}
- \int_gset:cn { g_ndu_variable_#3_int } {
- \int_use:c { g_ndu_variable_#3_int }
+ \int_gset:cn { g__ndu_variable_#3_int } {
+ \int_use:c { g__ndu_variable_#3_int }
#4
\l__ndu_operand_int
}
\group_end:
}
-\NewDocumentCommand \nduResult { m o m } {
- \tl_if_exist:cF { l_ndu_group_#1_seq }
- {
- \msg_error:nnn { non-decimal-units } { missing-unit-group } { #1 }
- }
+\NewDocumentCommand \nduResult { m O{} m } {
\group_begin:
\ndu_set_options:nn {#1} {#2}
- \int_if_exist:cF { g_ndu_variable_#3_int } {
- \int_zero_new:c { g_ndu_variable_#3_int }
+ \int_if_exist:cF { g__ndu_variable_#3_int } {
+ \int_zero_new:c { g__ndu_variable_#3_int }
}
\tl_clear_new:N \l__ndu_result_tl
@@ -656,22 +714,22 @@
\ndu_repr_to_seq:Nnc
\l_tmpa_seq
{ #1 }
- { g_ndu_variable_#3_int }
+ { g__ndu_variable_#3_int }
% are we outputting aligned cells?
- \bool_if:NTF \l_ndu_aligned_bool
+ \bool_if:NTF \l__ndu_aligned_bool
{
\ndu_format_values_aligned:NcN
\l__ndu_result_tl
- { l_ndu_group_#1_seq }
+ { l__ndu_group_#1_seq }
\l_tmpa_seq
}
{
\ndu_format_values:NcNN
\l__ndu_result_tl
- { l_ndu_group_#1_seq }
+ { l__ndu_group_#1_seq }
\l_tmpa_seq
- \l_ndu_unit_separator_tl
+ \l__ndu_unit_separator_tl
}
% output
@@ -679,27 +737,23 @@
\group_end:
}
-\NewDocumentCommand \nduHeader { m o } {
- \tl_if_exist:cF { l_ndu_group_#1_seq }
- {
- \msg_error:nnn { non-decimal-units } { missing-unit-group } { #1 }
- }
+\NewDocumentCommand \nduHeader { m O{} } {
\group_begin:
\ndu_set_options:nn {#1} {#2}
\tl_set:Nn \l__ndu_result_tl { \leavevmode }
% build boxes
- \seq_set_eq:Nc \l_tmpa_seq { l_ndu_group_#1_seq }
+ \seq_set_eq:Nc \l_tmpa_seq { l__ndu_group_#1_seq }
\seq_map_indexed_inline:Nn \l_tmpa_seq
{
\ndu_make_box:cnn
{ l__ndu_cell_ \int_to_alph:n {##1} _box }
- { \hfill \tl_use:c { l_ndu_symbol_ ##2 _tl } }
+ { \hfill \tl_use:c { l__ndu_symbol_ ##2 _tl } }
{ ##1 }
\tl_put_right:Nn \l__ndu_result_tl { \box_use:c { l__ndu_cell_ \int_to_alph:n {##1} _box } }
% stop when we hit the desired unit depth
- \tl_if_eq:NnT \l_ndu_unit_depth_tl {##2}
+ \tl_if_eq:NnT \l__ndu_unit_depth_tl {##2}
{
\seq_map_break:
}
@@ -711,17 +765,13 @@
}
\NewDocumentCommand \nduSymbol { m } {
- \tl_use:c { l_ndu_symbol_#1_tl }
+ \tl_use:c { l__ndu_symbol_#1_tl }
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Column type for tabularray
%
-% TODO: using the font key is pretty hacky...
-% need to ask if theres a better option
-% https://github.com/lvjr/tabularray/issues
-
\keys_define:nn { non-decimal-units }
{
tabularray~column~type .code = {
@@ -750,7 +800,7 @@
\c__ndu_RESULT_tl {
\nduResult
{ #1 }
- { \l_ndu_current_variable_tl }
+ { \l__ndu_current_variable_tl }
}
\c__ndu_SKIP_tl {
% nothing
@@ -763,7 +813,7 @@
% elenganter solution?
\cs_if_exist:cTF { \ndu__tblr_cell_get_csname: }
% or use a specific bool, e.g., \ndu__do_math_bool
- { \tl_clear:N \l_ndu_current_variable_tl }
+ { \tl_clear:N \l__ndu_current_variable_tl }
{ \cs_gset:cpn { \ndu__tblr_cell_get_csname: } {} }
\nduValue
{ #1 }
@@ -790,9 +840,4 @@
german .code:n = { \input{non-decimal-units.german} } ,
}
-\IfFormatAtLeastTF { 2022-06-01 } {
- \ProcessKeyOptions [ non-decimal-units / options ]
-}{
- \RequirePackage { l3keys2e }
- \ProcessKeysOptions { non-decimal-units / options }
-} \ No newline at end of file
+\ProcessKeyOptions [ non-decimal-units / options ]