1

In my exemple I want to put two lists together, and order them

    \def\Wmax{10^8}
    \def\Wmin{.01}
\def\mySamplLst{\Wmin}
\xdef\mySamplNb{10}
\pgfmathparseFPU{(\Wmax/\Wmin)^(1/\mySamplNb)}
\xdef\myStep{\pgfmathresult}
\foreach \i in {1,2,...,\mySamplNb} {\pgfmathparseFPU{\Wmin*(\myStep^(\i))}
  \xdef\mySamplLst{\mySamplLst,\pgfmathresult}}

\def\mySamplNb{10} \def\mySamplCenter{10} \def\mySamplwidth{2} \pgfmathparseFPU{\mySamplwidth^(1/\mySamplNb)} \xdef\myStep{\pgfmathresult} \foreach \i in {-\mySamplNb,...,-1,0,1,2,...,\mySamplNb} {\pgfmathparseFPU{\mySamplCenter*(\myStep^(\i))} \xdef\mySamplLst{\mySamplLst,\pgfmathresult}}

\mySamplLst

It gives of something like : once you're done rounding up.

.01,0.1,1,10,100,1000,10000,100000,1000000,10000000,100000000,5,5,5,6,6,7,7,8,8,9,10,10,11,12,13,14,15,16,17,18,19

if you want to know I'm going to use that list in a samples at= for a semilog plot

  • Would you know a way to order that list ?

That seems like a straight forward request, it should be the simplest \sort{s,o,r,t,e,d} giving out a list containing {d,e,o,r,s,t} but noooooo... I've been wandering aimlessly between post on sorting specific objects and documentation of l3sort which give no working example...

Ok I can stop this rant...

Thank you for your time.

(I am seriously thinking of giving up on latex calculation and start using python to generate latex, since every little task seems to require 1 day of work, an acquaintance that speaks in latex source code as a first language or prepare a readable question on here and wait for generous experts.)

Yes, I know about Sorting a list of randomly generated numbers and I can't understand what's done.

P.S. : And you know what, if I put the code I gave, without the last line that displays the list, inside my standalone document, it actually takes some space... It's just calculations, no displays, and still it takes space on the document... I swear sometimes I think LaTeX is intentionally made to drive people crazy.

LMT-PhD
  • 1,244
  • 1
    On your P.S., if you put this in the body of your document inside a paragraph, the line ends turn into spaces, and those disturb the whitespace. You can kill line ends using %, but it's likely easier to either do the work in the preamble or perhaps if absolutely necessary between paragraphs. – Joseph Wright May 06 '21 at 13:40
  • Thank you for the explanation, I tried putting % at the end of every line, it removed some but still takes some space. How do you put things between paragraphs, when I don't use paragraphs in the first place ? In the end I will try to put as much as possible before the begin{document} – LMT-PhD May 06 '21 at 14:07
  • Would you mind removing the gratuitous profanities? – egreg May 06 '21 at 17:03
  • 1
    You should look at the sagetex package which gives you access to Python and a CAS called Sage. An example on this using Python and sagetex is here. There are lots more sagetex examples if you search. You need the Sage CAS, you can experiment with it with a free Cocalc account. – DJP May 06 '21 at 22:09
  • Thank you @DJP , this definitely deserves a look! – LMT-PhD May 07 '21 at 18:10
  • sagetex looked promising, what a shame it needs a heavy installation that did not work for me. – LMT-PhD May 08 '21 at 07:09

1 Answers1

1

The core idea you need here is that the sorting code needs some type of comparison to use. Here, we are comparing floating point values, so we need \fp_compare:nNnTF, viz.

  \clist_sort:Nn \mySamplLst
    {
      \fp_compare:nNnTF {#1} > {#2}
        { \sort_return_swapped: }
        { \sort_return_same: }
    }

i.e. if the first value is bigger than the second, we swap, whereas if the second value is bigger, we leave alone.

If you want to wrap this up in a command to apply to any old comma list, that's easy - we just need to keep a track of nesting.

\NewDocumentCommand \SortSampleList { m }
  {
    \clist_sort:Nn #1
      {
        \fp_compare:nNnTF {##1} > {##2}
          { \sort_return_swapped: }
          { \sort_return_same: }
      }
  }

I see you've used pgf for the maths, but as we are already using the expl3 code for sorting, I'd be minded to use it for the maths too. Keeping your variable naming but re-working in expl3 gives me

\def\Wmax{10^8}
\def\Wmin{.01}
\def\mySamplNb{10}

\ExplSyntaxOn \clist_new:N \mySamplLst \clist_put_right:Nx \mySamplLst { \fp_eval:n { \Wmin } } \fp_set:Nn \myStep { (\Wmax/\Wmin)^(1/\mySamplNb) } \int_step_inline:nnnn { 1 } { 1 } { \mySamplNb } { \clist_put_right:Nx \mySamplLst { \fp_eval:n { \Wmin*(\myStep^(#1)) } } } \ExplSyntaxOff

\def\mySamplNb{10} \def\mySamplCenter{10} \def\mySamplwidth{2}

\ExplSyntaxOn

\fp_set:Nn \myStep {\mySamplwidth^(1/\mySamplNb)} \int_step_inline:nnnn { -\mySamplNb } { 1 } { \mySamplNb } { \clist_put_right:Nx \mySamplLst { \fp_eval:n {\mySamplCenter*(\myStep^(#1))} } }

\clist_sort:Nn \mySamplLst { \fp_compare:nNnTF { #1 } > { #2 } { \sort_return_swapped: } { \sort_return_same: } }

\ExplSyntaxOff

You could there skip pre-calculating the step size: it's not necessary but I didn't want to alter your code too much.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • Thank you for your answer I'll dig into it and try to read it. – LMT-PhD May 06 '21 at 13:59
  • The problem is, that code only works once. I want to apply the sorting to several lists. Instead of just copying the code where I need it, I tried the simplest way : copy that code inside a \newcommand and call it once, and it doesn't work... so I put it in a file on its own and use \input... – LMT-PhD May 06 '21 at 15:54
  • Then I tried to put the second part that completes the list (starting with \fp_set:Nn \myStep {\mySamplwidth^(1/\mySamplNb)}) in a loop because I want several addings to the list. And it gives the same kind of error as my try with \newcommand – LMT-PhD May 06 '21 at 16:10
  • 1
    @LMT-PhD I've added how to use the sorting code in a dedicated command – Joseph Wright May 06 '21 at 16:57
  • 1
    @LMT-PhD I'd need a full example of what you are doing to help - possibly easiest if you mail me (joseph.wright@morningstar2.co.uk) with a 'real' example – Joseph Wright May 06 '21 at 16:58