# [NTG-context] store counter or variable and reread again

Sat Dec 30 17:56:39 CET 2006

On Sat, 30 Dec 2006, Thomas Engel wrote:

> Hello,
>
> i just playing a little bit with macros.
> For adding points to a total sum i use this macros
>
> \global\newcount\summeP \summeP=0
> %   ad the points of each question
> %   show the points on the right side
> \def\showP[#1]{\inright{\hskip 10mm \framed{#1} }}
> %   handle the points
> \def\pkt[#1]%
>     {\showP[#1]%
>      \getPunkte[#1] }
>
> this is working well but my problem is I want to use \summeP on the
> first page to show the total amount of points for the whole exam.
>
> I know that this needs a second run of texexec, but I don't know how to
> store the value at the end of the file and reread this stored value in a
> second run the get the right value.

Here is how to do the second run. I also use ConTeXt macros
(\makecounter etc) instead of plain TeX. Be careful of spurious
linebreaks.

% It may be better to use \setcounter series of macros for managing counters.
% First let us define a general macro for adding arbitrary number to a counter

\unprotect
{\@EA\xdef\csname#1\endcsname{\the\numexpr\csname#1\endcsname+#2\relax}}
\protect
% To Hans and Taco: Should the above macro be added to syst-ext?

% Now lets define everything in terms of ConTeXt's counter macros

% \global\newcount\summeP

\makecounter{Points}
% I changed summeP to Points just to be consistent

% \summeP=0
% \makecounter sets the value of counter to 0.

% %   ad the points of each question
% This macro is already been defined using numexpr above.

% %   show the points on the right side
\def\showP[#1]{\inright{\hskip 10mm \framed{#1} }}
% %   handle the points
\def\pkt[#1]%
{\showP[#1]%
%      \getPunkte[#1] }
%

% Now lets set up the two pass mechanism

\def\nofPoints{0} % Number of points from previous run

\definetwopasslist {Points} % variable where two pass data is stored

% Need to execute this at the start of each file
\def\checkPoints
{\gettwopassdata{Points} % Retreive the two pass data
\iftwopassdatafound
\xdef \nofPoints {\twopassdata}
\fi
\global\let\checkproblems\relax}

% An example usage

\starttext
\checkPoints

\title{This exam is of \nofPoints\ Points}

% Lets set 10 problems with different points

\dorecurse{10}
{\pkt[\recurselevel] \input tufte \endgraf}

% Now the rest of the magic to take care of the two pass run
% Basically this says that rerun if \nofPoints !=
% \countervalue{Points}. In the next run we set \nofPoints to the
% previous value of \countervalue{Points} (the \checkPoints macro in
% the beginning), so things should be stable after two runs.
\savetwopassdata {Points} {\nofPoints} {\countervalue{Points}}

\stoptext

HTH,