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

3
  • 2
    please always post complete documents that demonstrate the problem, your fragments use commands not defined by default which makes it hard to comment. Nov 18, 2014 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. Nov 19, 2014 at 3:09
  • I'll keep it in mind for the next problem, my apologies. Nov 19, 2014 at 5:59

1 Answer 1

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).

2
  • 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. Nov 19, 2014 at 5:57
  • @ZelosMalum --- I have edited my answer. Nov 19, 2014 at 10:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .