随机化一段代码行

随机化一段代码行

这与之前的问题,但我不太明白给出的答案。我所问的问题的一个可能用途是在下面的代码中匹配问题风格,其中代码行需要被随机解释以进行排版

\documentclass[8pt,letterpaper]{book}

\usepackage[allowrandomize,nopoints,proofing,answerkey]{eqexam}
\usepackage{textcomp}
\usepackage{graphicx}

\begin{document}
        \begin{exam}{}
        \begin{problem*}[\auto]
        Match each of names on the right, with the corresponding symbol on the left.

        \begin{multicols}{2}
        \begin{parts}
        \item\PTs*{2}\fillin{.5in}{\ref{LSNO}} \includegraphics[height=.5cm]{questionBank_img/LSNO}
        \item\PTs*{2}\fillin{.5in}{\ref{LSNC}} \includegraphics[height=.5cm]{questionBank_img/LSNC} 
        \item\PTs*{2}\fillin{.5in}{\ref{LSNOHC}} \includegraphics[height=.5cm]{questionBank_img/LSNOHC} 
        \item\PTs*{2}\fillin{.5in}{\ref{LSNCHO}} \includegraphics[height=.5cm]{questionBank_img/LSNCHO} 
        \item\PTs*{2}\fillin{.5in}{\ref{FoSNO}} \includegraphics[height=.5cm]{questionBank_img/FoSNO} 
        \item\PTs*{2}\fillin{.5in}{\ref{FoSNC}} \includegraphics[height=.5cm]{questionBank_img/FoSNC} 
        \item\PTs*{2}\fillin{.5in}{\ref{PSNO}} \includegraphics[height=.5cm]{questionBank_img/PSNO} 
        \item\PTs*{2}\fillin{.5in}{\ref{PSNC}} \includegraphics[height=.5cm]{questionBank_img/PSNC} 
        \item\PTs*{2}\fillin{.5in}{\ref{LLSNO}} \includegraphics[height=.5cm]{questionBank_img/LLSNO} 
        \item\PTs*{2}\fillin{.5in}{\ref{LLSNC}} \includegraphics[height=.5cm]{questionBank_img/LLSNC} 
        \item\PTs*{2}\fillin{.5in}{\ref{TSNO}} \includegraphics[height=.5cm]{questionBank_img/TSNO} 
        \item\PTs*{2}\fillin{.5in}{\ref{TSNC}} \includegraphics[height=.5cm]{questionBank_img/TSNC} 
        \item\PTs*{2}\fillin{.5in}{\ref{FlSNO}} \includegraphics[height=.5cm]{questionBank_img/FlSNO} 
        \item\PTs*{2}\fillin{.5in}{\ref{FlSNC}} \includegraphics[height=.5cm]{questionBank_img/FlSNC} 
        \columnbreak

        \renewcommand{\thepartno}{\Alph{partno}}
        \partsformat{\Alph{partno}.}            
        % The above redefinitions are reset when we leave the group, when \end{parts}
        % is completed. If the original definitions are needed to be reset before then
        % execute the following commands: \defaultthepartno, \defaultpartsformat

        % \foritem{a} resets the partno counter back to 0, and displays (a), which
        % has been redefined to A.


%the following lines of code should be shuffled somehow????

        \foritem{a}\label{LSNO} Limit switch NC
        \item\label{LSNC} Limit switch NC
        \item\label{LSNOHC} Limit switch NO, held closed
        \item\label{LSNCHO}  Limit switch NC, held open
        \item\label{FoSNO} Foot switch, NO
        \item\label{FoSNC} Foot switch, NC
        \item\label{PSNO} Pressure/Vacuum switch, NO
        \item\label{PSNC} Pressure/Vacuum switch, NC
        \item\label{LLSNO} Liquid level switch, NO
        \item\label{LLSNC} Liquid level switch, NC
        \item\label{TSNO} Temperature activated switch, NO
        \item\label{TSNC} Temperature activated switch, NC
        \item\label{FlSNO} Flow switch, NO
        \item\label{FlSNC}  Flow switch, NC

        \end{parts}
        \end{multicols}
        \end{problem*}
        \end{exam}
\end{document}

其结果如下: 这本来应该是一个简单的测试,不是吗?

答案1

几乎明白了……只需要让计数器正常工作。我缩短了示例,并用文本替换了实际图像,以便任何人都可以按原样编译

\documentclass{article}
\usepackage{tikz}
\usepackage[allowrandomize,nopoints,proofing,answerkey]{eqexam} % for instructor copy

\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

\begin{exam}{}
\begin{problem*}[\auto]
    Match each of names on the right, with the corresponding symbol on the left.
    \begin{multicols}{2}
        \begin{parts}
        \item\PTs*{2}\fillin{.5in}{\ref{LSNO}} image LSNO %\includegraphics[height=.5cm]{questionBank_img/LSNO}
        \item\PTs*{2}\fillin{.5in}{\ref{LSNC}} image LSNC %\includegraphics[height=.5cm]{questionBank_img/LSNC} 
        \item\PTs*{2}\fillin{.5in}{\ref{LSNOHC}} image LSNOHC %\includegraphics[height=.5cm]{questionBank_img/LSNOHC} 

        \columnbreak

        \renewcommand{\thepartno}{\Alph{partno}}
        \partsformat{\Alph{partno}.}            
        % The above redefinitions are reset when we leave the group, when \end{parts}
        % is completed. If the original definitions are needed to be reset before then
        % execute the following commands: \defaultthepartno, \defaultpartsformat

        % \foritem{a} resets the partno counter back to 0, and displays (a), which
        % has been redefined to A.

        \pgfmathdeclarelist{mylist}{{\label{LSNO} Limit switch NO}{\label{LSNC} Limit switch NC}{\label{LSNOHC} Limit switch NOHC}}
        \pgfmathgetlistlength{\l}{mylist}

        \pgfmathknuthshuffle{mylist}

        \foreach \i in {1,...,\l}{
        \ifnum\i=1 {\foritem{a}
            \pgfmathgetlistitem{\x}{mylist}{\i}
            \x}
        \else {\item \pgfmathgetlistitem{\x}{mylist}{\i} \x}
        \fi
        }

        \end{parts}
    \end{multicols}
\end{problem*}
\end{exam}
\end{document}

有点混乱,但结果是: 在此处输入图片描述

更新... 我的简短 3 元素示例似乎只起作用了。当我将其扩展为更长的列表时,所有“红色”答案都乱了顺序... 哎呀!如果不是因为它解决了我关于随机化代码的原始帖子,我本想删除这个答案的。我只需要找出一种通过随机化过程跟踪标签/引用的方法

更新 #2... 在进行了一项更改(将“if”语句更改为“ifnum”)后,修复了字母问题(...并添加了其他条目以制作更大的匹配示例),得到:

在此处输入图片描述

...至于参考文献中随机出现的“无序”问题,这与我的大文件存在问题有关,每当一个图无法放在一页纸上时,页码也会随机乱掉

相关内容