1

I have two list of questions. I want to create a command for declare random list with list name as argument.

\documentclass{article}
\usepackage{tikz}

\def\qfirstlist{%
{I - 1?}%
{I - 2?}%
{I - 3?}%
{I - 4?}%
{I - 5?}%
{I - 6?}%
{I - 7?}%
{I - 8?}%
{I - 9?}%
{I - 10?}%
}

\def\qsecondlist{%
{II - 1?}%
{II - 2?}%
{II - 3?}%
{II - 4?}%
{II - 5?}%
{II - 6?}%
{II - 7?}%
{II - 8?}%
{II - 9?}%
{II - 10?}%
}

\newcommand\declarelist[1]{
 \pgfmathdeclarerandomlist{#1}{\csname q#1\endcsname}
}

\declarelist{firstlist}
\declarelist{secondlist}

\begin{document}
\pgfmathrandomitem\z{firstlist}
\z

\pgfmathrandomitem\z{secondlist} 
\z
\end{document}

I expect that \declarelist{xxx} will work as \pgfmathdeclarerandomlist{xxx}{\qxxx} but it's don't. After \declarelist{firstlist} it's no list of items, there are list of one item I - 1?I - 2?I - 3?I - 4?I - 5?I - 6?I - 7?I - 8?I - 9?I - 10?

2
  • I am not sure I understand. \declarelist{firstlist}\qfirstlist produces the same output as \pgfmathrandomitem\z{firstlist}\z. Jan 11, 2017 at 12:40
  • I want refuse to use \qfirstlist token. I need operate with one identefire firstlist
    – Konstantin
    Jan 11, 2017 at 12:44

1 Answer 1

1

Define \declarelist as

\newcommand\declarelist[1]%
  {\expandafter\let\expandafter\tmp\csname q#1\endcsname
   \pgfmathdeclarerandomlist{#1}{\tmp}%
  }

then it will work.

The point is that \pgfmathdeclarerandomlist expands its first argument once before processing it. With your definition, expanding \csname q#1\endcsname for #1 = firstlist will result in \qfirstlist, which will be interpreted as a list with a single element. Randomly selecting an element will always result in \qfirstlist, which later is expanded to the complete list. With the definition above, however, the expansion of \tmp yields {I - 1?}{I - 2?}{I - 3?}... as needed.

enter image description here

\documentclass{article}
\usepackage{tikz}

\def\qfirstlist{%
{I - 1?}%
{I - 2?}%
{I - 3?}%
{I - 4?}%
{I - 5?}%
{I - 6?}%
{I - 7?}%
{I - 8?}%
{I - 9?}%
{I - 10?}%
}

\def\qsecondlist{%
{II - 1?}%
{II - 2?}%
{II - 3?}%
{II - 4?}%
{II - 5?}%
{II - 6?}%
{II - 7?}%
{II - 8?}%
{II - 9?}%
{II - 10?}%
}

\newcommand\declarelist[1]%
  {\expandafter\let\expandafter\tmp\csname q#1\endcsname
   \pgfmathdeclarerandomlist{#1}{\tmp}%
  }

\declarelist{firstlist}
\declarelist{secondlist}

\begin{document}
\pgfmathrandomitem\z{firstlist}
\z

\pgfmathrandomitem\z{secondlist} 
\z
\end{document}

You must log in to answer this question.

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