GeneralHow can I save and use the value of a counter?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
kimsg
Posts: 3
Joined: Mon Sep 28, 2009 7:15 am

How can I save and use the value of a counter?

Post by kimsg »

Hi, I am a new member to this forum. I have a tricky question. How can I save the value of a counter in order to use it BEFORE it is defined? It is like the combination of \label{key} and \ref{key} for equation. The only difference is that I am interested in saving and using the value of a variable that will be determined at teh end of the document.

Here is an example that illustrates my goal:

Code: Select all

\documentclass[a4paper,12pt]{report}

\newcounter{totalpoints}
\setcounter{totalpoints}{0}
\newcounter{question}
\setcounter{question}{0}
\newenvironment{question}[1]%
{%begindef
  \refstepcounter{question}%
  \addtocounter{totalpoints}{#1}%
  \fbox{\textbf{\thequestion}} [#1\ points]%
}%
{%enddef
}

\begin{document}

Max points: \maxpoints \\

\begin{question}{10}
  What is your pet's name?
\end{question}

\begin{question}{30}
  What is your mother's maiden name?
\end{question}\\

\newcommand{\maxpoints}{\thetotalpoints}
Max points: \maxpoints \\

\end{document}
Of course, the code does not compile as is because \maxpoints is not defined in the beginning. I am willing to run LaTeX multiple times, but I want to let the code determine the max points. Any suggestion will be greatly appreciated.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

fatra2
Posts: 126
Joined: Fri May 01, 2009 1:43 pm

Re: How can I save and use the value of a counter?

Post by fatra2 »

Hi there,

From what I know, if LaTeX can number your item/list or whatever whatelse you want to number, you can make a label and refer to it normally. Whether it officially comes before in your text. You might have to compile more than once, but you should be able to get a good result.

Now if you number it by yourself, and a bit randomly, you will have to trick the LaTeX compiler to think you are sectioning your text. For this, I would suggest looking at the \phantomsection option that you can label, and refer to normally.

Cheers
kimsg
Posts: 3
Joined: Mon Sep 28, 2009 7:15 am

How can I save and use the value of a counter?

Post by kimsg »

fatra2 wrote:Hi there,

From what I know, if LaTeX can number your item/list or whatever whatelse you want to number, you can make a label and refer to it normally. Whether it officially comes before in your text. You might have to compile more than once, but you should be able to get a good result.

Now if you number it by yourself, and a bit randomly, you will have to trick the LaTeX compiler to think you are sectioning your text. For this, I would suggest looking at the \phantomsection option that you can label, and refer to normally.

Cheers
Dear fatra2,

Thank you for a valuable suggestion. It seems to be promising. However, I don't need the line number at arbitrary location. What I need is to refer the value of a counter that will be determined later. Would you show me how to do this using \phantomsection?

Thanks.
kimsg
Posts: 3
Joined: Mon Sep 28, 2009 7:15 am

How can I save and use the value of a counter?

Post by kimsg »

I found a solution. The totcount package (http://ctan.org/pkg/totcount) does exactly what I want.

Thanks for your help.
Post Reply