Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}

\newcommand\addtext[1]{%
    \stepcounter{cnt}%
    \csdef{text\thecnt}{#1}}

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

\begin{document}

Question 1: Blah. 
\addtext{3}

Question 2: Blah.
\addtext{2}

Question 3: Blah.
\addtext{4}     

\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}
share|improve this question
    
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
add comment

1 Answer

up vote 1 down vote accepted

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
\newcommand\addtext[1]{%
    \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. 
\addtext{3}

Question 2: Blah.
\addtext{2}

Question 3: Blah.
\addtext{4}     

\end{document}
share|improve this answer
    
Great. I don't understand the description right now but it works perfectly. I will study the run in detail to understand what you're saying. It's not your lack of clarity; it's my inexperience. –  recluze Jan 18 '12 at 11:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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