Skip to main content
better explanations
Source Link
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 precision displayed bynumber of significant digits is \myPerCent3 is 3 significant digits. 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 two2 mandatory arguments of \myPerCent don't have to be numbers. All that's required isThey can also be expressions that theyto evaluate to numbers using standard math and LaTeX syntax rules.

enter image description hereenter 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[5]\myPerCent[6]{2441}{8667}.

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

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

\end{document}

For the sake of variety, a LuaLaTeX-based solution.

Two remarks about this solution:

  • The default precision displayed by \myPerCent is 3 significant digits. This can be overridden by specifying an optional argument, enclosed in square brackets.

  • The two mandatory arguments of \myPerCent don't have to be numbers. All that's required is that they evaluate to numbers using standard math 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[5]{2441}{8667}.

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

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}
added 27 characters in body
Source Link
Mico
  • 556.7k
  • 57
  • 760
  • 1.3k

For the sake of variety, a LuaLaTeX-based solution.

The default precision displayed by \myPerCent is 3 significant digits. This can be overridden by specifying an optional argument, enclosed in square brackets. WithTwo remarks about this approach, the two mandatory arguments of \myPerCent don't have to be numbers. All that's required is that they evaluate to numbers using standard math syntax rules.solution:

  • The default precision displayed by \myPerCent is 3 significant digits. This can be overridden by specifying an optional argument, enclosed in square brackets.

  • The two mandatory arguments of \myPerCent don't have to be numbers. All that's required is that they evaluate to numbers using standard math 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("\%.#1f"#1g",(#2)/(#3)) )
   }\,\%}
   
\begin{document}
\myPerCent{2441}{8667}, \myPerCent[5]{2441}{8667}.

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

For the sake of variety, a LuaLaTeX-based solution.

The default precision displayed by \myPerCent is 3 significant digits. This can be overridden by specifying an optional argument, enclosed in square brackets. With this approach, the two mandatory arguments of \myPerCent don't have to be numbers. All that's required is that they evaluate to numbers using standard math 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("\%.#1f",(#2)/(#3)) )
   }\,\%}
   
\begin{document}
\myPerCent{2441}{8667}, \myPerCent[5]{2441}{8667}.

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

For the sake of variety, a LuaLaTeX-based solution.

Two remarks about this solution:

  • The default precision displayed by \myPerCent is 3 significant digits. This can be overridden by specifying an optional argument, enclosed in square brackets.

  • The two mandatory arguments of \myPerCent don't have to be numbers. All that's required is that they evaluate to numbers using standard math 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[5]{2441}{8667}.

\myPerCent{10/2}{2*3},  \myPerCent[5]{10/2}{2*3}.
\end{document}
Source Link
Mico
  • 556.7k
  • 57
  • 760
  • 1.3k

For the sake of variety, a LuaLaTeX-based solution.

The default precision displayed by \myPerCent is 3 significant digits. This can be overridden by specifying an optional argument, enclosed in square brackets. With this approach, the two mandatory arguments of \myPerCent don't have to be numbers. All that's required is that they evaluate to numbers using standard math 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("\%.#1f",(#2)/(#3)) )
   }\,\%}
   
\begin{document}
\myPerCent{2441}{8667}, \myPerCent[5]{2441}{8667}.

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