8

This rather small document

\documentclass{article}

\usepackage{calculator}

\newcommand{\myPerCent}[2]{%
\DIVIDE{#1}{#2}{\sol}%
\MULTIPLY{\sol}{100}{\sol}%
\ROUND{\sol}{\sol}%
\sol%
}

\begin{document}
Computed \myPerCent{2441}{8667}.\par
\end{document}

produces this warning (I guess it's a warning and not an error as the PDF is finally built):

(\end occurred when \ifdim on line 13 was incomplete)

I have no clue how to solve the issue as the code looks ok at first glance, but it seems something is off here.

How can I use the package in a proper way?

(I'm quite happy I was able to identify the source for the warnings at least as the original document is rather large and I started looking into things like How to diagnose unclosed (incomplete) \iftrue or \ifnum? but somehow I didn't get me anywhere and I don't seem to know anything more than before...)

5
  • 3
    Do you need to use this package? Recent LaTeX has a floating point engine (l3fp) in the kernel... Commented yesterday
  • Looks like a package bug to me Commented yesterday
  • 2
    @JosephWright Yep, the internal macro \@@@ROUND is missing a \fi. Kind of pure luck that it "works". Commented yesterday
  • @campa Thanks for double-checking. At least I know now, it was not me breaking something. Commented yesterday
  • @Rmano I don't need this package at all. It was just what I came up with... Thanks to your solution I was able to happily remove it. Commented yesterday

3 Answers 3

10

If you can avoid using calculator, now floating point is included in modern LaTeX kernels, and siunitx is the gold standard for typesetting quantities:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{siunitx}% for \qty
\NewDocumentCommand{\myPerCent}{O{2} m m}{%
    \qty[round-precision=#1, round-mode=places]{\fpeval{#2/#3*100}}{\percent}%
}
\begin{document}
    Computed \myPerCent{2441}{8667}.\par
    Computed \myPerCent[0]{2441}{8667}.\par
\end{document}

two lines with 28.16% and 28%

1
  • Thanks. Perfect! Works like a charm. (Previously I used numprint for the formatted output but omitted it for the sake of brevity. Now it's all include, great!) Commented yesterday
6

The code of calculator is badly written and the author missed a \fi matching the outer \ifcase in \@@@ROUND.

% Source - https://tex.stackexchange.com/q/760261
% Posted by ViToni, modified by community. See post 'Timeline' for change history
% Retrieved 2026-02-27, License - CC BY-SA 4.0

\documentclass{article}

\usepackage{calculator}
\usepackage{xpatch}

\makeatletter
\xapptocmd{\@@@ROUND}{\fi}{}{}
\makeatother


\newcommand{\myPerCent}[2]{%
  \DIVIDE{#1}{#2}{\sol}%
  \MULTIPLY{\sol}{100}{\sol}%
  \ROUND[2]{\sol}{\sol}%
  \sol
}

\begin{document}

Computed \myPerCent{2441}{8667}.

\end{document}

No warning.

output

Compare the code in the package with the following, where indentation clearly shows the nesting of conditionals.

\def\@@@ROUND#1.#2#3#4#5#6.#7)[#8]#9{%
  \ifcase #8
    \COPY{#1}{#9}%
    \ifnum #2>4
      \ADD{#1}{1}{\cctr@tempp}%
      \COPY{\cctr@tempp}{#9}%
    \fi
  \or
    \COPY{#1.#2}{#9}%
    \ifnum #3>4
      \ADD{#2}{1}\cctr@tempq
      \COPY{#1}{\cctr@tempp}%
      \ifnum\cctr@tempq=10
        \ADD{\cctr@tempp}{1}\cctr@tempp
        \COPY{0}{\cctr@tempq}%
      \fi
      \COPY{\cctr@tempp.\cctr@tempq}{#9}%
    \fi
  \or
    \COPY{#1.#2#3}{#9}%
    \ifnum #4>4
      \ADD{#3}1\cctr@tempq
      \COPY{#2}{\cctr@tempp}%
      \COPY{#1}{\cctr@tempo}%
      \ifnum\cctr@tempq=10
        \ADD{\cctr@tempp}{1}{\cctr@tempp}%
        \COPY{0}{\cctr@tempq}%
      \fi
      \ifnum\cctr@tempp=10
        \ADD{\cctr@tempo}{1}\cctr@tempo
        \COPY{0}{\cctr@tempp}%
      \fi
      \COPY{\cctr@tempo.\cctr@tempp\cctr@tempq}{#9}%
    \fi
  \or
    \COPY{#1.#2#3#4}{#9}%
    \ifnum #5>4
      \ADD{#4}1\cctr@tempq
      \COPY{#3}{\cctr@tempp}%
      \COPY{#2}{\cctr@tempo}%
      \COPY{#1}{\cctr@tempn}%
      \ifnum\cctr@tempq=10
        \ADD{\cctr@tempp}{1}{\cctr@tempp}%
        \COPY{0}{\cctr@tempq}%
      \fi
      \ifnum\cctr@tempp=10
        \ADD{\cctr@tempo}{1}\cctr@tempo
        \COPY{0}{\cctr@tempp}%
      \fi
      \ifnum\cctr@tempo=10
        \ADD{\cctr@tempn}{1}\cctr@tempn
        \COPY{0}{\cctr@tempo}
      \fi
      \COPY{\cctr@tempn.\cctr@tempo\cctr@tempp\cctr@tempq}{#9}%
    \fi
  \or
    \COPY{#1.#2#3#4#5}{#9}%
    \ifnum #6>4
      \ADD{#5}1\cctr@tempq
      \COPY{#4}{\cctr@tempp}%
      \COPY{#3}{\cctr@tempo}%
      \COPY{#2}{\cctr@tempn}%
      \COPY{#1}{\cctr@tempm}%
      \ifnum\cctr@tempq=10
        \ADD{\cctr@tempp}{1}{\cctr@tempp}%
        \COPY{0}{\cctr@tempq}%
      \fi
      \ifnum\cctr@tempp=10
        \ADD{\cctr@tempo}{1}\cctr@tempo\COPY{0}{\cctr@tempp}%
      \fi
      \ifnum\cctr@tempo=10
        \ADD{\cctr@tempn}{1}\cctr@tempn
        \COPY{0}{\cctr@tempo}%
      \fi
      \ifnum\cctr@tempn=10
        \ADD{\cctr@tempm}{1}\cctr@tempm
        \COPY{0}{\cctr@tempn}%
      \fi
      \COPY{\cctr@tempm.\cctr@tempn\cctr@tempo\cctr@tempp\cctr@tempq}{#9}%
    \fi
  \fi % <------- MISSING!
}
1
  • Thanks for the example with xpath. It might be useful if I encounter a problem somewhere else. Commented 11 hours ago
5

For the sake of variety, here's a LuaLaTeX-based solution. It sets up a user macro called \myPerCent which takes 2 mandatory arguments -- the numerator and denominator terms of the ratio to be computed -- as well as 1 optional argument -- the desired precision or number of significant digits.

Two remarks about this solution:

  • The macro's default number of significant digits is 3. This can be overridden by specifying an optional argument, enclosed in square brackets, ahead of the 2 mandatory arguments. See the sample code below for details.

  • The 2 mandatory arguments of \myPerCent don't have to be numbers. They can also be expressions that to evaluate to numbers using standard math and LaTeX syntax rules.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{luacode} % for '\luaexec' macro
\newcommand{\myPerCent}[3][3]{\luaexec{%
   tex.sprint( 100 * string.format("\%.#1g" , (#2)/(#3)) )
   }\,\%}
   
\begin{document}

\myPerCent{2441}{8667}, \myPerCent[6]{2441}{8667}.

\myPerCent[1]{10/2}{2*3}, \myPerCent[5]{10/2}{2*3}.

\def\myten{10}\def\myfive{5} \myPerCent{\myfive}{\myten}.

\end{document}

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.