使单词的字母以随机顺序出现

使单词的字母以随机顺序出现

背景是一个儿童游戏,其中一系列问题一次出现在屏幕上,并且答案通过按下某个按钮逐渐地、随机地一个字母一个地出现。

例如,“问题 1:意大利的首都是哪里?”答案显示为未指定字符数的空白,按下某个按钮可能会出现 E,然后是 - - R - - E - -,然后一个接一个一直到 - -ROME- -。或者类似的安排。

这是什么仪器?如何让字母随机显示。

答案1

这是可以做到这一点的东西。我写这篇文章也是因为除了memberQ宏之外,它或多或少取自这里,它定义了 pgf 函数randompermutation(<n>),它产生整数的随机排列{1,...,<n>},和take返回子列表。基于这些函数和parser模块定义的宏是\ShowPartially。为了在单词上使用它,您必须首先“初始化”它,例如

 \ShowPartially{initial,n=2,word=Murmeltier}    

{1,...,<# of characters>}然后存储数字的一个排列,你可以显示例如 4 个随机字母

 \ShowPartially{n=4}

这样,较大的结果n将始终是较小的结果n。如果您想创建新的排列列表,只需\ShowPartially再次使用该initial键调用即可。

\documentclass{article}
\usepackage{pgf}
\usepgfmodule{parser}
\makeatletter
%membership test    
\pgfmathdeclarefunction{memberQ}{2}{%
  \begingroup%
    \edef\pgfutil@tmpb{0}%memberQ({\lstPast},\inow)
    \edef\pgfutil@tmpa{#2}%
    \expandafter\pgfmath@member@i#1\pgfmath@token@stop
    \edef\pgfmathresult{\pgfutil@tmpb}%
    \pgfmath@smuggleone\pgfmathresult%
  \endgroup}
\def\pgfmath@member@i#1{%
    \ifx\pgfmath@token@stop#1%
    \else
      \edef\pgfutil@tmpc{#1}%
      \ifx\pgfutil@tmpc\pgfutil@tmpa\relax%
      \gdef\pgfutil@tmpb{1}%
      \fi%
      \expandafter\pgfmath@member@i
    \fi}  
\pgfmathdeclarefunction{randompermutation}{1}{%
\begingroup%          
\c@pgf@counta=0%
\edef\pgfutil@tmpa{0}%
\loop
\advance\c@pgf@counta by1%
\edef\pgfutil@tmpa{\pgfutil@tmpa,\the\c@pgf@counta}%
\ifnum\c@pgf@counta<#1\relax
\repeat
\loop
\advance\c@pgf@counta by-1%
\pgfmathtruncatemacro{\pgfutil@tmpb}{1+rnd*\c@pgf@counta}%
\pgfmathtruncatemacro{\pgfutil@tmpc}{{\pgfutil@tmpa}[\pgfutil@tmpb]}%
\ifnum\c@pgf@counta=\numexpr#1-1\relax
\edef\pgfmathresult{\pgfutil@tmpc}%
\else
\edef\pgfmathresult{\pgfmathresult,\pgfutil@tmpc}%
\fi
\edef\pgfutil@tmpd{\pgfutil@tmpa}%
\edef\pgfutil@tmpa{0}%
\pgfutil@for\my@item:={\pgfutil@tmpd}\do{%
\unless\ifnum\pgfutil@tmpc=\my@item
\unless\ifnum\my@item=0\relax
\edef\pgfutil@tmpa{\pgfutil@tmpa,\my@item}%
\fi\fi}%
\ifnum\c@pgf@counta>0\relax
\repeat
\pgfmath@smuggleone\pgfmathresult%
\endgroup}
\pgfmathdeclarefunction{take}{2}{%
\begingroup%
\c@pgf@counta=0%
\pgfutil@for\my@item:={#1}\do{%
\advance\c@pgf@counta by1%
\ifnum\c@pgf@counta<#2\relax
\ifnum\c@pgf@counta=1\relax
\edef\pgfmathresult{\my@item}%
\else
\edef\pgfmathresult{\pgfmathresult,\my@item}%
\fi\fi}%
\pgfmath@smuggleone\pgfmathresult%
\endgroup}
\makeatother    
\newif\ifhideletterinitial
\hideletterinitialfalse
\pgfkeys{/hide letter/.cd,initial/.is if=hideletterinitial,n/.initial=1,
word/.initial=Rome}
\newcounter{letterpos}
\pgfparserdef{letterhide}{all};{\pgfparserswitch{final}}% 
\pgfparserdefunknown{letterhide}{all}%
{\stepcounter{letterpos}%
\unless\ifhideletterinitial
\pgfmathtruncatemacro{\itest}{memberQ({\mysublist},\number\value{letterpos})}%
\ifnum\itest=1\relax
\pgfparserletter
\else
\phantom{\pgfparserletter}%
\fi
\fi}% \pgfparserletter
\pgfparserset{letterhide/silent=true}%
\newcommand{\ShowPartially}[1]{%
\pgfkeys{/hide letter/.cd,#1}%
\ifhideletterinitial
\setcounter{letterpos}{0}%
\xdef\HideLetterWord{\pgfkeysvalueof{/hide letter/word}}%
\edef\temp{\noexpand\pgfparserparse{letterhide}\HideLetterWord;}%
\temp
\xdef\HideLetterNum{\number\value{letterpos}}%
\pgfmathsetmacro{\myperm}{randompermutation(\HideLetterNum)}%
\xdef\HideLetterPerm{\myperm}%
\hideletterinitialfalse
\fi
\pgfmathsetmacro{\mysublist}{take("\HideLetterPerm",\pgfkeysvalueof{/hide letter/n})}%
\setcounter{letterpos}{0}%
\edef\temp{\noexpand\pgfparserparse{letterhide}\HideLetterWord;}%
\temp
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\pgfmathsetseed{123}
\ShowPartially{initial,n=2,word=Murmeltier} 

\ShowPartially{n=3}

\ShowPartially{n=4}

\ShowPartially{n=5}

\ShowPartially{n=8}

\pgfmathsetseed{42}
\ShowPartially{initial,n=5,word=Amsterdam}  
\end{document}

在此处输入图片描述

这是ocgx2它的一个版本。

\documentclass{article}
\usepackage{ocgx2}
\usepackage{pgf}
\usepgfmodule{parser}
\renewcommand*\familydefault{\sfdefault}
\makeatletter
%membership test    
\pgfmathdeclarefunction{memberQ}{2}{%
  \begingroup%
    \edef\pgfutil@tmpb{0}%memberQ({\lstPast},\inow)
    \edef\pgfutil@tmpa{#2}%
    \expandafter\pgfmath@member@i#1\pgfmath@token@stop
    \edef\pgfmathresult{\pgfutil@tmpb}%
    \pgfmath@smuggleone\pgfmathresult%
  \endgroup}
\def\pgfmath@member@i#1{%
    \ifx\pgfmath@token@stop#1%
    \else
      \edef\pgfutil@tmpc{#1}%
      \ifx\pgfutil@tmpc\pgfutil@tmpa\relax%
      \gdef\pgfutil@tmpb{1}%
      \fi%
      \expandafter\pgfmath@member@i
    \fi}  
\pgfmathdeclarefunction{randompermutation}{1}{%
\begingroup%          
\c@pgf@counta=0%
\edef\pgfutil@tmpa{0}%
\loop
\advance\c@pgf@counta by1%
\edef\pgfutil@tmpa{\pgfutil@tmpa,\the\c@pgf@counta}%
\ifnum\c@pgf@counta<#1\relax
\repeat
\loop
\advance\c@pgf@counta by-1%
\pgfmathtruncatemacro{\pgfutil@tmpb}{1+rnd*\c@pgf@counta}%
\pgfmathtruncatemacro{\pgfutil@tmpc}{{\pgfutil@tmpa}[\pgfutil@tmpb]}%
\ifnum\c@pgf@counta=\numexpr#1-1\relax
\edef\pgfmathresult{\pgfutil@tmpc}%
\else
\edef\pgfmathresult{\pgfmathresult,\pgfutil@tmpc}%
\fi
\edef\pgfutil@tmpd{\pgfutil@tmpa}%
\edef\pgfutil@tmpa{0}%
\pgfutil@for\my@item:={\pgfutil@tmpd}\do{%
\unless\ifnum\pgfutil@tmpc=\my@item
\unless\ifnum\my@item=0\relax
\edef\pgfutil@tmpa{\pgfutil@tmpa,\my@item}%
\fi\fi}%
\ifnum\c@pgf@counta>0\relax
\repeat
\pgfmath@smuggleone\pgfmathresult%
\endgroup}
\pgfmathdeclarefunction{take}{2}{%
\begingroup%
\c@pgf@counta=0%
\pgfutil@for\my@item:={#1}\do{%
\advance\c@pgf@counta by1%
\ifnum\c@pgf@counta<#2\relax
\ifnum\c@pgf@counta=1\relax
\edef\pgfmathresult{\my@item}%
\else
\edef\pgfmathresult{\pgfmathresult,\my@item}%
\fi\fi}%
\pgfmath@smuggleone\pgfmathresult%
\endgroup}
\makeatother    
\newif\ifhideletterinitial
\hideletterinitialfalse
\pgfkeys{/hide letter/.cd,initial/.is if=hideletterinitial,n/.initial=1,
word/.initial=Rome}
\newcounter{letterpos}
\pgfparserdef{letterhide}{all};{\pgfparserswitch{final}}% 
\pgfparserdefunknown{letterhide}{all}%
{\stepcounter{letterpos}%
\unless\ifhideletterinitial
\pgfmathtruncatemacro{\itest}{memberQ({\mysublist},\number\value{letterpos})}%
\ifnum\itest=1\relax
\pgfparserletter
\else
\phantom{\pgfparserletter}%
\fi
\fi}% \pgfparserletter
\pgfparserset{letterhide/silent=true}%
\newcommand{\ShowPartially}[1]{%
\pgfkeys{/hide letter/.cd,#1}%
\ifhideletterinitial
\setcounter{letterpos}{0}%
\xdef\HideLetterWord{\pgfkeysvalueof{/hide letter/word}}%
\edef\temp{\noexpand\pgfparserparse{letterhide}\HideLetterWord;}%
\temp
\xdef\HideLetterNum{\number\value{letterpos}}%
\pgfmathsetmacro{\myperm}{randompermutation(\HideLetterNum)}%
\xdef\HideLetterPerm{\myperm}%
\hideletterinitialfalse
\fi
\pgfmathsetmacro{\mysublist}{take("\HideLetterPerm",\pgfkeysvalueof{/hide letter/n})}%
\setcounter{letterpos}{0}%
\edef\temp{\noexpand\pgfparserparse{letterhide}\HideLetterWord;}%
\temp
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\pgfmathsetseed{123}

\edef\iloop{2}\hfill\loop
\actionsocg[onmouseall]{}{,,ocg\iloop,}{,,,ocg\iloop}{\iloop}\ 
\edef\iloop{\the\numexpr\iloop+1}%
\ifnum\iloop<11\relax
\repeat


\ShowPartially{initial,n=1,word=Murmeltier}\par 

\edef\iloop{2}\loop
\begin{ocg}{OCG 2}{ocg\iloop}{0}
\ShowPartially{n=\iloop}
\end{ocg}\par
\edef\iloop{\the\numexpr\iloop+1}%
\ifnum\iloop<11\relax
\repeat
\end{document}

使用 Acrobat 阅读器查看时,会显示从 1 到 10 的数字,如果单击其中一个数字,则会显示相应的字母子集。(我不知道如何在此处上传插图。)

相关内容