emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [patch][ox-latex] context-aware subscript
@ 2013-08-18  1:29 Rasmus
  2013-08-19  8:26 ` Nicolas Goaziou
  0 siblings, 1 reply; 10+ messages in thread
From: Rasmus @ 2013-08-18  1:29 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1957 bytes --]

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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Improved-subscript-handling-in-ox-latex.patch --]
[-- Type: text/x-diff, Size: 4256 bytes --]

From eefe696de67bd4c9baefe928a58648c75adec41e Mon Sep 17 00:00:00 2001
From: rasmus <rasmus@gmx.us>
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


[-- Attachment #3: Type: text/plain, Size: 27 bytes --]



--
Need more coffee. . .

^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2013-08-31 14:46 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-08-18  1:29 [patch][ox-latex] context-aware subscript Rasmus
2013-08-19  8:26 ` Nicolas Goaziou
2013-08-28 11:55   ` Rasmus
2013-08-28 13:38     ` Nicolas Goaziou
2013-08-28 13:46       ` Rasmus
2013-08-28 19:21         ` Nicolas Goaziou
2013-08-29 10:50           ` Rasmus
2013-08-31  8:11             ` Nicolas Goaziou
2013-08-31 13:59               ` Rasmus
2013-08-31 14:46                 ` Nicolas Goaziou

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).