1

I'm trying to use \pgfmathrandomlists.

I know that the command \pgfmathdeclarerandomlist{list}{{item1}{item2}...} produces a list and that the command \pgfmathrandomitem select a random item from a list.

Once I have created and used the list for what I need, I would like to empty the list so that I can use a new list with the same name. I tryed \pgfmathdeclarerandomlist{list}{} but it does not work.

3
  • I tried also \pgfdeclaremathrandomlist{list}{{}} but it does not work either
    – user126154
    Feb 13, 2015 at 16:30
  • Can you be more specific on how it "does not work"? A minimal (non-)working example would help here. If you use \pgfmathdeclarerandomlist{list}{{item3}{item4}...} after some initial usage of the command, the contents in {list}, i.e. {item1}{item2}..., will simply get replaced by {item3}{item4}...
    – Herr K.
    Feb 13, 2015 at 19:31
  • Macros can be thought of as string pointers. You don't need to "empty" a list, just create a new one and point to it. Feb 14, 2015 at 15:09

1 Answer 1

1

There is no internal mechanism by which \pgfmathrandomitem truly knows whether a list is empty. The case below shows this anomaly:

\documentclass{article}
\usepackage{pgf}

\makeatletter
\newcommand{\pgfmathclearrandomlist}[1]{%
  \expandafter\global\expandafter\let\csname pgfmath@randomlist@#1\endcsname\relax}
\makeatother

\begin{document}

\pgfmathdeclarerandomlist{list}{{item1}{item2}{item3}{item4}}
\pgfmathrandomitem{\result}{list}\result% Returns item1/item2/item3/item4

\pgfmathdeclarerandomlist{list}{}
\pgfmathrandomitem{\result}{list}\result% Returns item1

\pgfmathdeclarerandomlist{list}{{}}
\pgfmathrandomitem{\result}{list}\result% Returns {}

\pgfmathclearrandomlist{list}
\pgfmathrandomitem{\result}{list}\result% List doesn't exist

\end{document}

The first instruction set creates a list with four items, from which a random one is selected. The second instruction "clears" this list, although the output is always the first item in the previously-defined list - item1. You can visually clear the list by giving it a single item that is empty - {}, but that may not help. Perhaps the solution would be to update the way \pgfmathdeclarerandomlist works when being assigned an empty list (this would be a marginal update/patch, since it seems like a bug), or clear it officially using \pgfmathclearrandomlist.

You must log in to answer this question.

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