From eefe696de67bd4c9baefe928a58648c75adec41e Mon Sep 17 00:00:00 2001 From: rasmus Date: Sat, 13 Jul 2013 15:59:23 +0200 Subject: [PATCH] Improved subscript handling in ox-latex. * ox-latex.el (org-latex-subscript-format): Controls the format of subscripts. (org-latex-entity): Collects multiple math entities into one math fragment. (org-latex-entity): Collect proceeding subscripts into same math fragment. (org-latex--script-size): Better collection of subscripts into math-fragments. Allows for different handling of math-subscripts and text subscripts. In the above "subscripts" refers to subscripts and superscripts. --- lisp/ox-latex.el | 55 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el index 1fe918a..183c89a 100644 --- a/lisp/ox-latex.el +++ b/lisp/ox-latex.el @@ -552,6 +552,20 @@ returned as-is." :options '(bold code italic strike-through underline verbatim)) +(defcustom org-latex-subscript-format '("\\text{%s}" . "%s") + "Formatting string or strings used for sub- and super-scripts. + +If `org-latex-subscript-format' is a cons of two strings, the first +element will be used when a subscript is plain text, +e.g. foo_bar, and the second will be used when a subscript is +math, e.g. beta_t. + +If `org-latex-subscript-format' is a string this is used in both +cases. + +The string should contain \"%s\", which is then replaced by the +actual subscript.") + ;;;; Drawers (defcustom org-latex-format-drawer-function nil @@ -1234,8 +1248,23 @@ holding contextual information. See `org-export-data'." "Transcode an ENTITY object from Org to LaTeX. CONTENTS are the definition itself. INFO is a plist holding contextual information." - (let ((ent (org-element-property :latex entity))) - (if (org-element-property :latex-math-p entity) (format "$%s$" ent) ent))) + (let ((ent (org-element-property :latex entity)) + (prev (org-export-get-previous-element entity info)) + (next (org-export-get-next-element entity info)) + (post-blanks-p (= (or (org-element-property :post-blank entity) 1) 0)) + (pre-blanks-p (= (or (org-element-property :post-blank + (org-export-get-previous-element + entity info)) 1) 0)) + (scripts '(subscript superscript))) + (if (not (org-element-property :latex-math-p entity)) ent + (concat + (if (and pre-blanks-p + (or (org-element-property :latex-math-p prev) + (memq (org-element-type prev) scripts))) "" "$") + ent + (if (and post-blanks-p + (or (org-element-property :latex-math-p next) + (memq (org-element-type next) scripts))) "" "$"))))) ;;;; Example Block @@ -2217,8 +2246,12 @@ channel." (let ((blank (org-element-property :post-blank obj))) (and blank (> blank 0) "\\ ")))))) (plain-text - (setq output - (format "%s\\text{%s}" output (org-export-data obj info)))) + (let* ((mathp (org-element-property :latex-math-p (org-export-get-previous-element (org-export-get-parent obj) info))) + (format (cond ((stringp org-latex-subscript-format) org-latex-subscript-format) + ((consp org-latex-subscript-format) (funcall (if mathp 'cdr 'car) org-latex-subscript-format)) + (t "\\text{%s}")))) + (setq output + (format (concat "%s" format) output (org-export-data obj info))))) (otherwise (setq output (concat output @@ -2232,12 +2265,14 @@ channel." ;; superscript into the same math snippet. (concat (and (not in-script-p) (let ((prev (org-export-get-previous-element object info))) - (or (not prev) - (not (eq (org-element-type prev) - (if (eq type 'subscript) 'superscript - 'subscript))) - (let ((blank (org-element-property :post-blank prev))) - (and blank (> blank 0))))) + (and + (not (org-element-property :latex-math-p prev)) + (or (not prev) + (not (eq (org-element-type prev) + (if (eq type 'subscript) 'superscript + 'subscript))) + (let ((blank (org-element-property :post-blank prev))) + (and blank (> blank 0)))))) "$") (if (eq (org-element-type object) 'subscript) "_" "^") (and (> (length output) 1) "{") -- 1.8.3.4