0

Suppose I have this org-mode table:

| Ativo | Valor R$ | % Percentual |
|-------+----------+--------------|
| A     |     13.0 |       0.1300 |
| B     |     33.0 |       0.3300 |
| C     |     54.0 |       0.5400 |
| TOTAL |    100.0 |          1.0 |
#+TBLFM: @2$3='(format "%0.4f" (/ @2$2 @5$2));N
#+TBLFM: @3$3='(format "%0.4f" (/ @3$2 @5$2));N
#+TBLFM: @4$3='(format "%0.4f" (/ @4$2 @5$2));N
#+TBLFM: @5$2='(apply '+ '(@2$2..@4$2));N
#+TBLFM: @5$3='(apply '+ '(@2$3..@4$3));N

Suppose the value of A was changed to be 113 instead of 13:

| Ativo | Valor R$ | % Percentual |
|-------+----------+--------------|
| A     |    113.0 |       0.1300 |
| B     |     33.0 |       0.3300 |
| C     |     54.0 |       0.5400 |
| TOTAL |    100.0 |          1.0 |
#+TBLFM: @2$3='(format "%0.4f" (/ @2$2 @5$2));N
#+TBLFM: @3$3='(format "%0.4f" (/ @3$2 @5$2));N
#+TBLFM: @4$3='(format "%0.4f" (/ @4$2 @5$2));N
#+TBLFM: @5$2='(apply '+ '(@2$2..@4$2));N
#+TBLFM: @5$3='(apply '+ '(@2$3..@4$3));N

This means that to have a correct table, I need to update the other values.

Usually, I put the cursor over each line starting with TBLFM and I execute org-ctrl-c-ctrl-c.

I would like to execute all of the formulas. I tried marking the region with all the table formulas with C-SPC and then executing C-c C-c. But, it did not work. The mini-buffer echoes:

user-error: ‘C-c C-c’ can do nothing useful here

How can I execute multiple TBLFM expressions at once?

After updating it, this is the expected result for me:

| Ativo | Valor R$ | % Percentual |
|-------+----------+--------------|
| A     |    113.0 |       0.5650 |
| B     |     33.0 |       0.1650 |
| C     |     54.0 |       0.2700 |
| TOTAL |    200.0 |          1.0 |
#+TBLFM: @2$3='(format "%0.4f" (/ @2$2 @5$2));N
#+TBLFM: @3$3='(format "%0.4f" (/ @3$2 @5$2));N
#+TBLFM: @4$3='(format "%0.4f" (/ @4$2 @5$2));N
#+TBLFM: @5$2='(apply '+ '(@2$2..@4$2));N
#+TBLFM: @5$3='(apply '+ '(@2$3..@4$3));N

2 Answers 2

2

I don't think you can apply all the formulas at once: if you do C-c C-c or one of its variations somewhere in the table, only the first formula gets evaluated. If you do it on one of the #+TBLFM lines, only that formula is executed. I believe that is meant for debugging purposes. If you want to apply more than one formula, you have to string them on a single #TBLFM line, with :: separators between the formulas.

The doc says:

Using multiple ‘TBLFM’ lines
............................

You may apply the formula temporarily.  This is useful when you want to
switch the formula applied to the table.  Place multiple ‘TBLFM’
keywords right after the table, and then press ‘C-c C-c’ on the formula
to apply.  Here is an example:

     | x | y |
     |---+---|
     | 1 |   |
     | 2 |   |
     #+TBLFM: $2=$1*1
     #+TBLFM: $2=$1*2

Pressing ‘C-c C-c’ in the line of ‘#+TBLFM: $2=$1*2’ yields:

     | x | y |
     |---+---|
     | 1 | 2 |
     | 2 | 4 |
     #+TBLFM: $2=$1*1
     #+TBLFM: $2=$1*2

If you recalculate this table, with ‘C-u C-c *’, for example, you get
the following result from applying only the first ‘TBLFM’ keyword.

     | x | y |
     |---+---|
     | 1 | 1 |
     | 2 | 2 |
     #+TBLFM: $2=$1*1
     #+TBLFM: $2=$1*2

No mention is made of applying both formulas at once.

Having said that, I presume you want separate lines because they are more readable and I agree. But if you have a collection of formulas and you want to evaluate all of them, you got to put them on a single #+TBLFM lines and use :: separators. However, you can shorten the formulas above substantially which mitigates to some extent the readability problem. The first three formulas are basically the same formula and so are the last two, so you only really need two formulas: one encapsulating the first three of your formulas and another encapsulating the last two. The trick is to use a range reference on the LHS of the equation:

...
#+TBLFM: @2$3..@4$3='(format "%0.4f" (/ $2 @5$2));N ::  @5$2..@5$3='(apply '+ '(@2..@4));N

Note that on the RHS of the first formula, we use $2 without specifying a row: the row is implicitly specified to be the same row that the LHS is currently using. Now on the LHS we have a range expression: row 2, column 3 to row 4 column 3. That's equivalent to three formulas, one for row 2 column 3, one for row 3, column 3 and one for row 4, column 3; in each of these three, the LHS reference $2 specifies the current row, column 2. That's equivalent to your three separate formulas.

Similarly on the second formula, the LHS is a range: row 5, column 2 to row 5, column3. That's equivalent to two formulas, one for row 5, column 2 and one for row 5, column 3. On the RHS, we also use a range @2..@4 where we don't specify a column: the range is then equivalent to @2$2..@4$2 in the first formula and @2$3..@4$3 in the second formula.

1

So tThe Org Mode info section 3.5.9 Updating the table might be helpful. There's this one in particular:

‘C-u C-c *’ or ‘C-u C-c C-c’
     Recompute the entire table, line by line.  Any lines before the
     first hline are left alone, assuming that these are part of the
     table header.

Does this work for you?

1
  • 1
    This has nothing to do with multiple #+TBLFM lines AFAIK: it is a way to guarantee that if a table row depends on a previous row, then recomputing the table this way will make sure that the table completely updates in one pass. If things are not so predictable then C-u C-u C-c C-c would iterate the update until things converge (assuming that they will - there is an upper bound on the iterations to prevent infinite loops, I think). But it only runs one #+TBLFM line: either the first one if you do it somewhere on the table, or the one you are currently on. AFAIK...
    – NickD
    Apr 27 at 15:18

Your Answer

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

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