1

I am trying to work out a special document that through a seed generates a test for my students. I have gotten quite alot to work but I have the issue that when I try to add my answers into an array (which I use to store the answers to be printed on a seperate sheet for myself).

\newcommand\setans[2]{%
\csdef{answer#1}{#2}}
\newcommand\addans[1]{%
\stepcounter{a}%
\csdef{answer\thea}{#1}}
\newcommand\getans[1]{%
\csuse{answer#1}}

This is what I use for my array and I store things in the manner of

\addans{\answer}

the issue is of course due to my algorithms the \answer variable changes along the line so when it comes to the end of the document it will just display for all positions the last \answer's value, is there a way to extract the text of \answer that I then use on \addans, or another way to go about it?

Best regards

  • 2
    please always post complete documents that demonstrate the problem, your fragments use commands not defined by default which makes it hard to comment. – David Carlisle Nov 18 '14 at 18:53
  • Welcome to TeX.SE. Reiterating David's comment, while code snippets are useful in explanations, it is always best to composd a fully compilable MWE including \documentclass and the appropriate packages that sets up the problem. While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem. In this specific case it would go a long way towards clearing up any confusion as to your intent. – Peter Grill Nov 19 '14 at 3:09
  • I'll keep it in mind for the next problem, my apologies. – Zelos Malum Nov 19 '14 at 5:59
3

Behold my psychic power! Use \cslet to set \answer1 etc. to the current definition of \answer, not answer itself.

\documentclass{article}
\usepackage{etoolbox}
\newcounter{a}
\setcounter{a}{0}

\newcommand\setans[2]{%
  \csdef{answer#1}{#2}}

\newcommand\addans[1]{%
  \stepcounter{a}%
  \cslet{answer\thea}{#1}} %  <--- Not \csdef!

\newcommand\getans[1]{%
  \csuse{answer#1}}

\begin{document}

\def\answer{42}
\addans{\answer}
\def\answer{99}
\addans{\answer}

\getans{1}

\getans{2}
\end{document}

answers

EDIT

If you want do more than just \addans{\answer} you can try the following definition for \addans.

\makeatletter
\newcommand\addans[1]{%
  \stepcounter{a}%
  \edef\@tmp{#1}
  \cslet{answer\thea}\@tmp} %  <--- Not \csdef!
\makeatother

This will work for the example you gave (i.e. \addans{C=\answer}), but if you try anything too elaborate it will probably break (in which case you might need to ask a new question in order to gain the attention of someone like egreg or David Carlisle).

  • That works fantastic! But it seems to not be accepting text anymore, I try to make it have \addans{C=\answer} and it either stops including C= or \answer part or even both. – Zelos Malum Nov 19 '14 at 5:57
  • @ZelosMalum --- I have edited my answer. – Ian Thompson Nov 19 '14 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.