0

I have an org-mode document, say

#+TITLE: My Title

* Section

My first section

and I want to export it to LaTeX with org-latex-export-to-latex. This generates a .tex file that I can now compile.

Question: Is there a way to append something like

%%% Local Variables:                                             
%%% coding: utf-8
%%% mode: latex
%%% TeX-engine: xetex
%%% End:

To the end of the .tex file, so that when I compile it with AUCTex, it will know to use the XeTeX engine to compile it instead of using the default one I have set? I tried the following

Using the org-export-before-parsing-functions hook

I defined the functions

(defun insert-at-end (text)
 "Insert the specified TEXT at the end of the current buffer."
   (save-excursion
      (goto-char (point-max))
      (insert text)))

(defun add-postamble ()
    (insert-at-end "%%% Local Variables: \n")
    (insert-at-end "%%% coding: utf-8\n")
    (insert-at-end "%%% mode: latex\n")
    (insert-at-end "%%% TeX-engine: xetex\n")
    (insert-at-end "%%% End: \n"))

Then tried adding it to the org-export-before-parsing-functions hook via

(add-hook 'org-export-before-parsing-functions
              'add-postamble)

What I got was the LaTeX file where the last few lines of the file looked like:

\%\%\% Local Variables: 
\%\%\% coding: utf-8
\%\%\% mode: latex
\%\%\% \TeX{}-engine: xetex
\%\%\% End: 
\end{document}

Using org-export-filter-final-output-functions

Defined a function

(defun my/org-export-add-lines (output backend info)
 "Add custom lines to the exported output."
 (when (and (eq backend 'latex)
                 (string= "add-lines-to-export" (plist-get info :post-export-output)))
   (concat output
           "%%% Local Variables: \n"
           "%%% coding: utf-8\n"
           "%%% mode: latex\n"
           "%%% TeX-engine: xetex\n"
           "%%% End: \n")))

and added them to org-export-filter-final-output-functions via

(add-to-list 'org-export-filter-final-output-functions 'my/org-export-add-lines)

This did nothing to the final output of the LaTeX file

Augmenting org-latex-classes

Suggested by ChatGPT I also tried to supplement org-latex-classes with a custom class with a line

("\\end{document}" . "\\end{document}\n%%%% Local Variables:\n%%%% coding: utf-8\n%%%% mode: latex\n%%%% TeX-engine: xetex\n%%%% End:"))

but that didn't work.

"Local variables"

I also tried adding "local-variables"?

# Local Variables:                                             
# org-coding: utf-8
# org-mode: latex
# org-TeX-engine: xetex
# End:

to my org buffer according to Using File Local Variables in org-mode, but this didn't seem to do anything. Perhaps I misunderstood what they were saying?

3
  • Neither the ChatGPT suggestion, nor adding local variables to the Org mode file can work. The filter function can work: why don't you add what you tried to your question?
    – NickD
    Commented Nov 14, 2023 at 19:49
  • Updated my question with more details about the attempt via hooks.
    – burek
    Commented Nov 14, 2023 at 22:54
  • In the final-output filter, when/why/how do you expect the condition (string= "add-lines-to-export" (plist-get info :post-export-output)) to be true?
    – NickD
    Commented Nov 15, 2023 at 2:29

1 Answer 1

1

Here's a slight modification of your final-output filter function that works. It primarily involves getting rid of a test that AFAICT will never evaluate to t:

(defun my/org-export-add-lines (output backend info)
  "Add custom lines to the exported output."
  (when (org-export-derived-backend-p backend 'latex)
    (concat output "\n\n"
            "%%% Local Variables: \n"
            "%%% coding: utf-8\n"
            "%%% mode: latex\n"
            "%%% TeX-engine: xetex\n"
            "%%% End: \n")))

(add-to-list 'org-export-filter-final-output-functions 'my/org-export-add-lines)

I'm not sure how you expect the test (string= "add-lines-to-export" (plist-get info :post-export-output)) to ever succeed, so I've eliminated it.

Two smaller changes: a) do not use eq to test the backend; instead use org-export-derived-backend-p as the documentation shows. That way, the function will apply if the backend is a derived backend of the indicated backend, not only when it is equal to it; and b) added a couple of newlines to make the output a bit neater.

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.