7

I would like to produce random texts in which some specific items would be sorted and printed in a random order, varying at each compilation. The intended purpose is to remove any hidden bias in the way the list is constructed. I have looked for a ready to use package or solution, but did not find any.

More specifically, I am working on the basis of something like this.

\documentclass[12pt]{article}
\usepackage[a4paper]{geometry}
\newcommand{\tobesorted}{}
\newenvironment{sortingitems}{}{}
\begin{document}
In my farm there are:
\begin{sortingitems}
\tobesorted pigs,
\tobesorted cows,
\tobesorted rabbits,
\tobesorted gnus,
\end{sortingitems}
and they all live in perfect harmony.
\end{document} 

which would print the list of animals in a random order that would vary at each compilation. In my intended application, I would have several a few hundreds short items to be sorted. For the moment, I do not care much about punctuation at the end of the last item (but I am also interested).

4
  • Are you willing and able to use LuaLaTeX to compile your document?
    – Mico
    Commented Nov 19, 2023 at 20:45
  • See also tex.stackexchange.com/questions/346260/… Commented Nov 20, 2023 at 4:06
  • 1
    @Mico Yes indeed.
    – Denis
    Commented Nov 20, 2023 at 5:15
  • @JohnKormylo Quite nice also. Thanks.
    – Denis
    Commented Nov 20, 2023 at 5:16

2 Answers 2

7

You can use the built-in \seq_shuffle:N command from the kernel to shuffle items and then use \seq_map_inline:Nn to map the items to your list.

Here's a version that puts everything into a single command. If you prefer you could make the command just do the shuffling and mapping and insert that command inside each list environment.

\documentclass{article}
\ExplSyntaxOn
\seq_new:N \l_denis_shuffle_items_seq
\NewDocumentCommand{\shuffleitems}{m}{
    \seq_set_from_clist:Nn \l_denis_shuffle_items_seq {#1}
    \seq_shuffle:N \l_denis_shuffle_items_seq
    \begin{enumerate}
    \seq_map_inline:Nn \l_denis_shuffle_items_seq {\item ##1}
    \end{enumerate}
} 
\ExplSyntaxOff
\begin{document}
\shuffleitems{pigs, cows,rabbits,gnus}
\end{document}

possible output of code

In your specific use case, if you want the enumerated items as a comma separated list inserted in running text rather than an enumeration, you can use the \seq_use:Nnnn macro to do that. This is a very nice macro which takes a sequence and then 3 arguments corresponding to the elements you want between 2, more than two, and the final 2 elements.

\documentclass{article}
\ExplSyntaxOn
\seq_new:N \l_denis_shuffle_questions_seq
\NewDocumentCommand{\shuffleitems}{m}{
    \seq_set_from_clist:Nn \l_denis_shuffle_questions_seq {#1}
    \seq_shuffle:N \l_denis_shuffle_questions_seq
    \seq_use:Nnnn \l_denis_shuffle_questions_seq {~and~}{,~}{,~and~}
}

\ExplSyntaxOff
\begin{document}
The \shuffleitems{pigs, cows,rabbits,gnus} all live in perfect harmony.
\end{document}

possible output of second example

6
  • Your solution is very elegant. Will test it more extensively. But thanks a lot for the very nice solution.
    – Denis
    Commented Nov 19, 2023 at 20:35
  • @Denis I've added a version that is closer to your actual use case.
    – Alan Munn
    Commented Nov 19, 2023 at 20:47
  • I can't help thinking TeX is not the easiest solution!
    – cfr
    Commented Nov 20, 2023 at 2:30
  • @cfr I don't know, this is pretty simple! It would definitely depend on the use case. I have a problem set that randomizes 5 macros using lua code, and it's more complicated than this. So much so that I'll probably rewrite it using this method. But I'm not going to have TeX randomize my experimental stimuli... :)
    – Alan Munn
    Commented Nov 20, 2023 at 2:42
  • I suppose it's just that I've never done it this way. It seems a lot of bother, I guess.
    – cfr
    Commented Nov 20, 2023 at 2:49
1

A way using knitr:

MWE

MWE.Rnw (Rstudio) or MWE.Rtex (Overleaf):

\documentclass{article}
\begin{document}
<<echo=F,>>=
v <- c("pigs","cows","rabbits","gnus")
@
\begin{itemize} 
\item I have \Sexpr{combine_words(sample(v))}
\item You have \Sexpr{combine_words(sample(v))}
\item He has \Sexpr{combine_words(sample(v))}
\item We have  
<<foo, echo=F,results='asis'>>=
cat("\\begin{itemize}\\item ")
cat(sample(v), sep="\n\\item ")
cat("\\end{itemize} ")
@
\item They have  
<<morefoo, echo=F,results='asis'>>=
<<foo>>
@
\end{itemize} 
\end{document}
1
  • Also nice using a different path. Thanks for this.
    – Denis
    Commented Nov 21, 2023 at 10:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .