Skip to main content
3 of 3
better explanations
Mico
  • 556.7k
  • 57
  • 760
  • 1.3k

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}
Mico
  • 556.7k
  • 57
  • 760
  • 1.3k