In https://orgmode.org/manual/References.html remote references are mentioned. Also there are constants for a local environment of a table. I would like to retrieve the value for a constant from the calculations of another table. In a sense it would not be a constant any longer, but in the scope of the table, it would still be a constant.

Is such a thing possible?

I tried:

#+CONSTANTS: hours=remote(other-table-name, @>$5)

But it does not seem to work and gives an error in the cell, where I use the constant.

  • Interesting question!! I did not know about this feature. Perhaps this feature is only available in specific versions of org-mode. What version of org-mode are you using? – Melioratus May 15 '20 at 15:08
  • 2
    @Melioratus If you are talking about #+CONSTANTS, it's not particularly well-known but it is an ancient feature: it is in release 5.01 (from 2008) and possibly in earlier releases too, when Org mode consisted of a single org.el file! Remote refs are not much younger: they were in 6.19a from early 2009. – NickD May 15 '20 at 20:01
  • 1
    Does it have to be in a #+CONSTANTS: line? These keywords are read only when the file is opened, they are parsed at that point to initialize various structures (in the #+CONSTANTS case, that is the list org-table-formula-constants) and then not looked at again until you press C-c C-c on the keyword line or the buffer is reverted from the underlying file. They are not meant to be the kind of scratchpad that you seem to want. Why can't you use the remote value directly? – NickD May 15 '20 at 20:10
  • @NickD I know about the constants behavior as you describe it. Sure, I can use a remote using formula inside the table and I will probably do that. I only wanted to inquire and see, whether it was possible in constants. But you are right, I don't have to use constants. – Zelphir Kaltstahl May 15 '20 at 20:47

Defining Constant Values from Remote Org-Table Cell for Use in #+TBLFM:

Initial Setup

Create Named Table, e.g. other-table-name

#+NAME: other-table-name
| S | M | T | W | H | F | S |
| 0 | 1 | 2 | 3 | 4 | 5 | 6 |

Method 1 - Dynamically Define #+CONSTANTS: using SRC Block Results

In this example, the programming language used is elisp but org-mode can be configured to run source blocks in over 30 programming languages so pick another language you like, if you don't don't want to use elisp.

The only requirements for SRC block are:

  1. SRC block #+RESULTS: must provide valid #+CONSTANTS: declaration, e.g. #+CONSTANTS: constantname=constantvalue
  2. Add :results drawer or :results raw headers to SRC block so that org-mode can interpret the #+RESULTS: as org syntax.
  3. The constant value must be fetched from cell in named table, e.g. other-table-name

In this specific example, I used Indexable Variable Values syntax, i.e. constant-value=other-table-name[2,4], to fetch constant value from a specific cell in table.

#+NAME: define-constant-with-src-block
#+BEGIN_SRC elisp :var constant-name="HOURS" :var constant-value=other-table-name[2,4] :results drawer
  (format "#+CONSTANTS: %s=%s" constant-name constant-value)

#+RESULTS: define-constant-with-src-block

#+name: example-table-method-1
| Value of $HOURS from CONSTANTS generated by SRC Block | 4 |
#+TBLFM: @1$2=$HOURS

Method 2 - Use org-table Advanced features syntax to create local table constant with value fetched using remote(other-table-name, @>$5)

#+name: example-table-method-2
| # | Value of $hours fetched from other-table-name         |       4 |
| $ |                                                       | hours=4 |
#+TBLFM: @1$3=$hours::@2$3='(format "hours=%s" remote(other-table-name, @>$5))

Thank you for asking your question!

The code in this answer was validated using the following:
emacs version: GNU Emacs 27.1
org-mode version: 9.3.7

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.