10

I have a long document with 10 tables, and I want to to have a summary table at the end of the document. Something like that:

tab1

| Nº | Description | Value |
+----+-------------+-------+
|...                       |
+----+-------------+-------+
|    | TOTAL       |   XXX |

...

tab10

| Nº | Description | Value |
+----+-------------+-------+
|...                       |
+----+-------------+-------+
|    | TOTAL       |   XXX |

tab-summary

| Description      | Value |
+------------------+-------|
| Total of Table 1 |   XXX |
| Total of Table 2 |   XXX |
| ...                      |
+------------------+-------|
| Grand Total      |   XXX |

Is there a way to reference the total of each table, instead of manually coping the results in the summary table?

1 Answer 1

17

You are looking for remote table references:

#+TBLNAME: tab1
| Nº  | Description | Value |
|-----+-------------+-------|
|     | TOTAL       |     1 |

...

#+TBLNAME: tab10
| Nº | Description | Value |
|----+-------------+-------|
|    | TOTAL       |     2 |

tab-summary
| Description       | Value |
|-------------------+-------|
| Total of Table 1  |     1 |
| Total of Table 10 |     2 |
|-------------------+-------|
| Grand Total       |     3 |
#+TBLFM: @2$2=remote(tab1,@2$3)::@3$2=remote(tab10,@2$3)::@>$2=vsum(@I$2..@II$2)

Note, this question has already an answer there: How to reference named table or code block in Org-mode


You can even generate tab-summary automatically. This is easy if formulas can directly be written into table cells. The following ctrl-c-ctrl-c-hook allows you to install all table formulas from the cells.

(defun org-table-install-formulas ()
  "Install formulas in cells starting with = or := at the bottom of the table as #+TBLFM line.
Do nothing when point is not inside a table."
  (interactive)
  (when (org-table-p)
    (save-excursion
      (goto-char (org-table-begin))
      (org-table-next-field)
      (while (progn
           (org-table-maybe-eval-formula)
           (looking-at "[^|\n]*|\\([[:space:]]*\n[[:space:]]*|\\)?[^|\n]*\\(|\\)"))
    (goto-char (match-beginning 2)))
      ))
  nil)

(add-hook #'org-ctrl-c-ctrl-c-hook #'org-table-install-formulas)

The automatic generation of the total-sum table is shown in the following example:

#+TBLNAME: tab1
| Nº  | Description | Value |
|-----+-------------+-------|
|     | TOTAL       |     1 |

...

#+TBLNAME: tab2
| Nº | Description | Value |
|----+-------------+-------|
|    | TOTAL       |     2 |

#+TBLNAME: tab3
| Nº | Description | Value |
|----+-------------+-------|
|    | TOTAL       |     3 |


#+BEGIN_SRC emacs-lisp :var basename="tbl" start=1 stop=3
(append
 '(("Description" "Value")
   hline)
   (cl-loop for i from start upto stop
        collect (list (format "Total of Table %d" i) (format ":=remote(tab%d,@>$3)" i)))
   '(
     hline
     ("Grand Total" ":=vsum(@I$2..@II$2)")))
#+END_SRC

#+RESULTS:
| Description      | Value               |
|------------------+---------------------|
| Total of Table 1 | :=remote(tab1,@>$3) |
| Total of Table 2 | :=remote(tab2,@>$3) |
| Total of Table 3 | :=remote(tab3,@>$3) |
|------------------+---------------------|
| Grand Total      | :=vsum(@I$2..@II$2) |

The example shows the table as it results from the execution of the emacs lisp source block.

If you have installed the above org-ctrl-c-ctrl-c-hook place point in the total-sum table and press C-c C-c you get the following table:

| Description      | Value |
|------------------+-------|
| Total of Table 1 |     1 |
| Total of Table 2 |     2 |
| Total of Table 3 |     3 |
|------------------+-------|
| Grand Total      |     6 |
#+TBLFM: @2$2=remote(tab1,@>$3)::@3$2=remote(tab2,@>$3)::@4$2=remote(tab3,@>$3)::@5$2=vsum(@I$2..@II$2)
1
  • 5
    How can I do this (remote) in an elisp formula? Nov 3, 2018 at 13:27

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.