[Tugindia] formula and calculations

E. Krishnan ekmath at asianetindia.com
Thu Dec 4 01:09:19 CET 2008


On Wed, 3 Dec 2008, H.S.Rai wrote:

> Is there some trick / wayout or addon, by which formula written in
> LaTeX may be evaluated by substituting values. i.e. can we take
> advantage of LaTeX formula to perform calculations.
>
> For example:
>
> If if have a=2 and b=4
>
> and LaTeX formula:
>
> c = a^2 + b^2
>
> should be used as such as used normaly in LaTeX to display formula and
> then it should substitute values and do calculation automatically
> like:
>
> c = 2^2 + 4^2
> c = 20

1. Within LaTeX, we can use the "fp" package as in

    \usepackage{fp}

    \def\sumsquare#1#2{%
      \FPeval\sumsquarecalc{#1^2+#2^2}
      \FPround\sumsquareround\sumsquarecalc0
      \sumsquareround}

    Let $c=a^2+b^2$. For $a=2$ and $b=4$, we get
    $c=\sumsquare{2}{4}$

2. In a Linux system, we can shell-escape (pdflatex -shell-escape) to the
    commandline calculator "bc" as in

    Let $c=a^2+b^2$. For $a=2$ and $b=4$, we get
    $c=\immediate\write18{echo "2^2+4^2"|bc -l>answer.tex}\input{answer}$


PS: Hai! long time no see

-- 
Krishnan


More information about the tugindia mailing list