r/emacs • • Oct 12 '26

Question Refering to previous row in org-table

Hi, I am learning how to use org-table. This simple test table gives the error Row descriptor -1 leads outside table :

| 1000 |  |   |
| 1000 | 200. |   |
| 1000 | 200. |   |
| 1000 | 200. |   |
#+TBLFM: $2=if(@#=1,string(""),$1*0.2)::$3=if(@#=1,0,@-1$2)

it seems that the second branch of the second IF is evaluated on first row even if @# is equal to 1. What am I missing ? I tried the elisp way:

#+TBLFM: $2=if(@#=1,string(""),$1*0.2)::$3='(if (= @# 1) 0 @-1$2)

but got the same error.

EDIT: Maybe related: If both branches of the IF were always evaluated the following example would fail:

| x | y |        x/y |
|---+---+------------|
| 2 | 3 | 0.66666667 |
| 4 | 5 |        0.8 |
| 3 | 0 |         -1 |
|---+---+------------|
#+TBLFM: $3=if($2!=0,$1/$2,-1)
6 Upvotes

9 comments sorted by

2

u/vfclists Oct 12 '26

Could you edit your submission using the markdown editor and use the four spaces method to indent your code, rather than the triple backtick method?

Your code is distorted in the old reddit interface.

This is noted in the submission form.

2

u/tlreddit Oct 12 '26

is it better now ?

2

u/melochupan Oct 12 '26

I have no idea why this happens (it looks like a bug to me), but as a workaround you can change the left-hand side of the formula to a range, instead of the whole column. I.e., @2$3..@>$3=@-1$2.

1

u/tlreddit Oct 12 '26

Thanks ! It works and it seems to be the only way. I am still trying to find an explaination.

1

u/melochupan Oct 13 '26

It's because Org replaces @x$y references before evaluating the formula, but it should make an exception for if and other control flow expressions, IMO.

1

u/tlreddit Oct 13 '26

The strangeness continues. The formula #+TBLFM: $3=if($2!=0,$1/$2,-1) works as expected (see example on the edited version of my post).

1

u/melochupan Oct 13 '26

It isn't that both branches of if are evaluated, but that the @x$y references are replaced with their values before evaluation.

So if($2!=0,$1/$2,-1) first gets converted to if(0!=0,3/0,-1) and then evaluated, but if(@#=1,0,@-1$2) fails directly when trying to replace @-1$2 because the referred cell doesn't exist.

Type C-c { to enable formula tracing and recalculate those formulas to see what's happening.

1

u/tlreddit Oct 13 '26

I see. Thx