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:
SRC
block #+RESULTS:
must provide valid #+CONSTANTS:
declaration, e.g. #+CONSTANTS: constantname=constantvalue
- Add
:results drawer
or :results raw
headers to SRC
block so that org-mode
can interpret the #+RESULTS:
as org syntax.
- 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)
#+END_SRC
#+RESULTS: define-constant-with-src-block
:results:
#+CONSTANTS: HOURS=4
:end:
#+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
#+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#+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 listorg-table-formula-constants
) and then not looked at again until you pressC-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