Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Is there an environment (or macro for itemize) that shuffles all items randomly each time the pdf is generated?

share|improve this question
    
Although it doesn't have an answer, the example in this question might be of some help: Problem randomizing answers in eqexam. –  Alan Munn Feb 3 '13 at 18:43
add comment

2 Answers

FWIW, ConTeXt supports this out of the box. Simply add the random key to \startitemize. (In ConTeXt, \startitemize[n] is equivalent to \begin{enumerate} of LaTeX).

\starttext
\startitemize[n,random]
  \startitem One \stopitem
  \startitem Two \stopitem
  \startitem Three \stopitem
  \startitem Four \stopitem
  \startitem Five \stopitem
  \startitem Six \stopitem
\stopitemize
\stoptext

which gives:

enter image description here

The random seed is stored in the tuc file so that you get the same output each time the file is compiled (otherwise, multiple compiles can lead to an infinite cycle and you may not get all the references correct). So you need to delete the tuc file to change the random seed, and hence get a different output.

share|improve this answer
    
You can also set the random seed without deleting the tuc file. See: wiki.contextgarden.net/Random –  Dave Jarvis Oct 5 '13 at 18:44
add comment

As the usual implementation of \begin{list} \item ... \item ... \item ... \end{list} doesn't read or save the actual text of the items, some out-of-my-league hacking needs to be done to save then permute list items if you want the same interface.

I asked a similar question on the pgf-users mailing list several years ago. Here is Mark Wibrow's answer for randomizing a PGF list of the form declared by \pgfmathdeclarelist. I've dropped this snippet into my exam files ever since.

\documentclass{article} 
\usepackage{tikz} 

\begin{document} 

\makeatletter 
\def\pgfmathdeclarelist#1#2{% 
        \def\pgfmath@list@name{#1}% 
        \c@pgfmath@counta=0% 
        \pgfmath@declarelistlist#2{\pgfmath@stop}% 
}% 
\def\pgfmath@declarelistlist#1{% 
        \ifx#1\pgfmath@stop% 
                \expandafter\edef\csname pgfmath@list@\pgfmath@list@name 
@length\endcsname{\the\c@pgfmath@counta}% 
        \else% 
                \advance\c@pgfmath@counta by1\relax% 
                \pgfutil@namedef{pgfmath@list@\pgfmath@list@name @\the\c@pgfmath@counta}{#1}% 
                \expandafter\pgfmath@declarelistlist% 
        \fi% 
} 

\def\pgfmathgetlistitem#1#2#3{\expandafter\let\expandafter#1\expandafter=\csname 
pgfmath@list@#2@#3\endcsname} 

\def\pgfmathsetlistitem#1#2#3{% 
        \pgfutil@namedef{pgfmath@list@#1@#2}{#3}% 
} 

\def\pgfmathgetlistlength#1#2{% 
        \expandafter\let\expandafter#1\expandafter=\csname 
pgfmath@list@#2@length\endcsname% 
} 

\def\pgfmathknuthshuffle#1{% 
        \pgfmathgetlistlength\pgfmath@len{#1}% 
        \pgfmathloop% 
        \ifnum\pgfmathcounter>\pgfmath@len% 
        \else% 
                \pgfmathrandominteger\pgfmath@temp{1}{\pgfmath@len}% 
                \pgfmathgetlistitem\pgfmath@@temp{#1}{\pgfmathcounter}% 
                \pgfmathgetlistitem\pgfmath@@@temp{#1}{\pgfmath@temp}% 
                \def\pgfmath@marshal{\pgfmathsetlistitem{#1}}% 
                \expandafter\pgfmath@marshal\expandafter{\expandafter\pgfmath@temp\expandafter}\expandafter{\pgfmath@@temp}% 
                \expandafter\pgfmath@marshal\expandafter{\expandafter\pgfmathcounter\expandafter}\expandafter{\pgfmath@@@temp}% 
        \repeatpgfmathloop% 
} 

\makeatother 

\pgfmathdeclarelist{mylist}{{A}{B}{C}{D}{E}{F}{G}{H}{I}{J}{K}{L}{M}} 
\pgfmathgetlistlength{\l}{mylist} 

\begin{tikzpicture}[every node/.style={circle, draw}] 

\foreach \i in {1,...,\l}{ 
        \pgfmathgetlistitem{\x}{mylist}{\i} 
        \node at (0,-\i) (\x-1) {\x}; 
} 

\pgfmathknuthshuffle{mylist} 

\foreach \i in {1,...,\l}{ 
        \pgfmathgetlistitem{\x}{mylist}{\i} 
        \node at (7.5,-\i) (\x-2) {\x}; 
} 

\foreach \i in {1,...,\l}{ 
        \pgfmathgetlistitem{\x}{mylist}{\i} 
        \draw [->] (\x-1) -- (\x-2); 
} 

\end{tikzpicture} 
\end{document} 
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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