0

I need to do the following

\newcounter{problem\theenumi}

Clearly, enumi changes with time, but the counter name must be fixed NOW! So, for example, even after \end{enumerate} I should have access to counter problem1, problem2, etc. I played with \expandafter with no luck. How do I do this?

  • Welcome to TeX.SX! My impression is that you want to look at the enumitem package. – egreg Nov 19 '14 at 15:24
  • \newcounter should almost always be in the preamble of the document, why do you think you will need it in the scope where enumi changes? – David Carlisle Nov 19 '14 at 15:29
  • Why don;t you use \label and \ref? Or are you after resume feature of enumitem package? – user11232 Nov 19 '14 at 15:29
  • What for should I use \label and \ref? Neither I work with enumitem package. My task is to count the points associated with each \item: \item text \pts{5} text \pts{3} text \pts{8} \item text \pts{7} .... I just want to generate counter names automatically for each item. More over it is related to totcount packange as I need result from the previous compilation run. – user66512 Nov 19 '14 at 15:34
  • tex has a fixed number of count registers (256 in classic tex, more in etex) it is almost always wrong to dynamically allocate them you will run out. – David Carlisle Nov 19 '14 at 16:27
2

Here is a guess of what you might be after:

enter image description here

\documentclass{article}

\makeatletter
\newcommand{\pts}[1]{%
  \expandafter\providecommand\csname problem\theenumi\endcsname{0}% Define problem count
  \expandafter\expandafter\expandafter
    \renewcommand\expandafter\csname\expandafter problem\expandafter\theenumi\expandafter\endcsname\expandafter{%
    \number\numexpr\csname problem\theenumi\endcsname+#1% Sum points thus far
  }%
  (#1~points)%
}
\newcommand{\totalpoints}{Total points: \csname problem\theenumi\endcsname}% Print total points thus far
\makeatother

\begin{document}

\begin{enumerate}
  \item
  This is a question. \pts{5}

  This is another question. \pts{7}

  \totalpoints

  \item
  This is a new question. \pts{3}

  This is yet another question. \pts{12}

  A final question. \pts{23}

  \totalpoints
\end{enumerate}

Here is some discussion.

\begin{enumerate}
  \item
  This is a question. \pts{5}

  This is another question. \pts{7}

  This is a new question. \pts{3}

  This is yet another question. \pts{12}

  A final question. \pts{23}

  \totalpoints
\end{enumerate}

\end{document}

\pts{<points>} prints (<points> points), but also keeps a running tally of the points associated with the problem/\item numbered \theenumi.

Note: The definition of \csname problem\theenumi\endcsname is local to the environment in which \pts is first called. So, if you have a nested enumeration, one should make the definition global - a minor change in code, but it's required.

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