[Tugindia] formula and calculations

Aditya Mahajan adityam at umich.edu
Sat Dec 6 00:28:31 CET 2008

On Fri, 5 Dec 2008, S. venkataraman wrote:

> On Thu, Dec 4, 2008 at 10:58 PM, H.S.Rai <hsrai at gmx.net> wrote:
>> On Thu, Dec 4, 2008 at 5:39 AM, E. Krishnan <ekmath at asianetindia.com>
>> wrote:
>>> 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
>> <snip>
>>>    Let $c=a^2+b^2$. For $a=2$ and $b=4$, we get
>>>    $c=\sumsquare{2}{4}$
>> But you have entered formula twice, once for calculation purpose, and
>> second time for display. I wished to enter it once, then by some mean
>> we should able display it and get the calculations done (if not
>> comples like \int, may be simple like division, multiplication etc).
>> So that chances of error are eliminated.
>> For doing thing as you suggested, I prefer to generate .tex file from
>> C++ or PHP, as I am not that good in LaTeX.
>>> PS: Hai! long time no see
>> I am working in an environment where no one uses LaTeX. So I am also
>> not able to work much with LaTeX. But when ever I plan to do something
>> serious work, I have to thing about LaTeX.
> Apparently, with Luatex, which has lua language
> interpreter embedded in it, many of these things will be possible.
> There is also perltex, that allows use of PERL commands in
> latex document.  The author( I have forgotten the name.)
> says that the same package can be adapted to any scripting language
> like Python.

Luatex does make things like this simple. Here is an example in ConTeXt. 
This example also highlights the calcmath module in ConTeXt (it parses 
expressions using lua and converts them to tex syntax) which is useful for 
such examples.



\def\dodefineexpression[#1][#2][#3]% expr LHS RHS
      {\ctxlua{assert(loadstring("##1"))() ;
               sqrt = math.sqrt ; % Just for ease
               log  = math.log  ; %
               tex.print("\string\\inlinecalcmath{#2 = " .. #3 .. "}");}}%
    \setvalue{SHOW#1}{\inlinecalcmath{#2 = #3}}}





% Now we can define expressions easily

\defineexpression[expr1][c][2*a^2 + b^2]

\defineexpression[expr2][d][sqrt(a^2 + b^2)]


Consider two expressions: \showexpression[expr1]\ and 
\showexpression[expr2]. When evaluated at $a = 2$, $b = 4$, we get 
\evaluateexpression[expr1][a=2; b=4]\ and 


The pdf output is attached. Notice that calcmath conveted 2*a into 2a and 
sqrt(..) into \sqrt{..} while printing to TeX.


More information about the tugindia mailing list