# Strange combined behavior of newcounter and newcommand

I have very simple document, where I declare a counter and wand to add bold text for each list item as follow:

\documentclass[11pt]{article}
\usepackage{vmargin}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{epstopdf}
\usepackage{epsfig}
\usepackage{alltt}
\setpapersize{USletter}
\setmarginsrb{1in}{1.0in}{0.8in}{.8in}{0pt}{0mm}{0.25in}{7mm}
%problems start here
\newcounter{problem}
\newcounter{total}
\addtocounter{problem}{1} \noindent {\bf \theproblem.} ({\bf #1 point})}

\begin{document}

\question{0.5} lost in latex

\question{1.5}  lost in latex

\question{2.5}  lost in latex
\end{document}


Now everything works well is I use only whole integers as 1,2,3 etc

However, if I use decimals: 0.5, 1.5 etc, latex produces some weird output:

Any suggestion on how to fix that appreciated!

• Counters are for integers. You can't feed them decimals. Off-topic: \bf is several decades obsolete in LaTeX, epsfig is either obsolete or deprecated, epstopdf is almost certainly unneeded and vmargin should be avoided as it is very buggy and doesn't work properly.
– cfr
Jun 7, 2018 at 0:36
• Counters are supposed to be integers, so when you do \addtocounter{total}{0.5}, the remaining .5 is ignored by \addtocounter and typeset (and you probably get an error). Jun 7, 2018 at 0:36
• @PhelypeOleinik There's no error, though.
– cfr
Jun 7, 2018 at 0:38
• so do I need to pass to arguments or there is a way to make it auto? Jun 7, 2018 at 0:39
• @Califlower You can do it automatically. You just need to use something different to track the total. Using two arguments would get very messy, I would think.
– cfr
Jun 7, 2018 at 0:55

Counters are supposed to be integers, so when you do \addtocounter{total}{0.5}, the remaining .5 is ignored by \addtocounter and typeset (and you probably get an error).

\documentclass{article}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{alltt}
\usepackage[letterpaper,left=1in,top=1in,right=.8in,bottom=.8in]{geometry}
\setlength{\footskip}{7mm}

\newcounter{problem}
\newlength{\total}
\newcommand{\question}[1]{%
\addtocounter{problem}{1}\noindent {\bfseries \theproblem.} ({\bfseries #1 point})}
\makeatletter
\newcommand{\printtotal}{{\bfseries Total: \strip@pt\total}}
\makeatother

\begin{document}

\question{0.5} Getting along with \LaTeX

\question{1.5} Getting along with \LaTeX

\question{2.5} Getting along with \LaTeX

\printtotal

\end{document}


I also replaced vmargin by geometry and removed epsfig and epstopdf.

Here's a partially expl3 solution. I've not changed bits which work fine in the original code (unless obsolete). This means the syntax is a mix of 2e and 3, but I hope this makes clearer what's doing the work of tracking the total.

\documentclass[11pt]{article}
\usepackage{expl3}
\newcounter{problem}
\ExplSyntaxOn
\fp_new:N \l_califlower_total_fp
\fp_set:Nn \l_califlower_total_fp { 0 }
\newcommand{\question}[1]{
\stepcounter{problem}\noindent\textbf{\theproblem. ~ (#1 ~ point)} ~ Total: ~ \fp_to_decimal:N \l_califlower_total_fp}
\ExplSyntaxOff
\begin{document}

\question{0.5} lost in latex

\question{1.5}  lost in latex

\question{2.5}  lost in latex
\end{document}


As the comments say, LaTeX counters accept only integer values. One way around this is to use a \newdim to create a counter, but then there is some trickery required to print the dimension using \strip@pt.

Here a full MWE:

\documentclass[11pt]{article}
\usepackage{vmargin}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{epstopdf}
\usepackage{epsfig}
\usepackage{alltt}
\usepackage{pgfmath}
\setpapersize{USletter}
\setmarginsrb{1in}{1.0in}{0.8in}{.8in}{0pt}{0mm}{0.25in}{7mm}
%problems start here
\newcounter{problem}
\newdimen\total
\makeatletter
\newcommand\printtotal{\strip@pt\total}
\makeatother
\newcommand{\question}[1]{
\total=\dimexpr\total+#1pt\relax%
\refstepcounter{problem}%
\noindent \textbf{\theproblem. (#1 point)}%
}

\begin{document}

\question{0.5} lost in latex

\question{1.5}  lost in latex

\question{2.5}  lost in latex

Total: \printtotal
\end{document}


The output is:

Note that I have used \refstepcounter to increment the problem counter. This is better because you can then use \label{...} and \ref{...} commands with the problem numbers. As suggested in the comments, I have also used \textbf{...}.