1

I would like to compile a snippet of code using a random case distinction. How can I implement that?

No-LaTeX pseudocode:

x = rand(1,4) // random number between 1 and 4
if( x == 1 )
Let us talk about apples!
else if( x == 2 )
Let us talk about pears!
else if( x == 3 )
Let us talk about plums!
else 
Let us talk about cherries!

What is the best way this can be implemented? I would like to use it within math environments as well.

1
  • If you have an uptodate (2020) TeXlive then there is the random.tex file for generating and using random numbers. Apr 22, 2020 at 17:28

2 Answers 2

3

You can make a command that outputs a random item from a list.

The items must be braced if they contain more than one token.

There are two versions: the first defines a command (suitable for long lists), the second can be used inline.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\makerandomchoice}{mm}
 {
  \shuhalo_random_choice_make:Nn #1 { #2 }
 }

\NewExpandableDocumentCommand{\randomchoice}{m}
 {
  \shuhalo_random_choice:n { #1 }
 }

\cs_new_protected:Nn \shuhalo_random_choice_make:Nn
 {
  \cs_new:Npn #1
   {
    \shuhalo_random_choice:n { #2 }
   }
 }

\cs_new:Nn \shuhalo_random_choice:n
 {
  \tl_item:nn { #1 } { \int_rand:nn { 1 } { \tl_count:n { #1 } } }
 }

\ExplSyntaxOff

\makerandomchoice{\talk}{
  {Let us talk about apples!}
  {Let us talk about pears!}
  {Let us talk about plums!}
  {Let us talk about cherries!}
}
\makerandomchoice{\letter}{ABCDEFGHIJKLMNOPQRSTUVWXYZ}

\begin{document}

\talk

\talk

\talk

\talk

\talk

\letter\letter\letter\letter\letter\letter\letter\letter
\letter\letter\letter\letter\letter\letter\letter\letter

\randomchoice{{Pear}{Apple}{Plum}{Cherry}}

\randomchoice{{Pear}{Apple}{Plum}{Cherry}}

\randomchoice{{Pear}{Apple}{Plum}{Cherry}}

\randomchoice{{Pear}{Apple}{Plum}{Cherry}}

\end{document}

Spaces between items are ignored.

enter image description here

5
  • Thanks! Is it possible to use such a capability inline? It seems that your solution requires putting content in the header section. I am thinking of something that replaces the uncomment-recompile-comment-cycle, so to speak.
    – shuhalo
    Apr 21, 2020 at 22:48
  • @shuhalo I added the “inline” version.
    – egreg
    Apr 21, 2020 at 22:54
  • This does the job well. Just out of curiousity, what is the random number generator used? I suspect it is biased on my system.
    – shuhalo
    Apr 21, 2020 at 23:56
  • While sufficient for my purposes, it does not seem to mesh well with blank lines within the argument. I'd be curious how that can be fixed.
    – shuhalo
    Apr 21, 2020 at 23:57
  • @shuhalo Use {+m} instead of {m} in both main definitions.
    – egreg
    Apr 22, 2020 at 7:36
0

If you know R and knitr, this can be achieved with some very similar to your pseudocode:

\documentclass{article}
\begin{document}
<<fruit-roulette,echo=F,cache=FALSE>>=
x    <- sample(1:4, 1) 
talk <- if( x == 1 ) {"apples"} else {
        if( x == 2 ) {"pears"}  else {
        if( x == 3 ) {"plums"}  else {
        if( x == 4 ) {"cherries"}}}} 
@

Because $x=\Sexpr{x}$, let us talk about \Sexpr{talk}! 

\end{document}

In each compilation the LaTeX text could change, being one possible result:


mwe


As you see here, the \Sexpr{} can be use in math and text modes, but take into account that the R output will be formatted according to LaTeX rules (e.g., being the foo R object "big apples", \Sexpr{foo} or $\mbox{\Sexpr{foo}}$ will produce just "big apples", but \Sexpr{talk} will produce "bigapples".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.