5

I have heard that you can do math in org-mode through tables, but looking it up comes up with things about LaTeX and other confusing subjects.

This is what I want to do:

|   |   1   |      2     |   3  |
|a.b| a1+b1 | a.b1+a2+b2 | etc. |
| a |   a1  |      a2    | etc. |
| b |   b1  |      b2    | etc. |
Total: a.b1, 2, 3, etc.

How can I do this? And is it possible?

2

2 Answers 2

4

Below are two examples. The simple one requires more manual labor, but is easier to understand. The advanced one is more generic, but may be more difficult to understand.

Simple

  |        |  1 | 2 | 3    |
  |--------+----+---+------|
  | a.b    |  4 | 6 | etc. |
  | a      |  1 | 2 | etc. |
  | b      |  3 | 4 | etc. |
  |--------+----+---+------|
  | Total: | 10 |   |      |
  #+TBLFM: @>$2=vsum(@2$2..@2$3)::@2$2=@3$2+@4$2::@2$3=@3$3+@4$3
  • :: separate formulas.
  • $x column number x.
  • @x row number x.
  • @# current row.
  • $# current column.
  • @> last row.
  • x..y range between x and y where x and y designate top-left and bottom-right cells to select.
  • vsum vector-sum, sums all elements of the given vector.

Briefly, what happens here can be described as: take values from second column third and fourth rows, add them together and save the result in the second row of the second column. Similarly, for the third column. Sum the second row between second and third columns and save the result in the last row, second column.

Advanced

  |        |  1 | 2 |  3 |
  |--------+----+---+----|
  | a.b    |  4 | 6 | 11 |
  | a      |  1 | 2 |  5 |
  | b      |  3 | 4 |  6 |
  |--------+----+---+----|
  | Total: | 21 |   |    |
  #+TBLFM: @>$2=vsum(@2$2..@2$4)::@2$2..@2$4=vsum(trn(pack([2, 3], @3$2..@4$4))_($#-1))
  • trn transpose matrix.
  • pack in this particular case it will rearrange the elements of the vector into a 2-by-3 matrix.
  • x_y access element of x at coordinates given by y.

Briefly, this extracts the rectangle between third row, second column and fourth row fourth column. Rearranges this as a 2-by-3 matrix. Transposes this matrix. Sums every row of this matrix and stores the result in the column with the index greater by one than that of the row summed. Finally, sums the second row (except for the first column) and saves that value in the second column, last row.

0

I'll try to explain the first sample in the documentation located here:

orgmode.org/worg/org-tutorials/org-spreadsheet-intro.html

So Basically, you refer to :

  • a line using @nb_line,
  • a column using $nb_col

(at any cell, typing C-c ? will show you it's reference)

And then you can create formulae by using the references: for instance, to compute the means of a two fields. Say we have:

| Student  | Maths | Physics | Mean |
|----------+-------+---------+------|
| Bertrand |    13 |      09 |      |
| Henri    |    15 |      14 |      |
| Arnold   |    17 |      13 |      |

You put the cursor in the first empty cell in the "Mean" column and you type =vmean($2..$3) (without any columns so the formula is applied to the whole column). You eval the formula with C-c C-c and that's it!

Hope it helps.

2
  • I read through the document about 30 minutes before you posted your comment, and looked at a video that was a quick demonstration of using tables as spreadsheets, so I understand how to do this now, and will post an answer on how to do this later. The reason I won't pick your answer is because what you are speaking of are "Means" which from what I gathered is '(a + b) / how many numbers there are.' If I am wrong then sorry, please explain a bit more because I am an idiot.
    – Anti.Josh
    Feb 22, 2016 at 19:13
  • No worry. No question is stupid to ask, and I really love helping people as much as other people helped me. You're very welcom.Yes I explained 'means but that can be extrapolated to any other function:)
    – turbopape
    Feb 25, 2016 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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