[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] Unit conversions and symbolic mathematics with Babel

From: Brett Viren
Subject: Re: [O] Unit conversions and symbolic mathematics with Babel
Date: Wed, 03 Sep 2014 09:18:36 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)


address@hidden (H. Dieter Wilhelm) writes:

>   But what is missing is to assign variables within a source block
>     #+BEGIN_SRC calc :var L1 = "5 mm" 
>     L2 := cvun( L1, m)
>     #+END_SRC    
>   Unfortunately this is not working.  Do you have an idea how to
>   implement this?
> - Are you using (better) alternatives?

I like Python and in Python I like Pint for units.


Below is an example org document that should "run" and shows a couple
ways to use snippets of Pint code to do unit conversion.  Not shown but
Pint Quantity objects support arithmetic so are useful for carrying
units through some calculation.

For assigning to variables - presumably for use in later blocks - maybe
you can investigate using the :session header argument to source blocks.

Have fun,

One way to install Pint is:

#+BEGIN_SRC sh :results silent
  pip install --user pint

Here is an example:

#+name: uconv
#+header: :var val="10m" :var unit="inch" 
#+BEGIN_SRC python
  import pint
  units = pint.UnitRegistry()
  return units.Quantity(val).to(unit)

#+RESULTS: uconv
: 393.700787402 inch

Now call =uconv= do to some other conversion:

#+call: uconv("2.54cm","inch")

: 1.0 inch

Attachment: pgpazU6ovjLXZ.pgp
Description: PGP signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]