4

Every time I export org-mode document to LaTeX, most sections get new and unique labels, e.g.,

\section{Introduction}
\label{sec:orge0665da}

The next time I export the document, the label will change, e.g.,

 \section{Introduction}
 \label{sec:orgcd0c5bf}

If I want to do a diff of the LaTeX documents to a previous version, I get a ton of changed labels that hide the changes I have made to the text. Sometimes this confuses latexdiff as well.

Is there a way to ensure that the exported labels are the same between exports? At least for as long as the number of (sub)sections does not change? Set a seed to a random number generator? Write a hook function that returns a label given a section?

I am aware of Is there a way to suggest label names in org-mode latex export?, that solves the problem for a specific label, but not the (in org-mode) un-labelled sections.

1
  • I don't know of any "official" mechanism to accomplish this and a cursory look through the sources did not produce an "aha" moment on a possible implementation. The only thing I can suggest ATM is sed: delete the labels before diffing.
    – NickD
    Commented May 5, 2020 at 13:29

1 Answer 1

2

I think you hit on the solution yourself:

Set a seed to a random number generator?

That seems to work just fine in my limited testing. The function org-export-new-reference in ox.el does indeed call random to generate the reference label.

To seed the random number generator, call random with some string (it seeds the generator with some value computed from the string). If you want the generator seeded before every single export use the org-export-before-processing-hook:

(defun seed-random-generator (_) (random "a fixed and unchanging string"))
(add-hook 'org-export-before-processing-hook #'seed-random-generator)

(To be honest, I don't really understand why you'd want to diff the generated LaTeX files instead of diffing the original org files. At least you're not diffing PDFs. :P)

6
  • latexdiff generates documents where additions are marked in blue and deletions in red. That document is helpful to distribute to people that are, eh...diff- and org-ed challenged. Commented May 20, 2020 at 20:08
  • @NiclasBörlin Oh, yes, latexdiff on the generated files is a great idea!
    – Omar
    Commented May 20, 2020 at 22:30
  • Another point for repeatable labels is when your changes are tracked by e.g. Git: you want to look only at the relevant changes between two commits. Commented Mar 18, 2022 at 14:41
  • 1
    @DenisBitouzé I usually don't commit any automatically generated files to a repository. So, in this case, I'd only commit the org files, not the LaTeX files. The org files don't have these random strings in them, so git handles them just fine.
    – Omar
    Commented Mar 18, 2022 at 17:04
  • 1
    I can understand. In some cases, you want to commit the .tex files in order they are used by colleagues that don't use Org Mode. Another point for repeatable labels is when you build the PDF files thanks to latexmk which will rerun the compilation (even) if (only) the labels have changed. Commented Mar 18, 2022 at 18:08

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.