Use ifthenelse{equal{ on a number called from reference












2















I want to store an answer key for an exam and then automatically move the multiple choice answer choices around for each question match its predetermined answer. (I need to conform my multiple choice exam to a predetermined answer key in order to use these cool scratch-off scantrons called "IF-AT" scantrons).



I have a partial solution (see MWE at bottom) based one three steps.




  1. Store the answers for the exam in an array using arrayjob.sty, 1234 meaning ABCD respectively. For example, usepackage{arrayjob} newarrayanswerkey readarray{answerkey}{3&4} records the answer to Q1 as C and the answer to Q2 as D.

  2. Then arabic{question} gives you the question number, and so answerkey(arabic{question}) gives you that previously stored answer for that question number.

  3. Use ifthenelse to execute different behavior based on that number answerkey(arabic{question}).


However, I can't figure out how to get ifthenelse to read answerkey(arabic{question}). In other words,



ifthenelse{equal{1}{answerkey(arabic{question})}}{some code}{}


doesn't work.
I think it's a matter of expanding macros at the right time.



In the MWE below I commented out the code testing arrayjob.sty + ifthen.sty because it fails to compile.



For good measure, I also tried replacing the two packages with pgfmath.sty and fp.sty respectively (Qs 6, 7, 8 in MWE below), but no combination compiles.



documentclass{exam}
usepackage{ifthen, fp, arrayjob, pgfmath}
newarrayanswerkey %arrayjob
readarray{answerkey}{4&3&2&1&1&1&1&1}
defanswerkeytwo{{{},4,3,2,1,1,1,1,1}} %pgfmath
begin{document}
begin{questions}
question testing arrayjob...
answerkey(arabic{question})
question testing pgf...
pgfmathparse{answerkeytwo[arabic{question}]}pgfmathresult
question testing ifthen...
ifthenelse{equal 2 2}{itbetrue}{itbefalse}.
question testing fp...
FPifeq 1 1 itbetrueelse itbefalsefi.
question testing arrayjob + ifthen... (should print itbetrue)
%ifthenelse{equal 1 {
% answerkey(arabic{question})
% }}{itbetrue}{itbefalse}.
question testing arrayjob + fp... (should print itbetrue)
%FPifeq 1 {
% answerkeyone(arabic{question})
% } itbetrue else itbefalse fi.
question testing pgfmath + ifthen... (should print itbetrue)
%ifthenelse{equal 1 {
% pgfmathparse{answerkeytwo[arabic{question}]}pgfmathresult
% }}{itbetrue}{itbefalse}.
question testing pgfmath + fp... (should print itbetrue)
%FPifeq 1 {
% pgfmathparse{answerkeytwo[arabic{question}]}pgfmathresult
% } itbetrueelse itbefalsefi.
end{questions}
end{document}


enter image description here



So how do I get answerkey(arabic{question})} to expand early enough for ifthenelse{equal{1}{answerkey(arabic{question})}}}{}{} to work?










