2

I have defined a \newcommand function like into this code:

\documentclass{scrartcl}
\usepackage{pbox,calc}

\newcounter{pointsExo}
\setcounter{pointsExo}{4}
\newcounter{pointsSousTotal}
\setcounter{pointsSousTotal}{0}

\newcounter{souspointsExo}
\newcommand{\sousPoints}[1]{
    \setcounter{souspointsExo}{#1}
    \setcounter{pointsSousTotal}{\value{pointsSousTotal} + #1}
    {\scriptsize{(#1~\ifthenelse{#1<2}{point}{points}}/\arabic{pointsExo})}
}

\newcommand{\subexo}[2]{
    #1\hfill\sousPoints{#2}% Points
    \medskip
}
\begin{document}
\subexo{Anything}{2}

\subexo{Something}{0.5}
\end{document}

I would like to be able to call the command with both real or integer numbers:

\subexo{Anything}{2}

or

\subexo{Something}{0.5}

But I have errors like this with my current code:

! Package calc Error: `.' invalid at this point.

What should I modify into my \newcommand definition to do this ?

4
  • 1
    As far as I know, counters can assume only integer values. Please take a look on the calculator package
    – user31729
    Commented Apr 15, 2014 at 11:19
  • 1
    You might want to take a look at class exam or package exsheets. Looks like you are trying to achieve some kind of exam paper.
    – Johannes_B
    Commented Apr 15, 2014 at 11:27
  • There's some inconsistency in your question: the \sousPoints command is defined as having only one argument, and you use it with two: \sousPoints{Anything}{2}.
    – Bernard
    Commented Apr 15, 2014 at 12:08
  • @Johannes_B, yes, I will give a look at these packages. Thank you.
    – lauhub
    Commented Apr 15, 2014 at 16:13

1 Answer 1

3

Counters can only contain integer numbers. If you want to deal with real numbers you have to play with lengths.

The following MWE uses lengths instead of counters (the internal macro \strip@pt is used to strip pt when printing the length):

\documentclass{scrartcl}
\usepackage{pbox,calc}

\makeatletter
\newcommand*{\strippt}[1]{\strip@pt#1}
\makeatother

\newlength{\pointsExo}
\setlength{\pointsExo}{4pt}
\newlength{\pointsSousTotal}
\setlength{\pointsSousTotal}{0pt}

\newlength{\souspointsExo}
\newcommand{\sousPoints}[1]{
    \setlength{\souspointsExo}{#1pt}
    \setlength{\pointsSousTotal}{\pointsSousTotal + #1pt}
    {\scriptsize{(#1~\ifdim#1pt=1pt point\else points\fi/\strippt\pointsExo)}}
}

\newcommand{\subexo}[2]{
    #1\hfill\sousPoints{#2}% Points
    \medskip
}
\begin{document}
\subexo{Anything}{2}

\subexo{Something}{0.5}
\end{document} 

Output:

enter image description here

2
  • Thanks ! one more question: does all length need to be prefixed with \ ? Should I add a \ to all my counters ?
    – lauhub
    Commented Apr 15, 2014 at 18:16
  • @lauhub Yes, lengths are like commands and need a \ before. Commented Apr 15, 2014 at 19:06

You must log in to answer this question.

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