emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Aaron Ecay <aaronecay@gmail.com>
To: emacs-orgmode@gnu.org
Subject: [PATCH 1/5] ox-latex: add optional-packages machinery
Date: Wed, 20 Feb 2013 23:02:22 -0500	[thread overview]
Message-ID: <1361419346-23146-2-git-send-email-aaronecay@gmail.com> (raw)
In-Reply-To: <1361419346-23146-1-git-send-email-aaronecay@gmail.com>

This code allows latex packages to be inserted into the output document
only if they are needed.  The function ‘org-latex--use-package’ is
provided for code to signal that it wants a package inserted into the
output.  The ‘org-latex-optional-packages-options-alist’ variable allows
optionally loaded packages to be customized.  It contains slots for
options to be passed to the \usepackage line, as well as arbitrary code
to be inserted into the preamble.

Code to use this mechanism is in followup patches.
---
 lisp/ox-latex.el | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 54 insertions(+), 3 deletions(-)

diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 9d5b5c5..9895028 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -418,6 +418,22 @@ toc:nil option, not to those generated with #+TOC keyword."
   :group 'org-export-latex
   :type 'boolean)
 
+(defcustom org-latex-optional-package-options-alist nil
+  "An alist of options for packages which will be loaded if needed.
+
+The format is a list of triples of (PACKAGE-NAME PACKAGE-OPTIONS
+PREAMBLE-CODE).  The PACKAGE-OPTIONS will be passed as options to
+the \\usepackage command.  The PREAMBLE-CODE will be inserted in
+the preamble of the resulting document (before any LaTeX_HEADER
+lines).
+
+Either of the values may be omitted (nil)."
+  :group 'org-export-latex
+  :type '(repeat
+	  (list
+	   (string :tag "Package name")
+	   (string :tag "Package options")
+	   (string :tag "Preamble code"))))
 
 ;;;; Headline
 
@@ -1087,6 +1103,11 @@ just outside of it."
      (funcall search-refs element))
    ""))
 
+(defun org-latex--use-package (info package)
+  (let ((optional-packages (plist-get info :latex-optional-packages)))
+    (plist-put info :latex-optional-packages
+	       (add-to-list 'optional-packages package))))
+
 
 \f
 ;;; Template
@@ -1110,7 +1131,8 @@ holding export options."
 		     (if (not class-options) header
 		       (replace-regexp-in-string
 			"^[ \t]*\\\\documentclass\\(\\(\\[.*\\]\\)?\\)"
-			class-options header t nil 1)))))
+			class-options header t nil 1))))
+	       (optional-packages (plist-get info :latex-optional-packages)))
 	  (if (not document-class-string)
 	      (user-error "Unknown LaTeX class `%s'" class)
 	    (org-latex-guess-babel-language
@@ -1118,8 +1140,13 @@ holding export options."
 	      (org-splice-latex-header
 	       document-class-string
 	       org-latex-default-packages-alist
-	       org-latex-packages-alist nil
-	       (plist-get info :latex-header-extra)))
+	       (org-latex-add-optional-packages
+		optional-packages
+		org-latex-packages-alist)
+	       nil
+	       (org-latex-add-optional-package-preambles
+		optional-packages
+		(plist-get info :latex-header-extra))))
 	     info)))))
      ;; Possibly limit depth for headline numbering.
      (let ((sec-num (plist-get info :section-numbers)))
@@ -1183,6 +1210,30 @@ holding export options."
      ;; Document end.
      "\\end{document}")))
 
+(defun org-latex-add-optional-packages (optional-packages packages)
+  (if (not optional-packages)
+      packages
+    (append packages
+	    (mapcar
+	     (lambda (pkg)
+	       (list (or (cadr (assoc pkg org-latex-optional-package-options-alist)) "")
+		     pkg
+		     nil))
+	     optional-packages))))
+
+(defun org-latex-add-optional-package-preambles (optional-packages preamble)
+  (if (not optional-packages)
+      preamble
+    (concat
+     (mapconcat
+      #'identity
+      (delq nil
+	    (mapcar (lambda (pkg)
+		      (nth 2 (assoc pkg org-latex-optional-package-options-alist)))
+		    optional-packages))
+      "\n")
+     "\n" preamble)))
+
 
 \f
 ;;; Transcode Functions
-- 
1.8.1.4

  reply	other threads:[~2013-02-21  4:02 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-02-21  4:02 [RFC] [PATCH] conditional use of latex packages Aaron Ecay
2013-02-21  4:02 ` Aaron Ecay [this message]
2013-02-21  4:02 ` [PATCH 2/5] ox-latex: convert source code and table export to use optional packages Aaron Ecay
2013-02-21  4:02 ` [PATCH 3/5] ob-R: change the file extension for tikz figures Aaron Ecay
2013-02-21  4:02 ` [PATCH 4/5] ox-latex: Treat tikz files as images Aaron Ecay
2013-02-21  4:02 ` [PATCH 5/5] ox-latex: Convert the image inclusion code to use optional packages Aaron Ecay
2013-02-21  9:51 ` [RFC] [PATCH] conditional use of latex packages Suvayu Ali
2013-02-21 15:19 ` Nicolas Goaziou
2013-02-21 17:33   ` Aaron Ecay
2013-02-21 18:39     ` Nicolas Goaziou
2013-02-24 18:47       ` Aaron Ecay
2013-02-24 18:50         ` [PATCH] ox-latex: add optional-packages machinery Aaron Ecay

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1361419346-23146-2-git-send-email-aaronecay@gmail.com \
    --to=aaronecay@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).