You can use org-element-map
to apply a function that gets the #+NAME
of each matched element. Here's a function that does that:
#+begin_src elisp
(defun my/get-name (e)
(org-element-property :name e))
(defun my/latex-environment-names ()
(org-element-map (org-element-parse-buffer) 'latex-environment #'my/get-name))
#+end_src
We parse the whole buffer, select the latex-environment
elements and map over them the function my/get-name
which gets the name of each such environment, accumulating them into a list and returning that.
You call it with M-: (my/latex-environment-names)
. But you probably want to write a wrapper command that calls it and deals with the list of names however it wants to. Here's a simple-minded example of that:
#+begin_src elisp
(defun my/report-latex-environment-names ()
(interactive)
(message (format "%S" (my/latex-environment-names))))
(define-key org-mode-map (kbd "C-c z") #'my/report-latex-environment-names)
#+end_src
Then C-c z
will invoke the report function which will print the result of my/latex-environment-names
in the echo area.
Here is the enhanced version as requested in a comment. Given the hint, I found out that the :value
property holds the text of the environment, so we need to do some string manipulations to get only the part we want. string-split
splits the string on newlines (\n
) and returns the lines as a list; we then use nth
to get the equation which is the second element of the list (remember, nth
starts numbering the elements of the list from 0), and string-trim
trims off leading and trailing whitespace. We call both the name
and the value
function at each matching element and we concatenate the results into a single string. Overall, it's very similar to the code block above:
#+begin_src elisp
(defun my/get-name (e)
(org-element-property :name e))
(defun my/get-value (e)
(string-trim (nth 1 (string-split (org-element-property :value e) "\n"))))
(defun my/get-both (e)
(concat (my/get-name e) " " (my/get-value e)))
(defun my/latex-environment-names-values ()
(org-element-map (org-element-parse-buffer) 'latex-environment #'my/get-both))
#+end_src