TeX - LaTeX Asked by David G. on January 24, 2021
This is a part two of a question I asked a few days ago and was answered rather spectacularly by user egreg, which you can find here.
I’m now looking to take part of the LaTeX provided to me, and change the command that generates a list of numbers. I’d like to take the generated list and sort them by ascending order. The code that I have so far to generate the list of numbers is here:
documentclass[12pt]{article}
usepackage{xfp}
ExplSyntaxOn
NewDocumentCommand{randomvalues}{m}
{
int_rand:nn { -40 } { 40 }
prg_replicate:nn { #1 - 1 } { , :int_rand:nn { -40 } { 40 } }
}
ExplSyntaxOff
Currently, it will output a list m number of random and unique integers between -40 and 40, e.g. x = -2, 5, 0, -10, 30; for m=5. I want that list to look like x = -10, -2, 0, 5, 30.
The very first example in the documentation of l3sort
shows how to sort a list of numbers in a comma-separated list:
In the example above, a comma-separated list is sorted with clist_sort:Nn
. The first argument is the variable to be sorted, and the second argument is the comparison code.
I adapted your code to first generate the n random numbers, then store them in a sequence, and then sort that sequence with seq_sort:Nn
, and finally use that sequence with seq_use:Nn
.
documentclass[12pt]{article}
usepackage{xparse}
ExplSyntaxOn
seq_new:N l__davidg_randint_seq
NewDocumentCommand{randomvalues}{m}
{
int_step_inline:nn {#1}
{ seq_put_right:Nx l__davidg_randint_seq { int_rand:nn { -40 } { 40 } } }
seq_sort:Nn l__davidg_randint_seq
{
int_compare:nNnTF { ##1 } < { ##2 }
{ sort_return_same: }
{ sort_return_swapped: }
}
seq_use:Nn l__davidg_randint_seq { ,~ }
}
ExplSyntaxOff
begin{document}
randomvalues{5}
medskip
randomvalues{55}
end{document}
Correct answer by Phelype Oleinik on January 24, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP