I've written a function that does what you want in a quite extendable
manner. It checks which headlines contain the property CUSTOM_LABEL
(or some other property you configure) and calls the function
endless/insert-org-label-latex
on each of them with the value of the
property as an argument.
The example snippet also shows how to extend it for html or other backends.
Configure the replacements
With this variable you can configure properties you care about and
which functions get called to handle each property.
(defcustom endless/org-property-mapping
'((latex ("CUSTOM_LABEL" . endless/insert-org-label-latex))
(html ("CUSTOM_LABEL" . endless/insert-org-label-html)))
"List of mappings from org property to arbitrary strings.
Each element is a list:
(BACKEND (PROPERTY1 . FUNCTION1) (PROPERTY2 . FUNCTION2) ...)
FUNCTION are functions which get called with a single
argument (the value of PROPERTY) and are responsible for doing
whatever should be done."
:type '(repeat (cons symbol (repeat (cons string string)))))
The Heavy worker
This function is what you should add to the org export hook. It takes
care of checking for the properties listed above, and calling the
functions associated with those properties.
(defun endless/replace-org-property (backend)
"Convert org properties using `endless/org-property-mapping'.
Lookup BACKEND in `endless/org-property-mapping' for a list of
\(PROPERTY REPLACEMENT). For each healine being exported, if it has a
PROPERTY listed insert a string immediately after the healine given by
(format REPLACEMENT PROPERTY-VALUE)"
(let ((map (cdr (assoc backend endless/org-property-mapping)))
value replacement)
(when map
(org-map-entries
(lambda ()
(dolist (it map)
(save-excursion
(when (setq value (org-entry-get (point) (car it)))
(funcall (cdr it) value)))))))))
(add-hook 'org-export-before-processing-hook #'endless/replace-org-property)
The functions you define
These are the ones that do the actual replacement. Below is an example
for the latex case.
(defun endless/insert-org-label-latex (label)
"Insert \"\\\\label{LABEL}\\n\" after the :PROPERTY: drawer."
(search-forward-regexp org-property-end-re)
(forward-char 1)
(insert (format "\\label{%s}\n" label)))
Result
Evaluate all of this code above, then export the following org buffer to latex.
* Test
:PROPERTIES:
:CUSTOM_LABEL: hi
:END:
Test
The resulting latex buffer should be something like this.
\section{Test}
\label{sec-1}
\label{hi}
Test
emacs-orgmode
list (among others, lists.gnu.org/archive/html/emacs-orgmode/2014-09/msg00498.html). I tried that code, and just settingorg-latex-custom-id-as-label
. It works fine with HTML export, but it does not have any effect with LaTeX export. I wish I could rely just onorg-mode
core functions, still I do like @malababrba's answer, as it allows a nice generalization.\section{h}\label{sec-1}
I am usingGNU Emacs 24.3.94.1 (x86_64-apple-darwin13.4.0, NS apple-appkit-1265.21) of 2014-10-04 on builder10-9.porkrind.org
andOrg-mode version 8.2.6 (release_8.2.6-1 @ /Applications/Emacs.app/Contents/Resources/lisp/org/)
. Also, to make sure, I renamed my .emacs.d, so it ran with no custom stuff.org-mode
using thisel-get
recipe: github.com/dimitri/el-get/blob/master/recipes/org-mode.rcp, but I still getOrg-mode version 8.2.6 (release_8.2.6-1 @ /Users/gsl/.emacs.d/el-get/org-mode/lisp/
Would you know how to tweak that recipe so that I could use it for the dev-branch? I could also ask this as new question. Thank you so much for pointing that out.