emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [BUG] text cell contents have extra parentheses when used in formula


From: Ihor Radchenko
Subject: Re: [BUG] text cell contents have extra parentheses when used in formulas [9.6.4 ( @ /home/jet/.config/emacs/elpa/org-9.6.4/)]
Date: Sun, 23 Apr 2023 19:57:25 +0000

Ihor Radchenko <yantar92@posteo.net> writes:

>> #+TITLE: text formula repro
>>
>> | Key | Value        |
>> |-----+--------------|
>> | A   | [40, 65, 41] |
>> | B   | [40, 66, 41] |
>> #+TBLFM: $2="$1"
>>
>> Here you can see that the contents of the Key column were wrapped in
>> parentheses (ASCII 40 and 41) when turned into strings.
>
> If you turn formula debugger on (M-x org-table-toggle-formula-debugger),
> you will see the following (I also added extra explanations):
> ...
> $1->    "(A)" ;; after handling column references; the (...) is added
>               ;; because of Calc syntax to ensure that values inside
>               ;; cell are grouped. There is no notion of strings in GNU
>               ;; Calc (AFAIK). So, (...) grouping always represents
>               ;; algebraic Calc expression.
> Result: [40, 65, 41] ;; (calc-eval "\"(A)\"")

After looking closer, the code wrapping cell value into (...) is
actually not necessarily correct.

The parenthesis wrapping has been introduced in

e7e5e25100246e5eab3a36034ac2de578dabd02f
Author:     Carsten Dominik <carsten.dominik@gmail.com>
Fix bug with algebraic evaluation in tables.

There was a problem that complex fields need a pair of parentheses
around them.  For example, if one field is "a a" and another field
is "a+b", multiplication of the two gave "a^3+b" instead of "a^3+a^2 b".
Now variable replacement puts extra parenthesis around values.

----

However, in a way, unconditional wrapping of cell value in parentheses is
against how Org table formulas intuitively work - references are
replaced by appropriate cell values.

Wrapping a cell value in (...) only makes sense as long as algebraic
operations are considered. However, when cell value is intentionally
treated as string (explicit vector of character codes in GNU Calc
conventions), extra (...) will introduce the observed 40 ... 41 into the
vector.

It might be tricky to remove the existing behaviour though - removing the
parenthesis will likely break the existing use cases with genuine
algebraic computations. We might, however, use "L" flag, described in
"3.5.2 Formula syntax for Calc":

ā€˜Lā€™
     Literal, for Lisp formulas only.  See the next section.

Now, it is only used for Lisp formulas, but we can make use of it in
Calc formulas as well, preventing this (...) wrap.

Or maybe others have better ideas.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]