0

Problem description

In org-mode's LaTeX export configuration, the %s operator can be used as a placeholder for the headline string, as can be seen in the examples at

https://orgmode.org/worg/org-tutorials/org-latex-export.html

For reasons far too complicated to explain here, I would like to name the sections in my exported .tex file not according to the corresponding headlines in my .org file, but instead according to the respective sections' CUSTOM_ID property.

I am wondering whether it is possible to achieve this. For example, there might be an operator equivalent to %s, which is automatically replaced by a section's CUSTOM_ID rather than by its headline?

Example to illustrate

I'd like the .org code

* My section
:PROPERTIES:
:CUSTOM_ID: foo
:END:

to result in the following .tex code when exported via C-c C-e C-b l L:

\section{foo}

and not in the following:

\section{My section}
1

Why can't you name the section "foo" in the first place?

In any case, the transcoding of a headline into the appropriate latex construct is done by org-latex-headline in ox-latex.el. The headline is transcoded by calling the function specified by the :latex-format-headline-function which by default is org-latex-format-headline-default-function. You can customize the setting of the option to your own function and model that on the default function. The main difficulty will be getting the custom_id out of the info parameter (I hope that the custom_id is in the info parameter, but I have not checked: ymmv).

Not a complete answer, but I hope it is illuminating enough for you to be able to see where to go from here.

  • Simply naming the section "foo" is not an option due to reasons faaar to compex for the discussion here :) Thanks for shedding some light on this, I might have a look at the details - perhaps that can eventually lead to a more complete answer. – Florian H. Sep 3 '18 at 19:12
0

You can do this with an org-export-before-parsing-hook. The functions in this hook can be used to modify a copy of the org-buffer prior to exporting it. I would let-bind this variable in a special :noexport: heading in your document like this:

* build                                                            :noexport:

#+BEGIN_SRC emacs-lisp
(let ((org-export-before-parsing-hook
       '(lambda (_)
      (org-element-map (org-element-parse-buffer)
          'headline
        (lambda (hl)
          (when (org-element-property :CUSTOM_ID hl)
            (goto-char (org-element-property :begin hl))
            (org-edit-headline (org-element-property :CUSTOM_ID hl))))))))
  (org-latex-export-to-latex))
#+END_SRC

Then, you just run this block, e.g. by C-c C-c to create the tex-file. You can modify the last line if you want to also make the pdf and open it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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