以列表名称作为参数命令声明随机列表

以列表名称作为参数命令声明随机列表

我有两个问题列表。我想创建一个命令,以列表名称作为参数来声明随机列表。

\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}

我期望这\declarelist{xxx}会起作用,\pgfmathdeclarerandomlist{xxx}{\qxxx}但事实并非如此。\declarelist{firstlist}没有项目列表之后,有一个项目的列表I - 1?I - 2?I - 3?I - 4?I - 5?I - 6?I - 7?I - 8?I - 9?I - 10?

答案1

定义\declarelist

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

那么它就会起作用。

重点是\pgfmathdeclarerandomlist扩展了它的第一个参数一次在处理之前。按照您的定义,扩展\csname q#1\endcsname=#1firstlist得到\qfirstlist,它将被解释为具有单个元素的列表。随机选择一个元素将始终得到\qfirstlist,稍后将扩展为完整列表。但是,按照上述定义,扩展 可根据需要产生\tmp结果{I - 1?}{I - 2?}{I - 3?}...

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}

相关内容