# Extracting text from variable

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]{%
\stepcounter{a}%
\newcommand\getans[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

• 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

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]{%

\stepcounter{a}%

\newcommand\getans[1]{%

\begin{document}

\getans{1}

\getans{2}
\end{document}


EDIT

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

\makeatletter

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