0

I would like to extract the numbers from a table like

| 21.57 Mg  | 10^3 g | 1 m^3  |
|-----------+--------+--------|
| 1.4^3 m^3 | 1 Mg   | 10^3 L |

and multiply / divide the numbers while ignoring the units and table characters. Is this possible to implement in an automated way?

Thanks in advance,

georg

  • It would be better to organize the table differently:arrange quantities with the same units in the same column and pull out the units into a header line, leaving only the numbers in the table. That's how tables are published in journals e.g., but in this case, it would allow you to do arithmetic much more easily. – NickD Jul 14 '17 at 0:10
0

Yes this is possible.
org-table formulas support executing elisp code.

See here (as you wished units where ignored):

|             21.57 Mg |  10^3 g |   1 m^3 |
|----------------------+---------+---------|
|            1.4^3 m^3 |    1 Mg |  10^3 L |
|              1421.57 | 10001.0 | 10001.0 |
|              30198.0 | 10000.0 | 10000.0 |
| 0.015407142857142857 | 10000.0 |  0.0001 |
#+TBLFM: @>>>='(apply '+ (mapcar '(lambda (x) (string-to-number (replace-regexp-in-string "\\^" "e" x))) '(@1..@2)))
#+TBLFM: @>>='(apply '* (mapcar '(lambda (x) (string-to-number (replace-regexp-in-string "\\^" "e" x))) '(@1..@2)))
#+TBLFM: @>='(apply '/ (mapcar '(lambda (x) (string-to-number (replace-regexp-in-string "\\^" "e" x))) '(@1..@2)))

Some explanation to the formulas:

  1. @> means write into last line of the table (@>> means: second last line, and so on)
  2. (@1..@2) is the range of fields. This means: use values from first row to second row. See org-table documentation.
  3. You have to convert your field numbers to valid elisp numbers. Therefore you have to replace the ^ with e. This is done with (replace-regexp-in-string "\\^" "e" x).
  4. You have to convert the resulting string to a number. This is done with string-to-number
  5. You have to do this for every field in a column therefore mapcar over the range of fields. See online tutorials, if you want to know why lambda is used.
  6. From former stuff you get a list. Every value of this list you wanted to be added (+) or multiplied (*) or divided (/). Therefore you use apply.
  7. You can use fields in your formulas with @>$1 (last row first column). For more info see org-table documentation.
  8. If you write all formulas at one line, then you can eval everything with one C-c C-c.
    do it like this: +TBLFM: @>>>='(apply ...)::@>>='(apply...)::@>='(apply...)
  9. If you want to eval units also, it gets more complicated.
  10. If formulas get too complex, you could use org-mode code blocks. See org-mode documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.