Sort a list of numbers in different representations with a macro in l3
up vote
1
down vote
favorite
I want to use a macro to sort numbers that have different representations. For example 3.5%, 0.321, 17/5, ...
If possible, the output of the sorted list should be in the same format again, or 17/5 should be in frac{17}{5} format.
Unfortunately, my knowledge is not enough, so I wanted to ask for help and tips.
With my previous attempt, the numbers can already be sorted. But not yet in the different formats.
Using xint, I can also get the representation: 17/5 -> frac{17}{5}, but then I can not sort the list anymore.
documentclass{article}
usepackage{siunitx,xfp,xintexpr}
sisetup{group-separator={,},output-decimal-marker={,}}
ExplSyntaxOn
NewDocumentCommand { Sortiere } { s m }
{
clist_set:Nn l_sort_clist { #2 }
clist_sort:Nn l_sort_clist
{
IfBooleanTF{#1}
{ fp_compare:nNnTF { fp_eval:n{ ##1 } } < { fp_eval:n{ ##2 } }
{ sort_return_swapped: }
{ sort_return_same: }
}
{ fp_compare:nNnTF { fp_eval:n{ ##1 } } > { fp_eval:n{ ##2 } }
{ sort_return_swapped: }
{ sort_return_same: }
}
}
% seq_new:N l__thomas_A_seq
% seq_clear:N l__thomas_A_seq
% clist_map_inline:nn { #2 }
% { seq_put_right:Nx l__thomas_A_seq
% { ensuremath { xintSignedFrac{xintIrr { fp_eval:n { ##1 } } } } }
% }
% seq_use:Nn l__thomas_A_seq {;,}
{l_sort_clist}
}
ExplSyntaxOff
begin{document}
Sortiere{1,12,9/5,7.12,-3,-5,2,7,11}
end{document}
macros latex3 xint
add a comment |
up vote
1
down vote
favorite
I want to use a macro to sort numbers that have different representations. For example 3.5%, 0.321, 17/5, ...
If possible, the output of the sorted list should be in the same format again, or 17/5 should be in frac{17}{5} format.
Unfortunately, my knowledge is not enough, so I wanted to ask for help and tips.
With my previous attempt, the numbers can already be sorted. But not yet in the different formats.
Using xint, I can also get the representation: 17/5 -> frac{17}{5}, but then I can not sort the list anymore.
documentclass{article}
usepackage{siunitx,xfp,xintexpr}
sisetup{group-separator={,},output-decimal-marker={,}}
ExplSyntaxOn
NewDocumentCommand { Sortiere } { s m }
{
clist_set:Nn l_sort_clist { #2 }
clist_sort:Nn l_sort_clist
{
IfBooleanTF{#1}
{ fp_compare:nNnTF { fp_eval:n{ ##1 } } < { fp_eval:n{ ##2 } }
{ sort_return_swapped: }
{ sort_return_same: }
}
{ fp_compare:nNnTF { fp_eval:n{ ##1 } } > { fp_eval:n{ ##2 } }
{ sort_return_swapped: }
{ sort_return_same: }
}
}
% seq_new:N l__thomas_A_seq
% seq_clear:N l__thomas_A_seq
% clist_map_inline:nn { #2 }
% { seq_put_right:Nx l__thomas_A_seq
% { ensuremath { xintSignedFrac{xintIrr { fp_eval:n { ##1 } } } } }
% }
% seq_use:Nn l__thomas_A_seq {;,}
{l_sort_clist}
}
ExplSyntaxOff
begin{document}
Sortiere{1,12,9/5,7.12,-3,-5,2,7,11}
end{document}
macros latex3 xint
What is the problem with the code you provided? The output is sorted correctly and the representation isn't changed.
– Skillmon
Dec 13 '17 at 14:49
@Skillmon there is no 0.231% in the input and in the output. And I want to have frac{a}{b} in the output instead of a/b.
– Thomas
Dec 13 '17 at 15:15
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I want to use a macro to sort numbers that have different representations. For example 3.5%, 0.321, 17/5, ...
If possible, the output of the sorted list should be in the same format again, or 17/5 should be in frac{17}{5} format.
Unfortunately, my knowledge is not enough, so I wanted to ask for help and tips.
With my previous attempt, the numbers can already be sorted. But not yet in the different formats.
Using xint, I can also get the representation: 17/5 -> frac{17}{5}, but then I can not sort the list anymore.
documentclass{article}
usepackage{siunitx,xfp,xintexpr}
sisetup{group-separator={,},output-decimal-marker={,}}
ExplSyntaxOn
NewDocumentCommand { Sortiere } { s m }
{
clist_set:Nn l_sort_clist { #2 }
clist_sort:Nn l_sort_clist
{
IfBooleanTF{#1}
{ fp_compare:nNnTF { fp_eval:n{ ##1 } } < { fp_eval:n{ ##2 } }
{ sort_return_swapped: }
{ sort_return_same: }
}
{ fp_compare:nNnTF { fp_eval:n{ ##1 } } > { fp_eval:n{ ##2 } }
{ sort_return_swapped: }
{ sort_return_same: }
}
}
% seq_new:N l__thomas_A_seq
% seq_clear:N l__thomas_A_seq
% clist_map_inline:nn { #2 }
% { seq_put_right:Nx l__thomas_A_seq
% { ensuremath { xintSignedFrac{xintIrr { fp_eval:n { ##1 } } } } }
% }
% seq_use:Nn l__thomas_A_seq {;,}
{l_sort_clist}
}
ExplSyntaxOff
begin{document}
Sortiere{1,12,9/5,7.12,-3,-5,2,7,11}
end{document}
macros latex3 xint
I want to use a macro to sort numbers that have different representations. For example 3.5%, 0.321, 17/5, ...
If possible, the output of the sorted list should be in the same format again, or 17/5 should be in frac{17}{5} format.
Unfortunately, my knowledge is not enough, so I wanted to ask for help and tips.
With my previous attempt, the numbers can already be sorted. But not yet in the different formats.
Using xint, I can also get the representation: 17/5 -> frac{17}{5}, but then I can not sort the list anymore.
documentclass{article}
usepackage{siunitx,xfp,xintexpr}
sisetup{group-separator={,},output-decimal-marker={,}}
ExplSyntaxOn
NewDocumentCommand { Sortiere } { s m }
{
clist_set:Nn l_sort_clist { #2 }
clist_sort:Nn l_sort_clist
{
IfBooleanTF{#1}
{ fp_compare:nNnTF { fp_eval:n{ ##1 } } < { fp_eval:n{ ##2 } }
{ sort_return_swapped: }
{ sort_return_same: }
}
{ fp_compare:nNnTF { fp_eval:n{ ##1 } } > { fp_eval:n{ ##2 } }
{ sort_return_swapped: }
{ sort_return_same: }
}
}
% seq_new:N l__thomas_A_seq
% seq_clear:N l__thomas_A_seq
% clist_map_inline:nn { #2 }
% { seq_put_right:Nx l__thomas_A_seq
% { ensuremath { xintSignedFrac{xintIrr { fp_eval:n { ##1 } } } } }
% }
% seq_use:Nn l__thomas_A_seq {;,}
{l_sort_clist}
}
ExplSyntaxOff
begin{document}
Sortiere{1,12,9/5,7.12,-3,-5,2,7,11}
end{document}
macros latex3 xint
macros latex3 xint
edited 2 days ago
Andrew
29.3k34178
29.3k34178
asked Dec 13 '17 at 14:12
Thomas
549212
549212
What is the problem with the code you provided? The output is sorted correctly and the representation isn't changed.
– Skillmon
Dec 13 '17 at 14:49
@Skillmon there is no 0.231% in the input and in the output. And I want to have frac{a}{b} in the output instead of a/b.
– Thomas
Dec 13 '17 at 15:15
add a comment |
What is the problem with the code you provided? The output is sorted correctly and the representation isn't changed.
– Skillmon
Dec 13 '17 at 14:49
@Skillmon there is no 0.231% in the input and in the output. And I want to have frac{a}{b} in the output instead of a/b.
– Thomas
Dec 13 '17 at 15:15
What is the problem with the code you provided? The output is sorted correctly and the representation isn't changed.
– Skillmon
Dec 13 '17 at 14:49
What is the problem with the code you provided? The output is sorted correctly and the representation isn't changed.
– Skillmon
Dec 13 '17 at 14:49
@Skillmon there is no 0.231% in the input and in the output. And I want to have frac{a}{b} in the output instead of a/b.
– Thomas
Dec 13 '17 at 15:15
@Skillmon there is no 0.231% in the input and in the output. And I want to have frac{a}{b} in the output instead of a/b.
– Thomas
Dec 13 '17 at 15:15
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
Imho you should separate the values and the representation. And you can use a simple integer sequence to do the sorting and then apply this sorting to other sequences (I used fruits and quantities as it is imho easier to understand if the sequences contains differents things):
documentclass{article}
usepackage{xparse}
begin{document}
ExplSyntaxOn
seq_new:N l_th_fruit_seq
seq_new:N l_th_fruit_sorted_seq
seq_set_from_clist:Nnl_th_fruit_seq {apples,pears,pineapples,bananas}
seq_new:N l_th_fruitqty_seq
seq_new:N l_th_fruitqty_sorted_seq
seq_set_from_clist:Nnl_th_fruitqty_seq {10,5,20,2}
int_zero:Nl_tmpa_int
seq_clear:Nl_tmpa_seq
seq_map_inline:Nnl_th_fruit_seq % a temporary seq with 1,2,3,4 for the sorting
{
int_incr:Nl_tmpa_int
seq_put_right:NVl_tmpa_seq {l_tmpa_int}
}
seq_sort:Nn l_tmpa_seq %sort over the quantity
{
int_compare:nNnTF { seq_item:Nnl_th_fruitqty_seq {#1} } > { seq_item:Nnl_th_fruitqty_seq {#2} }
{ sort_return_swapped: }
{ sort_return_same: }
}
seq_map_inline:Nn l_tmpa_seq %apply the sorting to the seqs:
{
seq_put_right:Nx l_th_fruit_sorted_seq { seq_item:Nn l_th_fruit_seq{#1} }
seq_put_right:Nx l_th_fruitqty_sorted_seq { seq_item:Nn l_th_fruitqty_seq{#1}}
}
Sorted~fruits:~seq_use:Nnl_th_fruit_sorted_seq {,~}
par
Quantities:~seq_use:Nn l_th_fruitqty_sorted_seq {,~}
ExplSyntaxOff
end{document}
-@UlrikeFischer very nice, thank you
– Thomas
Dec 13 '17 at 19:18
-@Ulrike Fischer, I think your idea is great, because it is very general. However, I have now found a solution that works with some replacements in the list.
– Thomas
Dec 15 '17 at 5:58
add a comment |
up vote
1
down vote
This is what I tried last.
documentclass{article}
usepackage[T1]{fontenc}
usepackage[utf8]{inputenc}
usepackage[ngerman]{babel}
usepackage{siunitx,xfp,xintexpr}
sisetup{group-separator={,},output-decimal-marker={,}}
ExplSyntaxOn
NewDocumentCommand{ Sortiere }{ O{} t> t! m }
{
group_begin:
keys_set:nn { thomas/Sortiere } { #1 }
clist_set:Nn l_T_numberlist_clist { #4 }
clist_set:Nn l_T_number_unsort_clist { #4 }
tl_replace_all:Nnn l_T_number_unsort_clist { % } { /100 }
regex_replace_all:nnN { c{ln} } { ln } l_T_number_unsort_clist
bool_if:NTF l__T_Sortiere_unsort_bool
{
clist_clear:N l_T_printnumbers_unsort_clist
clist_map_inline:Nn l_T_number_unsort_clist
{ clist_put_right:Nn l_T_printnumbers_unsort_clist { ensuremath { ##1 } } }
ensuremath {
regex_replace_all:nnN { /100 } { c{num}cB{ 1cE}c{,}c{%} } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { ln } { c{ln} } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { [+-]?d+/[+-]?d+ } { c{xintSignedFrac} cB{ cE} } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { [+-]?d+e[+-]?d+ } { c{num} cB{ cE} } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { . } { , } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { (sqrt)({[+-]?d+}) } { c{sqrt} cB{ 2cE} } l_T_printnumbers_unsort_clist
left{ clist_use:Nnnn l_T_printnumbers_unsort_clist { ;, }{ ;, }{ ;, } right}
}
}
{
tl_replace_all:Nnn l_T_numberlist_clist { % } { /100 }
regex_replace_all:nnN { c{ln} } { ln } l_T_numberlist_clist
clist_sort:Nn l_T_numberlist_clist
{
IfBooleanTF{#2}
{ fp_compare:nTF { ##1 < ##2 } }
{ fp_compare:nTF { ##1 > ##2 } }
{ sort_return_swapped: }
{ sort_return_same: }
}
clist_clear:N l_T_printnumbers_clist
clist_map_inline:Nn l_T_numberlist_clist
{ IfBooleanTF{#3}
{ clist_put_right:Nn l_T_printnumbers_clist { ensuremath { ##1 } } }
{ clist_put_right:Nn l_T_printnumbers_clist { ensuremath { xintSignedFrac{ xintIrr { xinttheexpr round ( fp_eval:n { ##1 } ,l__thomas_sortiere_round_int ) relax } } } } }
}
IfBooleanTF{#3}{
regex_replace_all:nnN { /100 } { c{num}cB{ 1cE}c{,}c{%} } l_T_printnumbers_clist
regex_replace_all:nnN { ln } { c{ln} } l_T_printnumbers_clist
regex_replace_all:nnN { [+-]?d+/[+-]?d+ } { c{xintSignedFrac} cB{ cE} } l_T_printnumbers_clist
regex_replace_all:nnN { [+-]?d+e[+-]?d+ } { c{num} cB{ cE} } l_T_printnumbers_clist
regex_replace_all:nnN { . } { , } l_T_printnumbers_clist
regex_replace_all:nnN { (sqrt)({[+-]?d+}) } { c{sqrt} cB{ 2cE} } l_T_printnumbers_clist
}
{ }
IfBooleanTF{#2}
{ clist_use:Nnnn l_T_printnumbers_clist { ,ensuremath { geq }, }{ ,ensuremath { geq }, }{ ,ensuremath { geq }, } }
{ clist_use:Nnnn l_T_printnumbers_clist { ,ensuremath { leq }, }{ ,ensuremath { leq }, }{ ,ensuremath { leq }, } }
}
group_end:
}
clist_new:N l_T_printnumbers_clist
clist_new:N l_T_numberlist_clist
clist_new:N l_T_number_unsort_clist
clist_new:N l_T_printnumbers_unsort_clist
keys_define:nn { thomas/Sortiere }
{
unsort .bool_set:N = l__T_Sortiere_unsort_bool,
unsort .initial:n = false, % Starteinstellung
unsort .default:n = true, %
round .int_set:N = l__thomas_sortiere_round_int,
round .initial:n = 4,
}
ExplSyntaxOff
sisetup{exponent-product=cdot}
begin{document}
[ Sortiere[unsort]>{2^4, 4^2, 3^2, 5^{0.5}, 13, 25-1, ln(17)} ]
[ Sortiere>{2^4, 4^2, 3^2, 5^{0.5}, 13, 25-1, ln(17)} ]
[ Sortiere[round=12]>{2^4, 4^2, 3^2, 5^{0.5}, 13, 25-1, ln(17)} ]
[ Sortiere>!{2^4, 4^2, 3^2, 5^{0.5}, 13, 25-1, ln(17)} ]
[ Sortiere{1.5%,1,12,9/5,7.12,-3e1,-5,2,sqrt{8}} ]
[ Sortiere[unsort]!{1.5%,1,12,-11/5,7.12,-3e1,-5,2,sqrt{8}} ]
end{document}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Imho you should separate the values and the representation. And you can use a simple integer sequence to do the sorting and then apply this sorting to other sequences (I used fruits and quantities as it is imho easier to understand if the sequences contains differents things):
documentclass{article}
usepackage{xparse}
begin{document}
ExplSyntaxOn
seq_new:N l_th_fruit_seq
seq_new:N l_th_fruit_sorted_seq
seq_set_from_clist:Nnl_th_fruit_seq {apples,pears,pineapples,bananas}
seq_new:N l_th_fruitqty_seq
seq_new:N l_th_fruitqty_sorted_seq
seq_set_from_clist:Nnl_th_fruitqty_seq {10,5,20,2}
int_zero:Nl_tmpa_int
seq_clear:Nl_tmpa_seq
seq_map_inline:Nnl_th_fruit_seq % a temporary seq with 1,2,3,4 for the sorting
{
int_incr:Nl_tmpa_int
seq_put_right:NVl_tmpa_seq {l_tmpa_int}
}
seq_sort:Nn l_tmpa_seq %sort over the quantity
{
int_compare:nNnTF { seq_item:Nnl_th_fruitqty_seq {#1} } > { seq_item:Nnl_th_fruitqty_seq {#2} }
{ sort_return_swapped: }
{ sort_return_same: }
}
seq_map_inline:Nn l_tmpa_seq %apply the sorting to the seqs:
{
seq_put_right:Nx l_th_fruit_sorted_seq { seq_item:Nn l_th_fruit_seq{#1} }
seq_put_right:Nx l_th_fruitqty_sorted_seq { seq_item:Nn l_th_fruitqty_seq{#1}}
}
Sorted~fruits:~seq_use:Nnl_th_fruit_sorted_seq {,~}
par
Quantities:~seq_use:Nn l_th_fruitqty_sorted_seq {,~}
ExplSyntaxOff
end{document}
-@UlrikeFischer very nice, thank you
– Thomas
Dec 13 '17 at 19:18
-@Ulrike Fischer, I think your idea is great, because it is very general. However, I have now found a solution that works with some replacements in the list.
– Thomas
Dec 15 '17 at 5:58
add a comment |
up vote
3
down vote
accepted
Imho you should separate the values and the representation. And you can use a simple integer sequence to do the sorting and then apply this sorting to other sequences (I used fruits and quantities as it is imho easier to understand if the sequences contains differents things):
documentclass{article}
usepackage{xparse}
begin{document}
ExplSyntaxOn
seq_new:N l_th_fruit_seq
seq_new:N l_th_fruit_sorted_seq
seq_set_from_clist:Nnl_th_fruit_seq {apples,pears,pineapples,bananas}
seq_new:N l_th_fruitqty_seq
seq_new:N l_th_fruitqty_sorted_seq
seq_set_from_clist:Nnl_th_fruitqty_seq {10,5,20,2}
int_zero:Nl_tmpa_int
seq_clear:Nl_tmpa_seq
seq_map_inline:Nnl_th_fruit_seq % a temporary seq with 1,2,3,4 for the sorting
{
int_incr:Nl_tmpa_int
seq_put_right:NVl_tmpa_seq {l_tmpa_int}
}
seq_sort:Nn l_tmpa_seq %sort over the quantity
{
int_compare:nNnTF { seq_item:Nnl_th_fruitqty_seq {#1} } > { seq_item:Nnl_th_fruitqty_seq {#2} }
{ sort_return_swapped: }
{ sort_return_same: }
}
seq_map_inline:Nn l_tmpa_seq %apply the sorting to the seqs:
{
seq_put_right:Nx l_th_fruit_sorted_seq { seq_item:Nn l_th_fruit_seq{#1} }
seq_put_right:Nx l_th_fruitqty_sorted_seq { seq_item:Nn l_th_fruitqty_seq{#1}}
}
Sorted~fruits:~seq_use:Nnl_th_fruit_sorted_seq {,~}
par
Quantities:~seq_use:Nn l_th_fruitqty_sorted_seq {,~}
ExplSyntaxOff
end{document}
-@UlrikeFischer very nice, thank you
– Thomas
Dec 13 '17 at 19:18
-@Ulrike Fischer, I think your idea is great, because it is very general. However, I have now found a solution that works with some replacements in the list.
– Thomas
Dec 15 '17 at 5:58
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Imho you should separate the values and the representation. And you can use a simple integer sequence to do the sorting and then apply this sorting to other sequences (I used fruits and quantities as it is imho easier to understand if the sequences contains differents things):
documentclass{article}
usepackage{xparse}
begin{document}
ExplSyntaxOn
seq_new:N l_th_fruit_seq
seq_new:N l_th_fruit_sorted_seq
seq_set_from_clist:Nnl_th_fruit_seq {apples,pears,pineapples,bananas}
seq_new:N l_th_fruitqty_seq
seq_new:N l_th_fruitqty_sorted_seq
seq_set_from_clist:Nnl_th_fruitqty_seq {10,5,20,2}
int_zero:Nl_tmpa_int
seq_clear:Nl_tmpa_seq
seq_map_inline:Nnl_th_fruit_seq % a temporary seq with 1,2,3,4 for the sorting
{
int_incr:Nl_tmpa_int
seq_put_right:NVl_tmpa_seq {l_tmpa_int}
}
seq_sort:Nn l_tmpa_seq %sort over the quantity
{
int_compare:nNnTF { seq_item:Nnl_th_fruitqty_seq {#1} } > { seq_item:Nnl_th_fruitqty_seq {#2} }
{ sort_return_swapped: }
{ sort_return_same: }
}
seq_map_inline:Nn l_tmpa_seq %apply the sorting to the seqs:
{
seq_put_right:Nx l_th_fruit_sorted_seq { seq_item:Nn l_th_fruit_seq{#1} }
seq_put_right:Nx l_th_fruitqty_sorted_seq { seq_item:Nn l_th_fruitqty_seq{#1}}
}
Sorted~fruits:~seq_use:Nnl_th_fruit_sorted_seq {,~}
par
Quantities:~seq_use:Nn l_th_fruitqty_sorted_seq {,~}
ExplSyntaxOff
end{document}
Imho you should separate the values and the representation. And you can use a simple integer sequence to do the sorting and then apply this sorting to other sequences (I used fruits and quantities as it is imho easier to understand if the sequences contains differents things):
documentclass{article}
usepackage{xparse}
begin{document}
ExplSyntaxOn
seq_new:N l_th_fruit_seq
seq_new:N l_th_fruit_sorted_seq
seq_set_from_clist:Nnl_th_fruit_seq {apples,pears,pineapples,bananas}
seq_new:N l_th_fruitqty_seq
seq_new:N l_th_fruitqty_sorted_seq
seq_set_from_clist:Nnl_th_fruitqty_seq {10,5,20,2}
int_zero:Nl_tmpa_int
seq_clear:Nl_tmpa_seq
seq_map_inline:Nnl_th_fruit_seq % a temporary seq with 1,2,3,4 for the sorting
{
int_incr:Nl_tmpa_int
seq_put_right:NVl_tmpa_seq {l_tmpa_int}
}
seq_sort:Nn l_tmpa_seq %sort over the quantity
{
int_compare:nNnTF { seq_item:Nnl_th_fruitqty_seq {#1} } > { seq_item:Nnl_th_fruitqty_seq {#2} }
{ sort_return_swapped: }
{ sort_return_same: }
}
seq_map_inline:Nn l_tmpa_seq %apply the sorting to the seqs:
{
seq_put_right:Nx l_th_fruit_sorted_seq { seq_item:Nn l_th_fruit_seq{#1} }
seq_put_right:Nx l_th_fruitqty_sorted_seq { seq_item:Nn l_th_fruitqty_seq{#1}}
}
Sorted~fruits:~seq_use:Nnl_th_fruit_sorted_seq {,~}
par
Quantities:~seq_use:Nn l_th_fruitqty_sorted_seq {,~}
ExplSyntaxOff
end{document}
answered Dec 13 '17 at 18:24
Ulrike Fischer
182k7288661
182k7288661
-@UlrikeFischer very nice, thank you
– Thomas
Dec 13 '17 at 19:18
-@Ulrike Fischer, I think your idea is great, because it is very general. However, I have now found a solution that works with some replacements in the list.
– Thomas
Dec 15 '17 at 5:58
add a comment |
-@UlrikeFischer very nice, thank you
– Thomas
Dec 13 '17 at 19:18
-@Ulrike Fischer, I think your idea is great, because it is very general. However, I have now found a solution that works with some replacements in the list.
– Thomas
Dec 15 '17 at 5:58
-@UlrikeFischer very nice, thank you
– Thomas
Dec 13 '17 at 19:18
-@UlrikeFischer very nice, thank you
– Thomas
Dec 13 '17 at 19:18
-@Ulrike Fischer, I think your idea is great, because it is very general. However, I have now found a solution that works with some replacements in the list.
– Thomas
Dec 15 '17 at 5:58
-@Ulrike Fischer, I think your idea is great, because it is very general. However, I have now found a solution that works with some replacements in the list.
– Thomas
Dec 15 '17 at 5:58
add a comment |
up vote
1
down vote
This is what I tried last.
documentclass{article}
usepackage[T1]{fontenc}
usepackage[utf8]{inputenc}
usepackage[ngerman]{babel}
usepackage{siunitx,xfp,xintexpr}
sisetup{group-separator={,},output-decimal-marker={,}}
ExplSyntaxOn
NewDocumentCommand{ Sortiere }{ O{} t> t! m }
{
group_begin:
keys_set:nn { thomas/Sortiere } { #1 }
clist_set:Nn l_T_numberlist_clist { #4 }
clist_set:Nn l_T_number_unsort_clist { #4 }
tl_replace_all:Nnn l_T_number_unsort_clist { % } { /100 }
regex_replace_all:nnN { c{ln} } { ln } l_T_number_unsort_clist
bool_if:NTF l__T_Sortiere_unsort_bool
{
clist_clear:N l_T_printnumbers_unsort_clist
clist_map_inline:Nn l_T_number_unsort_clist
{ clist_put_right:Nn l_T_printnumbers_unsort_clist { ensuremath { ##1 } } }
ensuremath {
regex_replace_all:nnN { /100 } { c{num}cB{ 1cE}c{,}c{%} } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { ln } { c{ln} } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { [+-]?d+/[+-]?d+ } { c{xintSignedFrac} cB{ cE} } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { [+-]?d+e[+-]?d+ } { c{num} cB{ cE} } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { . } { , } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { (sqrt)({[+-]?d+}) } { c{sqrt} cB{ 2cE} } l_T_printnumbers_unsort_clist
left{ clist_use:Nnnn l_T_printnumbers_unsort_clist { ;, }{ ;, }{ ;, } right}
}
}
{
tl_replace_all:Nnn l_T_numberlist_clist { % } { /100 }
regex_replace_all:nnN { c{ln} } { ln } l_T_numberlist_clist
clist_sort:Nn l_T_numberlist_clist
{
IfBooleanTF{#2}
{ fp_compare:nTF { ##1 < ##2 } }
{ fp_compare:nTF { ##1 > ##2 } }
{ sort_return_swapped: }
{ sort_return_same: }
}
clist_clear:N l_T_printnumbers_clist
clist_map_inline:Nn l_T_numberlist_clist
{ IfBooleanTF{#3}
{ clist_put_right:Nn l_T_printnumbers_clist { ensuremath { ##1 } } }
{ clist_put_right:Nn l_T_printnumbers_clist { ensuremath { xintSignedFrac{ xintIrr { xinttheexpr round ( fp_eval:n { ##1 } ,l__thomas_sortiere_round_int ) relax } } } } }
}
IfBooleanTF{#3}{
regex_replace_all:nnN { /100 } { c{num}cB{ 1cE}c{,}c{%} } l_T_printnumbers_clist
regex_replace_all:nnN { ln } { c{ln} } l_T_printnumbers_clist
regex_replace_all:nnN { [+-]?d+/[+-]?d+ } { c{xintSignedFrac} cB{ cE} } l_T_printnumbers_clist
regex_replace_all:nnN { [+-]?d+e[+-]?d+ } { c{num} cB{ cE} } l_T_printnumbers_clist
regex_replace_all:nnN { . } { , } l_T_printnumbers_clist
regex_replace_all:nnN { (sqrt)({[+-]?d+}) } { c{sqrt} cB{ 2cE} } l_T_printnumbers_clist
}
{ }
IfBooleanTF{#2}
{ clist_use:Nnnn l_T_printnumbers_clist { ,ensuremath { geq }, }{ ,ensuremath { geq }, }{ ,ensuremath { geq }, } }
{ clist_use:Nnnn l_T_printnumbers_clist { ,ensuremath { leq }, }{ ,ensuremath { leq }, }{ ,ensuremath { leq }, } }
}
group_end:
}
clist_new:N l_T_printnumbers_clist
clist_new:N l_T_numberlist_clist
clist_new:N l_T_number_unsort_clist
clist_new:N l_T_printnumbers_unsort_clist
keys_define:nn { thomas/Sortiere }
{
unsort .bool_set:N = l__T_Sortiere_unsort_bool,
unsort .initial:n = false, % Starteinstellung
unsort .default:n = true, %
round .int_set:N = l__thomas_sortiere_round_int,
round .initial:n = 4,
}
ExplSyntaxOff
sisetup{exponent-product=cdot}
begin{document}
[ Sortiere[unsort]>{2^4, 4^2, 3^2, 5^{0.5}, 13, 25-1, ln(17)} ]
[ Sortiere>{2^4, 4^2, 3^2, 5^{0.5}, 13, 25-1, ln(17)} ]
[ Sortiere[round=12]>{2^4, 4^2, 3^2, 5^{0.5}, 13, 25-1, ln(17)} ]
[ Sortiere>!{2^4, 4^2, 3^2, 5^{0.5}, 13, 25-1, ln(17)} ]
[ Sortiere{1.5%,1,12,9/5,7.12,-3e1,-5,2,sqrt{8}} ]
[ Sortiere[unsort]!{1.5%,1,12,-11/5,7.12,-3e1,-5,2,sqrt{8}} ]
end{document}
add a comment |
up vote
1
down vote
This is what I tried last.
documentclass{article}
usepackage[T1]{fontenc}
usepackage[utf8]{inputenc}
usepackage[ngerman]{babel}
usepackage{siunitx,xfp,xintexpr}
sisetup{group-separator={,},output-decimal-marker={,}}
ExplSyntaxOn
NewDocumentCommand{ Sortiere }{ O{} t> t! m }
{
group_begin:
keys_set:nn { thomas/Sortiere } { #1 }
clist_set:Nn l_T_numberlist_clist { #4 }
clist_set:Nn l_T_number_unsort_clist { #4 }
tl_replace_all:Nnn l_T_number_unsort_clist { % } { /100 }
regex_replace_all:nnN { c{ln} } { ln } l_T_number_unsort_clist
bool_if:NTF l__T_Sortiere_unsort_bool
{
clist_clear:N l_T_printnumbers_unsort_clist
clist_map_inline:Nn l_T_number_unsort_clist
{ clist_put_right:Nn l_T_printnumbers_unsort_clist { ensuremath { ##1 } } }
ensuremath {
regex_replace_all:nnN { /100 } { c{num}cB{ 1cE}c{,}c{%} } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { ln } { c{ln} } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { [+-]?d+/[+-]?d+ } { c{xintSignedFrac} cB{ cE} } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { [+-]?d+e[+-]?d+ } { c{num} cB{ cE} } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { . } { , } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { (sqrt)({[+-]?d+}) } { c{sqrt} cB{ 2cE} } l_T_printnumbers_unsort_clist
left{ clist_use:Nnnn l_T_printnumbers_unsort_clist { ;, }{ ;, }{ ;, } right}
}
}
{
tl_replace_all:Nnn l_T_numberlist_clist { % } { /100 }
regex_replace_all:nnN { c{ln} } { ln } l_T_numberlist_clist
clist_sort:Nn l_T_numberlist_clist
{
IfBooleanTF{#2}
{ fp_compare:nTF { ##1 < ##2 } }
{ fp_compare:nTF { ##1 > ##2 } }
{ sort_return_swapped: }
{ sort_return_same: }
}
clist_clear:N l_T_printnumbers_clist
clist_map_inline:Nn l_T_numberlist_clist
{ IfBooleanTF{#3}
{ clist_put_right:Nn l_T_printnumbers_clist { ensuremath { ##1 } } }
{ clist_put_right:Nn l_T_printnumbers_clist { ensuremath { xintSignedFrac{ xintIrr { xinttheexpr round ( fp_eval:n { ##1 } ,l__thomas_sortiere_round_int ) relax } } } } }
}
IfBooleanTF{#3}{
regex_replace_all:nnN { /100 } { c{num}cB{ 1cE}c{,}c{%} } l_T_printnumbers_clist
regex_replace_all:nnN { ln } { c{ln} } l_T_printnumbers_clist
regex_replace_all:nnN { [+-]?d+/[+-]?d+ } { c{xintSignedFrac} cB{ cE} } l_T_printnumbers_clist
regex_replace_all:nnN { [+-]?d+e[+-]?d+ } { c{num} cB{ cE} } l_T_printnumbers_clist
regex_replace_all:nnN { . } { , } l_T_printnumbers_clist
regex_replace_all:nnN { (sqrt)({[+-]?d+}) } { c{sqrt} cB{ 2cE} } l_T_printnumbers_clist
}
{ }
IfBooleanTF{#2}
{ clist_use:Nnnn l_T_printnumbers_clist { ,ensuremath { geq }, }{ ,ensuremath { geq }, }{ ,ensuremath { geq }, } }
{ clist_use:Nnnn l_T_printnumbers_clist { ,ensuremath { leq }, }{ ,ensuremath { leq }, }{ ,ensuremath { leq }, } }
}
group_end:
}
clist_new:N l_T_printnumbers_clist
clist_new:N l_T_numberlist_clist
clist_new:N l_T_number_unsort_clist
clist_new:N l_T_printnumbers_unsort_clist
keys_define:nn { thomas/Sortiere }
{
unsort .bool_set:N = l__T_Sortiere_unsort_bool,
unsort .initial:n = false, % Starteinstellung
unsort .default:n = true, %
round .int_set:N = l__thomas_sortiere_round_int,
round .initial:n = 4,
}
ExplSyntaxOff
sisetup{exponent-product=cdot}
begin{document}
[ Sortiere[unsort]>{2^4, 4^2, 3^2, 5^{0.5}, 13, 25-1, ln(17)} ]
[ Sortiere>{2^4, 4^2, 3^2, 5^{0.5}, 13, 25-1, ln(17)} ]
[ Sortiere[round=12]>{2^4, 4^2, 3^2, 5^{0.5}, 13, 25-1, ln(17)} ]
[ Sortiere>!{2^4, 4^2, 3^2, 5^{0.5}, 13, 25-1, ln(17)} ]
[ Sortiere{1.5%,1,12,9/5,7.12,-3e1,-5,2,sqrt{8}} ]
[ Sortiere[unsort]!{1.5%,1,12,-11/5,7.12,-3e1,-5,2,sqrt{8}} ]
end{document}
add a comment |
up vote
1
down vote
up vote
1
down vote
This is what I tried last.
documentclass{article}
usepackage[T1]{fontenc}
usepackage[utf8]{inputenc}
usepackage[ngerman]{babel}
usepackage{siunitx,xfp,xintexpr}
sisetup{group-separator={,},output-decimal-marker={,}}
ExplSyntaxOn
NewDocumentCommand{ Sortiere }{ O{} t> t! m }
{
group_begin:
keys_set:nn { thomas/Sortiere } { #1 }
clist_set:Nn l_T_numberlist_clist { #4 }
clist_set:Nn l_T_number_unsort_clist { #4 }
tl_replace_all:Nnn l_T_number_unsort_clist { % } { /100 }
regex_replace_all:nnN { c{ln} } { ln } l_T_number_unsort_clist
bool_if:NTF l__T_Sortiere_unsort_bool
{
clist_clear:N l_T_printnumbers_unsort_clist
clist_map_inline:Nn l_T_number_unsort_clist
{ clist_put_right:Nn l_T_printnumbers_unsort_clist { ensuremath { ##1 } } }
ensuremath {
regex_replace_all:nnN { /100 } { c{num}cB{ 1cE}c{,}c{%} } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { ln } { c{ln} } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { [+-]?d+/[+-]?d+ } { c{xintSignedFrac} cB{ cE} } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { [+-]?d+e[+-]?d+ } { c{num} cB{ cE} } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { . } { , } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { (sqrt)({[+-]?d+}) } { c{sqrt} cB{ 2cE} } l_T_printnumbers_unsort_clist
left{ clist_use:Nnnn l_T_printnumbers_unsort_clist { ;, }{ ;, }{ ;, } right}
}
}
{
tl_replace_all:Nnn l_T_numberlist_clist { % } { /100 }
regex_replace_all:nnN { c{ln} } { ln } l_T_numberlist_clist
clist_sort:Nn l_T_numberlist_clist
{
IfBooleanTF{#2}
{ fp_compare:nTF { ##1 < ##2 } }
{ fp_compare:nTF { ##1 > ##2 } }
{ sort_return_swapped: }
{ sort_return_same: }
}
clist_clear:N l_T_printnumbers_clist
clist_map_inline:Nn l_T_numberlist_clist
{ IfBooleanTF{#3}
{ clist_put_right:Nn l_T_printnumbers_clist { ensuremath { ##1 } } }
{ clist_put_right:Nn l_T_printnumbers_clist { ensuremath { xintSignedFrac{ xintIrr { xinttheexpr round ( fp_eval:n { ##1 } ,l__thomas_sortiere_round_int ) relax } } } } }
}
IfBooleanTF{#3}{
regex_replace_all:nnN { /100 } { c{num}cB{ 1cE}c{,}c{%} } l_T_printnumbers_clist
regex_replace_all:nnN { ln } { c{ln} } l_T_printnumbers_clist
regex_replace_all:nnN { [+-]?d+/[+-]?d+ } { c{xintSignedFrac} cB{ cE} } l_T_printnumbers_clist
regex_replace_all:nnN { [+-]?d+e[+-]?d+ } { c{num} cB{ cE} } l_T_printnumbers_clist
regex_replace_all:nnN { . } { , } l_T_printnumbers_clist
regex_replace_all:nnN { (sqrt)({[+-]?d+}) } { c{sqrt} cB{ 2cE} } l_T_printnumbers_clist
}
{ }
IfBooleanTF{#2}
{ clist_use:Nnnn l_T_printnumbers_clist { ,ensuremath { geq }, }{ ,ensuremath { geq }, }{ ,ensuremath { geq }, } }
{ clist_use:Nnnn l_T_printnumbers_clist { ,ensuremath { leq }, }{ ,ensuremath { leq }, }{ ,ensuremath { leq }, } }
}
group_end:
}
clist_new:N l_T_printnumbers_clist
clist_new:N l_T_numberlist_clist
clist_new:N l_T_number_unsort_clist
clist_new:N l_T_printnumbers_unsort_clist
keys_define:nn { thomas/Sortiere }
{
unsort .bool_set:N = l__T_Sortiere_unsort_bool,
unsort .initial:n = false, % Starteinstellung
unsort .default:n = true, %
round .int_set:N = l__thomas_sortiere_round_int,
round .initial:n = 4,
}
ExplSyntaxOff
sisetup{exponent-product=cdot}
begin{document}
[ Sortiere[unsort]>{2^4, 4^2, 3^2, 5^{0.5}, 13, 25-1, ln(17)} ]
[ Sortiere>{2^4, 4^2, 3^2, 5^{0.5}, 13, 25-1, ln(17)} ]
[ Sortiere[round=12]>{2^4, 4^2, 3^2, 5^{0.5}, 13, 25-1, ln(17)} ]
[ Sortiere>!{2^4, 4^2, 3^2, 5^{0.5}, 13, 25-1, ln(17)} ]
[ Sortiere{1.5%,1,12,9/5,7.12,-3e1,-5,2,sqrt{8}} ]
[ Sortiere[unsort]!{1.5%,1,12,-11/5,7.12,-3e1,-5,2,sqrt{8}} ]
end{document}
This is what I tried last.
documentclass{article}
usepackage[T1]{fontenc}
usepackage[utf8]{inputenc}
usepackage[ngerman]{babel}
usepackage{siunitx,xfp,xintexpr}
sisetup{group-separator={,},output-decimal-marker={,}}
ExplSyntaxOn
NewDocumentCommand{ Sortiere }{ O{} t> t! m }
{
group_begin:
keys_set:nn { thomas/Sortiere } { #1 }
clist_set:Nn l_T_numberlist_clist { #4 }
clist_set:Nn l_T_number_unsort_clist { #4 }
tl_replace_all:Nnn l_T_number_unsort_clist { % } { /100 }
regex_replace_all:nnN { c{ln} } { ln } l_T_number_unsort_clist
bool_if:NTF l__T_Sortiere_unsort_bool
{
clist_clear:N l_T_printnumbers_unsort_clist
clist_map_inline:Nn l_T_number_unsort_clist
{ clist_put_right:Nn l_T_printnumbers_unsort_clist { ensuremath { ##1 } } }
ensuremath {
regex_replace_all:nnN { /100 } { c{num}cB{ 1cE}c{,}c{%} } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { ln } { c{ln} } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { [+-]?d+/[+-]?d+ } { c{xintSignedFrac} cB{ cE} } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { [+-]?d+e[+-]?d+ } { c{num} cB{ cE} } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { . } { , } l_T_printnumbers_unsort_clist
regex_replace_all:nnN { (sqrt)({[+-]?d+}) } { c{sqrt} cB{ 2cE} } l_T_printnumbers_unsort_clist
left{ clist_use:Nnnn l_T_printnumbers_unsort_clist { ;, }{ ;, }{ ;, } right}
}
}
{
tl_replace_all:Nnn l_T_numberlist_clist { % } { /100 }
regex_replace_all:nnN { c{ln} } { ln } l_T_numberlist_clist
clist_sort:Nn l_T_numberlist_clist
{
IfBooleanTF{#2}
{ fp_compare:nTF { ##1 < ##2 } }
{ fp_compare:nTF { ##1 > ##2 } }
{ sort_return_swapped: }
{ sort_return_same: }
}
clist_clear:N l_T_printnumbers_clist
clist_map_inline:Nn l_T_numberlist_clist
{ IfBooleanTF{#3}
{ clist_put_right:Nn l_T_printnumbers_clist { ensuremath { ##1 } } }
{ clist_put_right:Nn l_T_printnumbers_clist { ensuremath { xintSignedFrac{ xintIrr { xinttheexpr round ( fp_eval:n { ##1 } ,l__thomas_sortiere_round_int ) relax } } } } }
}
IfBooleanTF{#3}{
regex_replace_all:nnN { /100 } { c{num}cB{ 1cE}c{,}c{%} } l_T_printnumbers_clist
regex_replace_all:nnN { ln } { c{ln} } l_T_printnumbers_clist
regex_replace_all:nnN { [+-]?d+/[+-]?d+ } { c{xintSignedFrac} cB{ cE} } l_T_printnumbers_clist
regex_replace_all:nnN { [+-]?d+e[+-]?d+ } { c{num} cB{ cE} } l_T_printnumbers_clist
regex_replace_all:nnN { . } { , } l_T_printnumbers_clist
regex_replace_all:nnN { (sqrt)({[+-]?d+}) } { c{sqrt} cB{ 2cE} } l_T_printnumbers_clist
}
{ }
IfBooleanTF{#2}
{ clist_use:Nnnn l_T_printnumbers_clist { ,ensuremath { geq }, }{ ,ensuremath { geq }, }{ ,ensuremath { geq }, } }
{ clist_use:Nnnn l_T_printnumbers_clist { ,ensuremath { leq }, }{ ,ensuremath { leq }, }{ ,ensuremath { leq }, } }
}
group_end:
}
clist_new:N l_T_printnumbers_clist
clist_new:N l_T_numberlist_clist
clist_new:N l_T_number_unsort_clist
clist_new:N l_T_printnumbers_unsort_clist
keys_define:nn { thomas/Sortiere }
{
unsort .bool_set:N = l__T_Sortiere_unsort_bool,
unsort .initial:n = false, % Starteinstellung
unsort .default:n = true, %
round .int_set:N = l__thomas_sortiere_round_int,
round .initial:n = 4,
}
ExplSyntaxOff
sisetup{exponent-product=cdot}
begin{document}
[ Sortiere[unsort]>{2^4, 4^2, 3^2, 5^{0.5}, 13, 25-1, ln(17)} ]
[ Sortiere>{2^4, 4^2, 3^2, 5^{0.5}, 13, 25-1, ln(17)} ]
[ Sortiere[round=12]>{2^4, 4^2, 3^2, 5^{0.5}, 13, 25-1, ln(17)} ]
[ Sortiere>!{2^4, 4^2, 3^2, 5^{0.5}, 13, 25-1, ln(17)} ]
[ Sortiere{1.5%,1,12,9/5,7.12,-3e1,-5,2,sqrt{8}} ]
[ Sortiere[unsort]!{1.5%,1,12,-11/5,7.12,-3e1,-5,2,sqrt{8}} ]
end{document}
answered Dec 14 '17 at 21:48
Thomas
549212
549212
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f405995%2fsort-a-list-of-numbers-in-different-representations-with-a-macro-in-l3%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is the problem with the code you provided? The output is sorted correctly and the representation isn't changed.
– Skillmon
Dec 13 '17 at 14:49
@Skillmon there is no 0.231% in the input and in the output. And I want to have frac{a}{b} in the output instead of a/b.
– Thomas
Dec 13 '17 at 15:15