share|improve this question



























    2















    I want to store an answer key for an exam and then automatically move the multiple choice answer choices around for each question match its predetermined answer. (I need to conform my multiple choice exam to a predetermined answer key in order to use these cool scratch-off scantrons called "IF-AT" scantrons).



    I have a partial solution (see MWE at bottom) based one three steps.




    1. Store the answers for the exam in an array using arrayjob.sty, 1234 meaning ABCD respectively. For example, usepackage{arrayjob} newarrayanswerkey readarray{answerkey}{3&4} records the answer to Q1 as C and the answer to Q2 as D.

    2. Then arabic{question} gives you the question number, and so answerkey(arabic{question}) gives you that previously stored answer for that question number.

    3. Use ifthenelse to execute different behavior based on that number answerkey(arabic{question}).


    However, I can't figure out how to get ifthenelse to read answerkey(arabic{question}). In other words,



    ifthenelse{equal{1}{answerkey(arabic{question})}}{some code}{}


    doesn't work.
    I think it's a matter of expanding macros at the right time.



    In the MWE below I commented out the code testing arrayjob.sty + ifthen.sty because it fails to compile.



    For good measure, I also tried replacing the two packages with pgfmath.sty and fp.sty respectively (Qs 6, 7, 8 in MWE below), but no combination compiles.



    documentclass{exam}
    usepackage{ifthen, fp, arrayjob, pgfmath}
    newarrayanswerkey %arrayjob
    readarray{answerkey}{4&3&2&1&1&1&1&1}
    defanswerkeytwo{{{},4,3,2,1,1,1,1,1}} %pgfmath
    begin{document}
    begin{questions}
    question testing arrayjob...
    answerkey(arabic{question})
    question testing pgf...
    pgfmathparse{answerkeytwo[arabic{question}]}pgfmathresult
    question testing ifthen...
    ifthenelse{equal 2 2}{itbetrue}{itbefalse}.
    question testing fp...
    FPifeq 1 1 itbetrueelse itbefalsefi.
    question testing arrayjob + ifthen... (should print itbetrue)
    %ifthenelse{equal 1 {
    % answerkey(arabic{question})
    % }}{itbetrue}{itbefalse}.
    question testing arrayjob + fp... (should print itbetrue)
    %FPifeq 1 {
    % answerkeyone(arabic{question})
    % } itbetrue else itbefalse fi.
    question testing pgfmath + ifthen... (should print itbetrue)
    %ifthenelse{equal 1 {
    % pgfmathparse{answerkeytwo[arabic{question}]}pgfmathresult
    % }}{itbetrue}{itbefalse}.
    question testing pgfmath + fp... (should print itbetrue)
    %FPifeq 1 {
    % pgfmathparse{answerkeytwo[arabic{question}]}pgfmathresult
    % } itbetrueelse itbefalsefi.
    end{questions}
    end{document}


    enter image description here



    So how do I get answerkey(arabic{question})} to expand early enough for ifthenelse{equal{1}{answerkey(arabic{question})}}}{}{} to work?










    share|improve this question

























      2












      2








      2








      I want to store an answer key for an exam and then automatically move the multiple choice answer choices around for each question match its predetermined answer. (I need to conform my multiple choice exam to a predetermined answer key in order to use these cool scratch-off scantrons called "IF-AT" scantrons).



      I have a partial solution (see MWE at bottom) based one three steps.




      1. Store the answers for the exam in an array using arrayjob.sty, 1234 meaning ABCD respectively. For example, usepackage{arrayjob} newarrayanswerkey readarray{answerkey}{3&4} records the answer to Q1 as C and the answer to Q2 as D.

      2. Then arabic{question} gives you the question number, and so answerkey(arabic{question}) gives you that previously stored answer for that question number.

      3. Use ifthenelse to execute different behavior based on that number answerkey(arabic{question}).


      However, I can't figure out how to get ifthenelse to read answerkey(arabic{question}). In other words,



      ifthenelse{equal{1}{answerkey(arabic{question})}}{some code}{}


      doesn't work.
      I think it's a matter of expanding macros at the right time.



      In the MWE below I commented out the code testing arrayjob.sty + ifthen.sty because it fails to compile.



      For good measure, I also tried replacing the two packages with pgfmath.sty and fp.sty respectively (Qs 6, 7, 8 in MWE below), but no combination compiles.



      documentclass{exam}
      usepackage{ifthen, fp, arrayjob, pgfmath}
      newarrayanswerkey %arrayjob
      readarray{answerkey}{4&3&2&1&1&1&1&1}
      defanswerkeytwo{{{},4,3,2,1,1,1,1,1}} %pgfmath
      begin{document}
      begin{questions}
      question testing arrayjob...
      answerkey(arabic{question})
      question testing pgf...
      pgfmathparse{answerkeytwo[arabic{question}]}pgfmathresult
      question testing ifthen...
      ifthenelse{equal 2 2}{itbetrue}{itbefalse}.
      question testing fp...
      FPifeq 1 1 itbetrueelse itbefalsefi.
      question testing arrayjob + ifthen... (should print itbetrue)
      %ifthenelse{equal 1 {
      % answerkey(arabic{question})
      % }}{itbetrue}{itbefalse}.
      question testing arrayjob + fp... (should print itbetrue)
      %FPifeq 1 {
      % answerkeyone(arabic{question})
      % } itbetrue else itbefalse fi.
      question testing pgfmath + ifthen... (should print itbetrue)
      %ifthenelse{equal 1 {
      % pgfmathparse{answerkeytwo[arabic{question}]}pgfmathresult
      % }}{itbetrue}{itbefalse}.
      question testing pgfmath + fp... (should print itbetrue)
      %FPifeq 1 {
      % pgfmathparse{answerkeytwo[arabic{question}]}pgfmathresult
      % } itbetrueelse itbefalsefi.
      end{questions}
      end{document}


      enter image description here



      So how do I get answerkey(arabic{question})} to expand early enough for ifthenelse{equal{1}{answerkey(arabic{question})}}}{}{} to work?










      share|improve this question














      I want to store an answer key for an exam and then automatically move the multiple choice answer choices around for each question match its predetermined answer. (I need to conform my multiple choice exam to a predetermined answer key in order to use these cool scratch-off scantrons called "IF-AT" scantrons).



      I have a partial solution (see MWE at bottom) based one three steps.




      1. Store the answers for the exam in an array using arrayjob.sty, 1234 meaning ABCD respectively. For example, usepackage{arrayjob} newarrayanswerkey readarray{answerkey}{3&4} records the answer to Q1 as C and the answer to Q2 as D.

      2. Then arabic{question} gives you the question number, and so answerkey(arabic{question}) gives you that previously stored answer for that question number.

      3. Use ifthenelse to execute different behavior based on that number answerkey(arabic{question}).


      However, I can't figure out how to get ifthenelse to read answerkey(arabic{question}). In other words,



      ifthenelse{equal{1}{answerkey(arabic{question})}}{some code}{}


      doesn't work.
      I think it's a matter of expanding macros at the right time.



      In the MWE below I commented out the code testing arrayjob.sty + ifthen.sty because it fails to compile.



      For good measure, I also tried replacing the two packages with pgfmath.sty and fp.sty respectively (Qs 6, 7, 8 in MWE below), but no combination compiles.



      documentclass{exam}
      usepackage{ifthen, fp, arrayjob, pgfmath}
      newarrayanswerkey %arrayjob
      readarray{answerkey}{4&3&2&1&1&1&1&1}
      defanswerkeytwo{{{},4,3,2,1,1,1,1,1}} %pgfmath
      begin{document}
      begin{questions}
      question testing arrayjob...
      answerkey(arabic{question})
      question testing pgf...
      pgfmathparse{answerkeytwo[arabic{question}]}pgfmathresult
      question testing ifthen...
      ifthenelse{equal 2 2}{itbetrue}{itbefalse}.
      question testing fp...
      FPifeq 1 1 itbetrueelse itbefalsefi.
      question testing arrayjob + ifthen... (should print itbetrue)
      %ifthenelse{equal 1 {
      % answerkey(arabic{question})
      % }}{itbetrue}{itbefalse}.
      question testing arrayjob + fp... (should print itbetrue)
      %FPifeq 1 {
      % answerkeyone(arabic{question})
      % } itbetrue else itbefalse fi.
      question testing pgfmath + ifthen... (should print itbetrue)
      %ifthenelse{equal 1 {
      % pgfmathparse{answerkeytwo[arabic{question}]}pgfmathresult
      % }}{itbetrue}{itbefalse}.
      question testing pgfmath + fp... (should print itbetrue)
      %FPifeq 1 {
      % pgfmathparse{answerkeytwo[arabic{question}]}pgfmathresult
      % } itbetrueelse itbefalsefi.
      end{questions}
      end{document}


      enter image description here



      So how do I get answerkey(arabic{question})} to expand early enough for ifthenelse{equal{1}{answerkey(arabic{question})}}}{}{} to work?







      arrays pgfmath fp ifthenelse






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 1 hour ago









      Matt SMatt S

      505




      505






















          1 Answer
          1






          active

          oldest

          votes


















          0














          IMHO you could do everything with pgfmath and simple ifnum statements. (This does not mean the other options are bad.) I illustrate this with this example. The only thing I changed was to use pgfmathtruncatemaccro to make sure I get an integer that one can test with ifnum.



          documentclass{exam}
          usepackage{pgfmath}
          defanswerkeytwo{{{},4,3,2,1,1,1,1,1}} %pgfmath
          begin{document}
          begin{questions}
          question testing pgfmath + ifnumdots (should print itbefalse)
          pgfmathtruncatemacro{itest}{answerkeytwo[numbervalue{question}]}
          ifnumitest=1
          itbetrue
          else
          itbefalse
          fi
          question testing pgfmath + ifnumdots (should print itbefalse)
          pgfmathtruncatemacro{itest}{answerkeytwo[numbervalue{question}]}
          ifnumitest=1
          itbetrue
          else
          itbefalse
          fi
          question testingpgfmath + ifnumdots (should print itbefalse)
          pgfmathtruncatemacro{itest}{answerkeytwo[numbervalue{question}]}
          ifnumitest=1
          itbetrue
          else
          itbefalse
          fi
          question testing pgfmath + ifnumdots (should print itbetrue)
          pgfmathtruncatemacro{itest}{answerkeytwo[numbervalue{question}]}
          ifnumitest=1
          itbetrue
          else
          itbefalse
          fi
          end{questions}
          end{document}


          enter image description here






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "85"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f481261%2fuse-ifthenelse-equal-on-a-number-called-from-reference%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            IMHO you could do everything with pgfmath and simple ifnum statements. (This does not mean the other options are bad.) I illustrate this with this example. The only thing I changed was to use pgfmathtruncatemaccro to make sure I get an integer that one can test with ifnum.



            documentclass{exam}
            usepackage{pgfmath}
            defanswerkeytwo{{{},4,3,2,1,1,1,1,1}} %pgfmath
            begin{document}
            begin{questions}
            question testing pgfmath + ifnumdots (should print itbefalse)
            pgfmathtruncatemacro{itest}{answerkeytwo[numbervalue{question}]}
            ifnumitest=1
            itbetrue
            else
            itbefalse
            fi
            question testing pgfmath + ifnumdots (should print itbefalse)
            pgfmathtruncatemacro{itest}{answerkeytwo[numbervalue{question}]}
            ifnumitest=1
            itbetrue
            else
            itbefalse
            fi
            question testingpgfmath + ifnumdots (should print itbefalse)
            pgfmathtruncatemacro{itest}{answerkeytwo[numbervalue{question}]}
            ifnumitest=1
            itbetrue
            else
            itbefalse
            fi
            question testing pgfmath + ifnumdots (should print itbetrue)
            pgfmathtruncatemacro{itest}{answerkeytwo[numbervalue{question}]}
            ifnumitest=1
            itbetrue
            else
            itbefalse
            fi
            end{questions}
            end{document}


            enter image description here






            share|improve this answer




























              0














              IMHO you could do everything with pgfmath and simple ifnum statements. (This does not mean the other options are bad.) I illustrate this with this example. The only thing I changed was to use pgfmathtruncatemaccro to make sure I get an integer that one can test with ifnum.



              documentclass{exam}
              usepackage{pgfmath}
              defanswerkeytwo{{{},4,3,2,1,1,1,1,1}} %pgfmath
              begin{document}
              begin{questions}
              question testing pgfmath + ifnumdots (should print itbefalse)
              pgfmathtruncatemacro{itest}{answerkeytwo[numbervalue{question}]}
              ifnumitest=1
              itbetrue
              else
              itbefalse
              fi
              question testing pgfmath + ifnumdots (should print itbefalse)
              pgfmathtruncatemacro{itest}{answerkeytwo[numbervalue{question}]}
              ifnumitest=1
              itbetrue
              else
              itbefalse
              fi
              question testingpgfmath + ifnumdots (should print itbefalse)
              pgfmathtruncatemacro{itest}{answerkeytwo[numbervalue{question}]}
              ifnumitest=1
              itbetrue
              else
              itbefalse
              fi
              question testing pgfmath + ifnumdots (should print itbetrue)
              pgfmathtruncatemacro{itest}{answerkeytwo[numbervalue{question}]}
              ifnumitest=1
              itbetrue
              else
              itbefalse
              fi
              end{questions}
              end{document}


              enter image description here






              share|improve this answer


























                0












                0








                0







                IMHO you could do everything with pgfmath and simple ifnum statements. (This does not mean the other options are bad.) I illustrate this with this example. The only thing I changed was to use pgfmathtruncatemaccro to make sure I get an integer that one can test with ifnum.



                documentclass{exam}
                usepackage{pgfmath}
                defanswerkeytwo{{{},4,3,2,1,1,1,1,1}} %pgfmath
                begin{document}
                begin{questions}
                question testing pgfmath + ifnumdots (should print itbefalse)
                pgfmathtruncatemacro{itest}{answerkeytwo[numbervalue{question}]}
                ifnumitest=1
                itbetrue
                else
                itbefalse
                fi
                question testing pgfmath + ifnumdots (should print itbefalse)
                pgfmathtruncatemacro{itest}{answerkeytwo[numbervalue{question}]}
                ifnumitest=1
                itbetrue
                else
                itbefalse
                fi
                question testingpgfmath + ifnumdots (should print itbefalse)
                pgfmathtruncatemacro{itest}{answerkeytwo[numbervalue{question}]}
                ifnumitest=1
                itbetrue
                else
                itbefalse
                fi
                question testing pgfmath + ifnumdots (should print itbetrue)
                pgfmathtruncatemacro{itest}{answerkeytwo[numbervalue{question}]}
                ifnumitest=1
                itbetrue
                else
                itbefalse
                fi
                end{questions}
                end{document}


                enter image description here






                share|improve this answer













                IMHO you could do everything with pgfmath and simple ifnum statements. (This does not mean the other options are bad.) I illustrate this with this example. The only thing I changed was to use pgfmathtruncatemaccro to make sure I get an integer that one can test with ifnum.



                documentclass{exam}
                usepackage{pgfmath}
                defanswerkeytwo{{{},4,3,2,1,1,1,1,1}} %pgfmath
                begin{document}
                begin{questions}
                question testing pgfmath + ifnumdots (should print itbefalse)
                pgfmathtruncatemacro{itest}{answerkeytwo[numbervalue{question}]}
                ifnumitest=1
                itbetrue
                else
                itbefalse
                fi
                question testing pgfmath + ifnumdots (should print itbefalse)
                pgfmathtruncatemacro{itest}{answerkeytwo[numbervalue{question}]}
                ifnumitest=1
                itbetrue
                else
                itbefalse
                fi
                question testingpgfmath + ifnumdots (should print itbefalse)
                pgfmathtruncatemacro{itest}{answerkeytwo[numbervalue{question}]}
                ifnumitest=1
                itbetrue
                else
                itbefalse
                fi
                question testing pgfmath + ifnumdots (should print itbetrue)
                pgfmathtruncatemacro{itest}{answerkeytwo[numbervalue{question}]}
                ifnumitest=1
                itbetrue
                else
                itbefalse
                fi
                end{questions}
                end{document}


                enter image description here







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 1 hour ago









                marmotmarmot

                111k5138263




                111k5138263






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f481261%2fuse-ifthenelse-equal-on-a-number-called-from-reference%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Lallio

                    Unable to find Lightning Node

                    Futebolista