5

I need to randomly change which symbol is attached to a particular command every time I compile my script.

I had hoped that this would be possible using the pgffor package, as in the minimal example below (where I am trying to redefine \add each time I compile):

\documentclass{article}
\usepackage{fdsymbol}
\usepackage{pgffor}
\newcommand{\add}{\medtriangleright}
\pgfmathsetseed{\number\pdfrandomseed}
\begin{document}
\pgfmathdeclarerandomlist{choices}{{\renewcommand{\add}{\pm}}{\renewcommand{\add}{+}}{\renewcommand{\add}{\cdot}}}
\foreach\x in {1}{
\pgfmathrandomitem{\choice}{choices}
\choice\space
}
$$5\add4$$
\end{document}

However this doesn't work. This is strange as this precise code works fine when I try to generate a random number from a list:

\documentclass{article}
\usepackage{fdsymbol}
\usepackage{pgffor}
\newcommand{\add}{\medtriangleright}
\pgfmathsetseed{\number\pdfrandomseed}
\begin{document}
\pgfmathdeclarerandomlist{choices}{{1}{2}{3}}
\foreach\x in {1}{
\pgfmathrandomitem{\choice}{choices}
\choice\space
}
$$5\add4$$
\end{document}

Any ideas what I'm doing wrong here? Thanks in advance.

Matthew
  • 51
  • Why do you use the \foreach loop? It doesn't seem to do anything in your example. – David Purton Oct 27 '18 at 11:59
  • As an aside, use \[ …\] instead of $$ … $$ for display maths. See https://tex.stackexchange.com/q/503/87678 – David Purton Oct 27 '18 at 12:07
  • Thanks! Yeah, the \foreach bit was because I was editing some other code where it was needed and didn't spot the redundancy before posting. Sorry for the confusion. – Matthew Oct 27 '18 at 20:07

2 Answers2

6

Unless I misunderstand your question, you don't need the \renewcommand macros or \foreach loop. You can also randomly change the operator mid way through the document.

\documentclass{article}
\usepackage{fdsymbol}
\usepackage{pgf}
\pgfmathsetseed{\number\pdfrandomseed}
\pgfmathdeclarerandomlist{choices}{{\medtriangleright}{\pm}{+}{\cdot}}
\begin{document}
\pgfmathrandomitem{\op}{choices}
\[ 5 \op 4 \]
\pgfmathrandomitem{\op}{choices}
\[ 5 \op 4 \]
\pgfmathrandomitem{\op}{choices}
\[ 5 \op 4 \]
\pgfmathrandomitem{\op}{choices}
\[ 5 \op 4 \]
\end{document}

enter image description here

David Purton
  • 25,884
6

You don't need \renewcommand.

\documentclass{article}
\usepackage{pgffor}

%\pgfmathsetseed{\number\pdfrandomseed}

\newcommand{\add}{\pgfmathrandomitem{\choice}{choices}\choice}
\pgfmathdeclarerandomlist{choices}{{\pm}{+}{\cdot}}

\begin{document}

$5\add4$
$5\add4$
$5\add4$

$5\add4$
$5\add4$
$5\add4$

$5\add4$
$5\add4$
$5\add4$

$5\add4$
$5\add4$
$5\add4$

\end{document}

enter image description here

A much more intuitive version with expl3 (not yet working with XeLaTeX, though).

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewExpandableDocumentCommand{\add}{}
 {
  \clist_rand_item:N \c_matthew_add_symbols_clist
 }
\clist_const:Nn \c_matthew_add_symbols_clist { \pm, +, \cdot }
\ExplSyntaxOff

\begin{document}

$5\add4$
$5\add4$
$5\add4$
$5\add4$
$5\add4$
$5\add4$
$5\add4$
$5\add4$
$5\add4$

\end{document}

This idea has more advantages, because it can be used in a “full expansion” context, which the PGF version can't.

egreg
  • 1,121,712