From mboxrd@z Thu Jan 1 00:00:00 1970 From: Rasmus Subject: [patch][ox-latex] context-aware subscript Date: Sun, 18 Aug 2013 03:29:13 +0200 Message-ID: <877gfjqq6w.fsf@pank.eu> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:44946) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VAzhT-00089o-P0 for emacs-orgmode@gnu.org; Sun, 18 Aug 2013 05:55:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VAzhN-0003dc-I0 for emacs-orgmode@gnu.org; Sun, 18 Aug 2013 05:55:07 -0400 Received: from plane.gmane.org ([80.91.229.3]:32889) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VAzhN-0003br-7e for emacs-orgmode@gnu.org; Sun, 18 Aug 2013 05:55:01 -0400 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1VAzhL-0005gN-OR for emacs-orgmode@gnu.org; Sun, 18 Aug 2013 11:54:59 +0200 Received: from 87-57-37-13-dynamic.dk.customer.tdc.net ([87.57.37.13]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 18 Aug 2013 11:54:59 +0200 Received: from rasmus by 87-57-37-13-dynamic.dk.customer.tdc.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 18 Aug 2013 11:54:59 +0200 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Hi, Currently one can't write something like \beta_t and get a nice result in org when exporting to LaTeX (where nice result := $\beta_t$). This patch tries to fix it. Nicolas, you might consider this a "can of worms", but since I'd already worked on it a while ago I'll try my luck. I'd appreciate if you'd let me know something like this would be acceptable before I put more work into it. With this patch the following document --8<---------------cut here---------------start------------->8--- #+TITLE: #+OPTIONS: toc:nil num:nil #+LATEX:\Huge * Math entity 1. \(\alpha\)\(\beta\)\(_\text{t}\) 2. \(\alpha\)\(\beta\)\(_t\) 3. \alpha\beta_t 4. \alpha\times\beta_t\gamma 5. foo_bar 6. \beta_bar --8<---------------cut here---------------end--------------->8--- translates to something like --8<---------------cut here---------------start------------->8--- \begin{enumerate} \item \(\alpha\)\(\beta\)\(_t\) \item $\alpha\beta_t$ \item $\alpha$\texttimes{}$\beta_{t\gamma}$ \item foo$_{\text{bar}}$ \item $\beta_{bar}$ \end{enumerate} --8<---------------cut here---------------end--------------->8--- For the reference, this is the output with the current HEAD --8<---------------cut here---------------start------------->8--- \begin{enumerate} \item \(\alpha\)\(\beta\)\(_\text{t}\) \item \(\alpha\)\(\beta\)\(_t\) \item $\alpha$$\beta$$_{\text{t}}$ \item $\alpha$\texttimes{}$\beta$$_{\text{t}\gamma}$ \item foo$_{\text{bar}}$ \item $\beta$$_{\text{bar}}$ \end{enumerate} --8<---------------cut here---------------end--------------->8--- As is evident from the pdf output $\alpha$$\beta$$_{\text{t}}$ ≠ $\alpha$$\beta$$_{{t}}$ ≠ $\alpha\beta_{t}$ There seems to be no difference between $\alpha$$\beta$ and $\alpha\beta$ in the pdf, but the latter is more aesthetically pleasing in the source. I (currently) don't see much use in trying to be context aware outside of the entity case (e.g. \(x\)_t?), but I could be wrong. –Rasmus --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Improved-subscript-handling-in-ox-latex.patch >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 --=-=-= Content-Type: text/plain -- Need more coffee. . . --=-=-=--