2

I would like to define some variables in a tex document. For example, I write in .tex something similar as: Our tool succeeds to validate x samples out of all the y samples, thus its success ratio is z. Where z is defined as x/y, and x (resp. y) is instanced somewhere else by a number, eg., 30 (resp. 47). As a result, after the compilation the text turns out to be Our tool succeeds to validate 30 samples out of all the 47 samples, thus its success ratio is 63.8%.

The advantage of this, is that I just need to change the value of some variables, to change all the numbers in the text... The difficulty is that z holds a percentage number.

Does anyone know how to achieve this?

SoftTimur
  • 19,767

4 Answers4

5
\documentclass{article}

\newcommand\x{30}
\newcommand\y{47}


\newcommand\mypercent[2]{%
\expandafter\myadddot\the\numexpr 10000*(#1)/(#2)\relax\relax\%}

\def\myadddot#1#2#3{%
\ifx\relax#3%
.#1%
\else
#1\expandafter\myadddot\fi
#2#3}

\begin{document}


Our tool succeeds to validate \x\ samples out of all the \y\ samples, thus its success ratio is 
\mypercent{\x}{\y}


\end{document}

or for 1dp

\newcommand\mypercent[2]{%
\expandafter\adddot\the\numexpr 1000*(#1)/(#2)\relax\relax\%}

\def\adddot#1#2{%
\ifx\relax#2%
.#1%
\else
#1\expandafter\adddot\fi
#2}
David Carlisle
  • 757,742
3

Yo can also use pgf math engine:

enter image description here

Notes:

  • The optional first parameter can be used to control the number of decimal digits displayed -- defaults to 2 digits if not specified.

Code:

\documentclass{article}
\usepackage{tikz}

\newcommand\x{30} \newcommand\y{47}

\newcommand{\MyPercent}[3][2]{% \pgfmathparse{100*#2/#3}% \pgfmathprintnumber[fixed,precision=#1]{\pgfmathresult}%% }%

\begin{document}

Our tool succeeds to validate \x\ samples out of all the \y\ samples, thus its success ratio is \MyPercent{\x}{\y}, or with more digits \MyPercent[5]{\x}{\y}.

\end{document}

Peter Grill
  • 223,288
3

The mandatory solution with expl3; the optional argument is the number of decimal digits (default 0); the result is rounded.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand\mypercent{O{0}mm}
 {
  \fp_eval:n { round ( 100 * #2/#3 , #1 ) } \%
 }
\ExplSyntaxOff

\newcommand\x{30}
\newcommand\y{47}

\begin{document}

Our tool succeeds to validate \x\ samples out of all the \y\ samples, thus its
success ratio is
\mypercent{\x}{\y}.

Our competitor validated only 1 sample out of 31. Her success ratio is thus
\mypercent{1}{31}, or, more precisely \mypercent [3]{1}{31}.

\end{document}

enter image description here

egreg
  • 1,121,712
1

success rate

\documentclass{article}
\usepackage{xintfrac}

\newcommand\x{30}
\newcommand\y{47}


\newcommand\mypercent[3][1]{\xintRound {#1}{\xintE{#2/#3}{2}}\%}

\begin{document}\thispagestyle{empty}

Our tool succeeds to validate \x\ samples out of all the \y\ samples, thus its
success ratio is  
\mypercent{\x}{\y}.

Our competitor validated only 1 sample out of 31. Her success ratio is thus
\mypercent{1}{31}, or, more precisely \mypercent [3]{1}{31}.

\end{document}