3

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}
\newcommand{\question}[1]{\addtocounter{total}{#1}
\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

Good output

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

broken latex

Any suggestion on how to fix that appreciated!

  • 3
    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 '18 at 0:36
  • 2
    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). – Phelype Oleinik Jun 7 '18 at 0:36
  • @PhelypeOleinik There's no error, though. – cfr Jun 7 '18 at 0:38
  • so do I need to pass to arguments or there is a way to make it auto? – Califlower Jun 7 '18 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 '18 at 0:55
6

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).

You can use lengths instead:

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

\newcounter{problem}
\newlength{\total}
\newcommand{\question}[1]{%
  \addtolength{\total}{#1 pt}%
  \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}

enter image description here

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

7

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]{
  \fp_add:Nn \l_califlower_total_fp { #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}

keeping count of more than integers

5

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:

enter image description here

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{...}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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