branch: externals/org
commit daf6b791756adcaee853ead1c200592f6cb9024e
Author: Pedro A. Aranda <[email protected]>
Commit: Ihor Radchenko <[email protected]>

    ox-latex: New keyword LATEX_CLASS_PRE to prepend lines before LaTeX preamble
    
    * lisp/ox-latex.el: (:options-alist,org-latex-make-preamble): New
    option to prepend lines before LaTeX preamble.
    (:options-alist): Declare the new option.
    (org-latex-make-preamble): Prepend the contents of :latex-class-pre
    unless creating an image.
    * etc/ORG-NEWS: Announce the new option.
    * doc/org-manual.org: Document #+LATEX_CLASS_PRE.
    * testing/lisp/test-ox-latex.el: (test-ox-latex/latex-class-pre)
    (test-ox-latex/latex-class-pre): Test prepending before LaTeX
    preamble.
---
 doc/org-manual.org            | 15 +++++++++++++++
 etc/ORG-NEWS                  |  3 +++
 lisp/ox-latex.el              | 13 +++++++++----
 testing/lisp/test-ox-latex.el | 17 +++++++++++++++++
 4 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index d892351412..158623d361 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -14273,6 +14273,11 @@ general options (see [[*Export Settings]]).
   hyperref settings.  See ~org-latex-classes~ for adjusting the
   structure and order of the LaTeX headers.
 
+- =LATEX_CLASS_PRE= ::
+
+  #+cindex: @samp{LATEX_CLASS_PRE}, keyword
+  Arbitrary lines to prepend before the LaTeX preamble.
+
 - =KEYWORDS= ::
 
   #+cindex: @samp{KEYWORDS}, keyword
@@ -14364,6 +14369,16 @@ A sample Org file with the above headers:
   some more text
 #+end_example
 
+#+cindex: @samp{LATEX_CLASS_PRE}, keyword
+The LaTeX export backend prepends values from =LATEX_CLASS_PRE=
+keywords before the LaTeX preamble. Use this option when you want to
+set the values passed to packages included in the document class. For
+example, to use extended names for the ~xcolor~ package, use
+
+#+begin_example
+,#+LATEX_CLASS_PRE: \PassOptionsToPackage{dvipsnames}{xcolor}
+#+end_example
+
 #+cindex: @samp{LANGUAGE}, keyword
 #+vindex: org-export-default-language
 LaTeX packages =babel= or =polyglossia= can also be loaded in a
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 72152e15a7..1a7ba4661e 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -509,6 +509,9 @@ This option specifies the PDF should be typeset using the 
Sans font
 specified in the document class (or the user) instead of the default
 font (i.e. the Roman font).
 
+*** New option ~#+LATEX_CLASS_PRE~
+
+This option prepends LaTeX code before the LaTeX preamble.
 
 ** New functions and changes in function arguments
 
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 8f8704aa2e..82828730b4 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -124,6 +124,7 @@
     (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t)
     (:latex-header "LATEX_HEADER" nil nil newline)
     (:latex-header-extra "LATEX_HEADER_EXTRA" nil nil newline)
+    (:latex-class-pre "LATEX_CLASS_PRE" nil nil newline)
     (:description "DESCRIPTION" nil nil parse)
     (:keywords "KEYWORDS" nil nil parse)
     (:subtitle "SUBTITLE" nil nil parse)
@@ -2025,10 +2026,14 @@ specified in `org-latex-default-packages-alist' or
              (let* ((class-options (plist-get info :latex-class-options))
                     (header (nth 1 (assoc class (plist-get info 
:latex-classes)))))
                (and (stringp header)
-                    (if (not class-options) header
-                      (replace-regexp-in-string
-                       "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
-                       class-options header t nil 1))))
+                    (mapconcat #'org-element-normalize-string
+                               (list
+                                 (and (not snippet?)
+                                      (plist-get info :latex-class-pre))
+                                (if (not class-options) header
+                                  (replace-regexp-in-string
+                                   "^[ 
\t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
+                                   class-options header t nil 1))))))
              (user-error "Unknown LaTeX class `%s'" class))))
     (org-latex-guess-polyglossia-language
      (org-latex-guess-babel-language
diff --git a/testing/lisp/test-ox-latex.el b/testing/lisp/test-ox-latex.el
index 4c05aadb56..de663c8e42 100644
--- a/testing/lisp/test-ox-latex.el
+++ b/testing/lisp/test-ox-latex.el
@@ -328,6 +328,23 @@ Fake test document
       (goto-char (point-min))
       (should (search-forward "\\begin{document}" nil t)))))
 
+(ert-deftest test-ox-latex/latex-class-pre ()
+  "Test #+LATEX_CLASS_PRE"
+  (org-test-with-exported-text 'latex
+                               "#+LATEX_CLASS_PRE: 
\\PassOptionsToPackage{dvipsnames}{xcolor}
+#+TITLE: Test prepending LaTeX before the preamble
+
+* Test
+
+Fake test document
+"
+      (goto-char (point-min))
+      (should (search-forward "\\PassOptionsToPackage{dvipsnames}{xcolor}" nil 
t))
+      ;; And after this
+      (should (search-forward "\\documentclass" nil t))
+      ;; And after this
+      (should (search-forward "\\begin{document}" nil t))))
+
 (ert-deftest test-ox-latex/math-in-alt-title ()
   "Test math wrapping in ALT_TITLE properties."
   (org-test-with-exported-text

Reply via email to