4

This is similar to a previous question, but I didn't really understand the answer given. One possible use of what I'm asking is in the following code for a Matching style of question, where lines of code would want to be randomly interpreted for typesetting

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

which results in the following: This would have be an easy test, non?

  • Could please explain (in more detail, perhaps by means of an example) of what you expect the different outputs should look like? Do you want to just randomize the right column? What about the images on the left? – Werner Jul 19 '14 at 06:07
  • I'm thinking that both columns being randomized would be computationally redundant. Putting either column in random order would result in the "red answers" beside the images not being an exact ordered copy of the "text descriptions" on the right, as desired. – InstrumentGuy Jul 19 '14 at 06:22
  • I have since found a latex package that can accomplish this type of task for exam type questions (esami), but now I want to know how to do it for myself, and learn! I'm trying different approaches of putting each code line as an element into a list... but haven't got the syntax yet. – InstrumentGuy Jul 19 '14 at 06:29
  • Perhaps somebody could let me know if I'm attempting to solve this the wrong way... I'm trying to put each of the lines of code that I want shuffled into a '\pgfmathdeclarelist{myList}{{\label{LSNC} Limit Switch NC}{\label{LSNO} Limit Switch NO}}' which I would then randomly pick out items from list '\pgfmathgetlistlength{\1}{myList}' times with the first item being picked out is concatenated with "\foritem{a}" and the remaining picks concatenated with "\item". I cannot figure out how to put escape characters into the list items. – InstrumentGuy Jul 19 '14 at 13:03

1 Answers1

1

Almost got it... just need to get the counters working correctly. I shortened the example as well as replaced the actual images with text so that anyone can compile as is

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

It a bit messy, but results in: enter image description here

UPDATE... my short 3-element example only appeared to have worked. When I expanded it to a longer list all of the "red" answers were out of order...grrr! I would have removed this answer except for the fact that it solves my original post about randomized code. I just need to figure out a way of tracking labels/references through the randomization process

UDDATE #2... after making one change (changing the "if" statement to an "ifnum") fixed the lettering (...and added the other entries to make a larger matching example) problem to give:

enter image description here

... as for the random "out of order" problem with references, it is related to something wrong in my large file that also randomly messes up my page numbers everytime a figure doesn't fit on a page