emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Eric Abrahamsen <eric@ericabrahamsen.net>
To: emacs-orgmode@gnu.org
Subject: getting calc-units working in table formulas
Date: Tue, 16 Dec 2014 12:21:50 +0800	[thread overview]
Message-ID: <87oar4ql5t.fsf@ericabrahamsen.net> (raw)

I've been playing with calc-units, and it's pretty amazing. See all the
units with `calc-view-units-table'.

Some calc-units stuff works out the box (maybe have to require
calc-units?), I think this should be mentioned in the manual:

| distance | time   | speed       |
|----------+--------+-------------|
| 3 km     | 2.5 hr | 1.2 km / hr |
#+TBLFM: @2$3=$1/$2

Who knew it could do that?! Probably everyone but me... It doesn't need
the constants.el package, and looks nicer in the input, to boot.

calc-units makes a few of its functions available via defmath:

| speed        | simplified speed |
|--------------+------------------|
| 40km / 2.5hr | 16. km / hr      |
|              |                  |
#+TBLFM: @2$2=usimplify($1)

But it's got a lot more tricks. I think unit conversion would be very
handy to have, but there's something I'm not getting about using
defmath. For instance, this works:

| km    |      ft |
|-------+---------|
| 2.5km | 8202.10 |
#+TBLFM: $2='(calc-eval (math-convert-units (calc-eval $1 'raw) (calc-eval "ft" 'raw))); %.2f

But this doesn't:

#+BEGIN_SRC emacs-lisp
  (defmath uconvert (expr target-units)
    (math-convert-units expr target-units))
#+END_SRC

| km    | ft                  |
|-------+---------------------|
| 2.5km | uconvert(2.5 km ft) |
#+TBLFM: $2=uconvert($1 ft)

I can't tell if I've written the calcFunc thing wrong, or if it's
somehow not getting installed correctly. I've tried several variants,
and they mostly all just give me the results above.

Does anyone know what I'm doing wrong?

Also, once that's figured out, wouldn't it be handy if Org came with a
few predefined units-related math functions? It would be a tiny bit of
code, for quite a bit more power.

At the very least, I'd like to provide a patch to the manual to make the
units stuff a little more explicit...

Eric

             reply	other threads:[~2014-12-16  4:16 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-16  4:21 Eric Abrahamsen [this message]
2014-12-16  6:33 ` getting calc-units working in table formulas Eric Abrahamsen
2014-12-16 13:06 ` Michael Brand
2014-12-17  1:09   ` Eric Abrahamsen
2014-12-17  6:39     ` Michael Brand
2014-12-17  7:02       ` Eric Abrahamsen
2014-12-18 10:07         ` [PATCH] " Eric Abrahamsen
2014-12-20 16:33           ` Nicolas Goaziou
2014-12-21  4:22             ` Eric Abrahamsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87oar4ql5t.fsf@ericabrahamsen.net \
    --to=eric@ericabrahamsen.net \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).