Let's say I have the following structure

** fruit
*** handkerchief
**** hat
**** smoke
*** ticket
**** cinema
***** toad

It is now possible to think of the subtrees under fruit as indexed, relatively to fruit, in the following manner:

  • 1
  • 1.1
  • 1.2
  • 2
  • 2.1
  • 2.11

How could I write a function that takes the ID of fruit as an argument and returns the fruit-relative index of the current subtree?

1 Answer 1


Here is my solution:

(defun my-index ()
  (let* ((all-hls (cl-loop for entry in
               (org-export--collect-headline-numbering (org-element-parse-buffer)
                                    '(:section-numbers t)))
               collect (cons (org-element-property :begin (car entry))
                     (cdr entry))))
     (reference-hl (save-excursion
             (org-open-link-from-string "[[*fruit]]")
             (cdr (assoc (org-element-property :begin (org-element-context)) all-hls))))
     (reference-depth (length reference-hl))
     (this-hl (cdr (assoc (org-element-property :begin (org-element-context)) all-hls))))
     (mapconcat 'number-to-string (nthcdr reference-depth this-hl) "."))))

It is adapted from code at Complete path numbering of org-mode headlines and plain lists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.