Nicolas Goaziou <n.goaz...@gmail.com> writes:

> Rasmus <ras...@gmx.us> writes:
>
>>> Correct. Then, fixing it is more important than caring about some user
>>> filter.
>>
>> OK, can I help?
>
> Sure, please go ahead.

I've just enclosed a quick patch (as in doesn't contain proper commit
msg), but it's basically the previous patch minus the removal of \text
in math plus some quick checks towards potential nasty filters.

It works with the following test file, but let me know about more
hair-pulling test cases, and/or filters.

#+BEGIN_SRC Org
* filters                                                          :noexport:

#+begin_src emacs-lisp
  (defun test-filter (script backend info)
    (when (org-export-derived-backend-p backend 'latex)
      (if  (string-match "a" script) "" script)))
  
  (defun test-filter2 (script backend info)
    (when (org-export-derived-backend-p backend 'latex)
      (replace-regexp-in-string "zz" "" script)))
  
  (add-to-list 'org-export-filter-subscript-functions
                 'test-filter)
  (add-to-list 'org-export-filter-subscript-functions
                 'test-filter2)
  
#+end_src

* test 
|                      | mathp | text |
|----------------------+-------+------|
| merge maybe          | \beta_t    | \times_t   |
| long merge maybe     | \beta_tb   | \times_tb  |
| filter drop          | \beta_abc  | \times_abc |
| filter replace all   | \beta_zz   | \times_zz  |
| filter replace parts | \beta_zzx  | \times_zzx  |

#+END_SRC

The output is something like this (hline removed)

                     & mathp               & text                       \\
                     %%%%%%%%%%%%%%%%%%%%%   %%%%%%%%%%%%%%%%%%%%%%%%%%
merge maybe          & $\beta_{\text{t}}$  & \texttimes{}$_{\text{t}}$  \\
long merge maybe     & $\beta_{\text{tb}}$ & \texttimes{}$_{\text{tb}}$ \\
filter drop          & $\beta$             & \texttimes{}               \\
filter replace all   & $\beta_{\text{}}$   & \texttimes{}$_{\text{}}$   \\
filter replace parts & $\beta_{\text{x}}$  & \texttimes{}$_{\text{x}}$  \\



>From b59c60eb7df5b1ff927aff9f4442b1d226d24ceb Mon Sep 17 00:00:00 2001
From: rasmus <ras...@gmx.us>
Date: Thu, 29 Aug 2013 12:18:00 +0200
Subject: [PATCH] maybe merge subscript and mathp entity with some checks

---
 lisp/ox-latex.el | 35 +++++++++++++++++++++++++++--------
 1 file changed, 27 insertions(+), 8 deletions(-)

diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 1fe918a..91ab912 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -1234,8 +1234,25 @@ 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))
+	(no-post-blanks-p (= (or (org-element-property :post-blank entity) 1) 0))
+	(no-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 no-pre-blanks-p
+		(memq (org-element-type prev) scripts)
+		(not (eq (org-export-data prev info) "")))
+	   "" "$")
+       ent
+       (if (and no-post-blanks-p
+		(memq (org-element-type next) scripts)
+		(not (eq (org-export-data next info) "")))
+	   "" "$")))))
 
 
 ;;;; Example Block
@@ -2232,12 +2249,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.4


-- 
When in doubt, do it!

Reply via email to