2

I have defined an environment that is used for calculation as shown below

\newcounter{Precision}
\newenvironment{calc}[1][1]{\setcounter{Precision}{#1}
$\pgfkeys{/pgf/fpu,/pgf/fpu/output format=sci}
}
{ 
\pgfmathfloatparsenumber{\pgfmathresult}
\pgfmathprintnumber[precision=\thePrecision]{\pgfmathresult}
\pgfkeys{/pgf/fpu=false}$}

The following environment call will help in setting the required precision.

\begin{calc}[2]
  \pgfmathparse{5/(2^(15))}
\end{calc}

In the similar way, I want to change the output format from "sci" to "fixed" through the argument.

\begin{calc}[2, fixed]
  \pgfmathparse{5/(2^(15))}
\end{calc}
\begin{calc}[2, sci]
  \pgfmathparse{5/(2^(15))}
\end{calc}

How to redefine the key in the newenvironment ?

1
  • 2
    Could you please provide a complete MWE in which you also show how you use the environment? Naively I would say \pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed} but this is most likely no the answer you are looking for, so please add the MWE.
    – user121799
    Jun 12, 2018 at 19:07

1 Answer 1

2

You don't need an environment for this. Just make a new command as a shortcutwith a default argument for the printing style.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fpu}
\newcommand*{\myparse}[2][sci,precision=2]{%
  \pgfkeys{/pgf/fpu}%
  \pgfmathparse{#2}%
  \pgfmathprintnumber[#1]{\pgfmathresult}%
  \pgfkeys{/pgf/fpu=false}%
}
\begin{document}
\myparse{5/(2^(15))},
\myparse[fixed, precision=5]{5/(2^(15))},
\myparse[1000 sep=\|, precision=0]{2^15)}
\end{document}

enter image description here

2
  • Hi percusse, Thanks for the code.How to replace the Dot symbol with X
    – Raja
    Jun 15, 2018 at 19:14
  • @Narashimaraja You can add \pgfkeys{/pgf/number format/sci generic={mantissa sep=\times, exponent={10^{#1}}}} at your preamble to change all occurences.
    – percusse
    Jun 15, 2018 at 23:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .