emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Rasmus <rasmus@gmx.us>
To: n.goaziou@gmail.com
Cc: emacs-orgmode@gnu.org
Subject: Re: [patch][ox-latex] context-aware subscript
Date: Thu, 29 Aug 2013 12:50:26 +0200	[thread overview]
Message-ID: <87vc2oojnx.fsf@gmx.us> (raw)
In-Reply-To: <87sixty63g.fsf@gmail.com> (Nicolas Goaziou's message of "Wed, 28 Aug 2013 21:21:07 +0200")

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

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

> Rasmus <rasmus@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}}$  \\




[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-maybe-merge-subscript-and-mathp-entity-with-some-che.patch --]
[-- Type: text/x-diff, Size: 2406 bytes --]

From b59c60eb7df5b1ff927aff9f4442b1d226d24ceb Mon Sep 17 00:00:00 2001
From: rasmus <rasmus@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


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



-- 
When in doubt, do it!


  reply	other threads:[~2013-08-29 10:50 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2013-08-31  8:11             ` Nicolas Goaziou
2013-08-31 13:59               ` Rasmus
2013-08-31 14:46                 ` Nicolas Goaziou

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=87vc2oojnx.fsf@gmx.us \
    --to=rasmus@gmx.us \
    --cc=emacs-orgmode@gnu.org \
    --cc=n.goaziou@gmail.com \
    /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).