Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
This appears to be a follow-up to this question (additional requirements: floating-point arithmetic and some modifications for the percent symbol). –  Paul Gessler May 6 at 15:22
    
PythonTeX or LuaTeX can do that. See [here][1] for examples. [1]: tex.stackexchange.com/questions/115109/calculate-prices/… –  Predrag Punosevac May 6 at 15:26
    
@PaulGessler Yes... –  SoftTimur May 6 at 15:37
    
@PredragPunosevac I tried your \directlua{tex.print(string.format("%.2f",2*(1234.56+9786.45)))}, but the compilation raised one error Undefined control sequence... –  SoftTimur May 6 at 15:48
    
@SoftTimur I asked if you needed non integer values last time, and you said no:-) –  David Carlisle May 6 at 16:05

4 Answers 4

up vote 1 down vote accepted

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}
share|improve this answer
\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}
share|improve this answer
    
\adddot is used by biblatex. –  egreg May 6 at 16:32
1  
@egreg a no doubt true but unrelated observation:-) –  David Carlisle May 6 at 16:33
    
code fails in case of 0% validation or for example with 1/3000 success rate ;-) –  jfbu May 6 at 17:02
    
Actually i always need 1 decimal digit after . (eg., %63.8 instead of 63.83), could you show me how to change your code? –  SoftTimur May 6 at 17:07
    
@SoftTimur answer updated –  David Carlisle May 6 at 18:04

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

share|improve this answer

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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.