# Use csdef variables before defining them

I'm trying to create a grade table. This is a follow up question to Create an array of variables. The issue is that the grading table sits below the questions. The way I want is that the questions should appear after the grading table.However, if the grading table is at the top, I will have not called any csdef commands and I won't have any output. (I've tried and this is true, of course.) So, how can I put the grading table before the actual questions.

p.s. I've tried totcount package and replaced csdefs with \newtotcounter but it gives huge number of errors which I have no clue about.

My MWE is here:

\documentclass{article}
\usepackage{etoolbox}

\newcounter{cnt}

\stepcounter{cnt}%
\csdef{text\thecnt}{#1}}

\newcommand\gettext[1]{%
\csuse{text#1}}

\begin{document}

Question 1: Blah.

Question 2: Blah.

Question 3: Blah.

\begin{tabular}{ |l|c@{} *{\thecnt}{c|} } % the first row is hidden
\hline
\newcounter{colnum}%
\setcounter{colnum}{0}%
Q. No.:\hfill &
\whileboolexpr
{ test {\ifnumcomp{\value{colnum}}{<}{\thecnt}} }%
{&\stepcounter{colnum}\thecolnum}\\
\hline
Score: &
\setcounter{colnum}{0}
\whileboolexpr
{ test {\ifnumcomp{\value{colnum}}{<}{\thecnt}} }%
{&\stepcounter{colnum}\hspace{1cm}}\\
\hline
Total: &
\setcounter{colnum}{0}
\whileboolexpr
{ test {\ifnumcomp{\value{colnum}}{<}{\thecnt}} }%
{&\stepcounter{colnum}\gettext{\thecolnum}}\\
\hline
\end{tabular}
\end{document}

-
You could put the questions in saveboxes and then use these after the table. As far as I know you can't have paragraph constructs in a savebox though, so you would need one for every question. Perhaps there is an easier way? –  Roelof Spijker Jan 18 '12 at 10:25
There is, and TeX.SX found it :) –  recluze Jan 18 '12 at 11:31

You can abuse the \label-\ref mechanism: the \addtext macro defines \@currentlabel as the grade for the exercise and issues a \label command that \gettext will retrieve at the next run. At end document we store also the final number of exercises. In order to avoid problems when the .aux file hasn't yet the information, we set \finalnumber to 2 if \getrefnumber{lastcnt} returns 0.

\documentclass{article}
\usepackage{etoolbox,refcount}

\newcounter{cnt}
\AtEndDocument{\refstepcounter{cnt}\label{lastcnt}}

\makeatletter
\stepcounter{cnt}%
\def\@currentlabel{#1}%
\label{text\thecnt}}
\makeatother

\newcommand\gettext[1]{\getrefnumber{text#1}}

\newcounter{colnum}

\begin{document}

\ifnum\getrefnumber{lastcnt}=0
\def\finalnumber{2}%
\else
\edef\finalnumber{\number\numexpr\getrefnumber{lastcnt}-1\relax}%
\fi

\begin{tabular}{ |l|c@{} *{\finalnumber}{c|} } % the first row is hidden
\hline
\setcounter{colnum}{0}%
Q. No.:\hfill &
\whileboolexpr
{ test {\ifnumcomp{\value{colnum}}{<}{\finalnumber}} }%
{&\stepcounter{colnum}\thecolnum}\\
\hline
Score: &
\setcounter{colnum}{0}
\whileboolexpr
{ test {\ifnumcomp{\value{colnum}}{<}{\finalnumber}} }%
{&\stepcounter{colnum}\hspace{1cm}}\\
\hline
Total: &
\setcounter{colnum}{0}
\whileboolexpr
{ test {\ifnumcomp{\value{colnum}}{<}{\finalnumber}} }%
{&\stepcounter{colnum}\gettext{\thecolnum}}\\
\hline
\end{tabular}

Question 1: Blah.