3

I'd like to have a counter, called ProblemPoints in the MWE, which is dependent on an environment problem, such that I can print the total points of each problem.

\documentclass{article}
\usepackage{totcount}
\newtotcounter{ProblemPoints}

\newenvironment{problem}
{
\setcounter{ProblemPoints}{0}
Problem: (\total{ProblemPoints} points)
}
{\vspace{1cm}}

\begin{document}
\begin{problem}
Question 1: (2 points)\addtocounter{ProblemPoints}{2}
Question 2: (2 points)\addtocounter{ProblemPoints}{2}
\end{problem}
\begin{problem}
Question 1: (5 points)\addtocounter{ProblemPoints}{5}
Question 2: (1 points)\addtocounter{ProblemPoints}{1}
\end{problem}
\end{document}

Here it just prints 6 (the total points of the last Problem) at each problem.

2
  • 2
    Welcome! Are you aware of the exam document class? BTW, you set the counter to 0 whenever you start the environment.
    – user121799
    Dec 9, 2017 at 18:13
  • I am now, thanks :) I'm still interested in a solution though, because I wanted to add this functionality to an already existing project which would require a lot of time to translate to the exam class.
    – W.Smith
    Dec 9, 2017 at 18:21

1 Answer 1

3

\total can only store one value. You can use the \label-\ref method.

\documentclass{article}

\newcounter{problem}
\newcounter{problempoints}
\renewcommand{\theproblem}{\theproblempoints}
\newcounter{question}

\newenvironment{problem}
 {%
  \par\addvspace{1cm}%
  \stepcounter{problem}%
  \setcounter{question}{0}%
  \setcounter{problempoints}{0}%
  Problem: (\ref{PB\arabic{problem}} points)\par
 }
 {%
  \addtocounter{problem}{-1}%
  \refstepcounter{problem}\label{PB\arabic{problem}}%
  \par\addvspace{1cm}%
 }
\newcommand{\question}[1]{%
  \par
  \stepcounter{question}%
  Question \thequestion: (#1 points)\addtocounter{problempoints}{#1}
}

\begin{document}
\begin{problem}
\question{2} What is $1+2$?
\question{2} What is $1+3$?
\end{problem}
\begin{problem}
\question{5} What is $100\cdot 100$?
\question{1} What is $2^4-4^2$?
\end{problem}
\end{document}

Each problem steps a counter for getting a label expanding to the final value of \theproblempoints.

I do \stepcounter at the beginning, then step back the counter and do \refstepcounter again, in order to set the current label at the last minute, so you can also use \label inside the environment.

enter image description here

2
  • Thanks, works perfectly. I'm not quite sure why you need both \newcounter{problem}and \newcounter{problempoints} if you are setting \renewcommand{\theproblem}{\theproblempoints}.
    – W.Smith
    Dec 9, 2017 at 18:43
  • @G.Basso When LaTeX performs \refstepcounter{problem}, it sets the current label to the expansion of \theproblem; so \label{whatever} just after the \refstepcounter command will make \ref{whatever} issue that value of \theproblem. In this case \label takes its argument built from the current value of problem.
    – egreg
    Dec 9, 2017 at 18:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .