emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [odt][ANN]  Embed links to mathml files as ODF formula
@ 2011-09-08 15:38 Jambunathan K
  2011-09-08 19:57 ` [PATCH] org.el: Add support for LaTeX to MathML conversion Jambunathan K
  0 siblings, 1 reply; 3+ messages in thread
From: Jambunathan K @ 2011-09-08 15:38 UTC (permalink / raw)
  To: emacs-orgmode

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


I am pleased to announce (preliminary) support for embedding mathml
files as ODF formulae in odt documents.

Specfically if one provides a link to mathml files as shown below

[[./equation.mathml]]

then the contents of the mathml file is embedded in the exported ODT
document as a ODF formula.

I intend to support link to *.odf files as well. (But this is not
available yet).

Moot point: I am still uncertain how the mathml equations specified in
Org format. For example, does the current approach of using file links
of the form *.mathml (or *.odf) extension sound OK? Should I use a new
mathml:// link type?

Please see the attached Org file for a quick demonstration of what the
current set of changes mean.

1. Update work area
2. Copy mathml.org, formula1.mathml, formula2.mathml to the same
   directory
3. Use C-c C-e O to export.

Currently the equations are embedded as unnumbered, display/paragraph
types.  

IMPORTANT: I am following this mail with a patch to org.el which will
add support for LaTeX-to-MathML conversion using an external
converter.


[-- Attachment #2: mathml.org --]
[-- Type: text/plain, Size: 695 bytes --]

#+TITLE:     mathml.org
#+AUTHOR:    Jambunathan K
#+EMAIL:     kjambunathan@gmail.com
#+DESCRIPTION:
#+KEYWORDS:
#+LANGUAGE:  en
#+OPTIONS:   H:3 num:t toc:t \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t
#+OPTIONS:   TeX:t LaTeX:t skip:nil d:nil todo:t pri:nil tags:not-in-toc

#+EXPORT_SELECT_TAGS: export
#+EXPORT_EXCLUDE_TAGS: noexport
#+LINK_UP:   
#+LINK_HOME: 
#+XSLT:

* Unnumbered Display Equation (Non-captioned)
  [[./formula1.mathml]]


* Un-numbered Display Equation (Captioned)
#+CAPTION: Kinetic Energy
#+LABEL:Equation:1
  [[file:formula2.mathml]]


* COMMENT LaTeX Fragment

#+CAPTION: Kinetic Energy 
#+LABEL:Equation:1

  \begin{equation*}
  e = \frac{1}{2}mv^2
  \end{equation*}
  

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: formula1.mathml --]
[-- Type: text/xml, Size: 293 bytes --]

<?xml version="1.0" encoding="UTF-8"?>

<math xmlns="http://www.w3.org/1998/Math/MathML">
 <semantics>
  <mrow>
   <mi>x</mi>
   <mi mathvariant="normal">=</mi>
   <msqrt>
    <mi>b</mi>
   </msqrt>
  </mrow>
  <annotation encoding="StarMath 5.0">x = sqrt b</annotation>
 </semantics>
</math>

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #4: formula2.mathml --]
[-- Type: text/xml, Size: 414 bytes --]

<?xml version="1.0" encoding="UTF-8"?>

<math xmlns="http://www.w3.org/1998/Math/MathML">
 <semantics>
  <mrow>
   <mrow>
    <mi>e</mi>
    <mi mathvariant="normal">=</mi>
    <mfrac>
     <mn>1</mn>
     <mn>2</mn>
    </mfrac>
   </mrow>
   <msup>
    <mi mathvariant="italic">mv</mi>
    <mn>2</mn>
   </msup>
  </mrow>
  <annotation encoding="StarMath 5.0">e=1 over 2 mv^2
</annotation>
 </semantics>
</math>

[-- Attachment #5: mathml.odt --]
[-- Type: application/vnd.oasis.opendocument.text, Size: 9453 bytes --]

[-- Attachment #6: Type: text/plain, Size: 5 bytes --]


-- 

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

* [PATCH] org.el: Add support for LaTeX to MathML conversion
  2011-09-08 15:38 [odt][ANN] Embed links to mathml files as ODF formula Jambunathan K
@ 2011-09-08 19:57 ` Jambunathan K
  2011-09-17 19:35   ` Jambunathan K
  0 siblings, 1 reply; 3+ messages in thread
From: Jambunathan K @ 2011-09-08 19:57 UTC (permalink / raw)
  To: emacs-orgmode

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


> IMPORTANT: I am following this mail with a patch to org.el which will
> add support for LaTeX-to-MathML conversion using an external
> converter.

MathToWeb is available from
http://www.mathtoweb.com/cgi-bin/mathtoweb_home.pl

Your .emacs should look something like this.

(custom-set-variables
 '(org-latex-to-mathml-convert-command "java -jar %j -unicode -force -df %o %I")
 '(org-latex-to-mathml-jar-file "~/tmp-odt/mathtoweb.jar"))

I am attaching odt files used with both dvipng and mathml options. The
latex-mathml.odt reports an error on one of the $$ $$ equations.

Note to reviewers: Can someone enhance the patch so that
`org-latex-src-embed-type can take all the various anchors that a latex
equation could use - inline, display-unnumbered, display-numbered, what
else?

Note to the users:
1. You need to not only apply this patch but also pull from the repo for
   full support.
2. Currently there is NO support for numbered and displayed equations
   which go like this

   x = y             (1)
   y = z             (2)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org.el-Add-support-for-LaTeX-to-MathML-conversion.patch --]
[-- Type: text/x-patch, Size: 8309 bytes --]

From 762bffe1fc11d28502d6842c9b7d0049442ee2ef Mon Sep 17 00:00:00 2001
From: Jambunathan K <kjambunathan@gmail.com>
Date: Fri, 9 Sep 2011 00:14:59 +0530
Subject: [PATCH] org.el: Add support for LaTeX to MathML conversion

* lisp/org.el (org-latex-to-mathml-jar-file)
(org-latex-to-mathml-convert-command): New user-customizable
variables.
(org-format-latex-mathml-available-p, org-create-math-formula)
(org-format-latex-as-mathml): New functions.
(org-format-latex): Add a new local variable block-type that
notes the nature of the equation - inline or display.
Associate it's value to `org-latex-src-embed-type' property of
dvipng links.  Add mathml as new processing type.
---
 lisp/org.el |  159 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 154 insertions(+), 5 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index d63b854..b907338 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -16507,11 +16507,11 @@ Some of the options can be changed using the variable
 	  (plist-get (org-infile-export-plist) :latex-header-extra))
 	 (cnt 0) txt hash link beg end re e checkdir
 	 executables-checked string
-	 m n block linkfile movefile ov)
+	 m n block-type block linkfile movefile ov)
     ;; Check the different regular expressions
     (while (setq e (pop re-list))
-      (setq m (car e) re (nth 1 e) n (nth 2 e)
-	    block (if (nth 3 e) "\n\n" ""))
+      (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
+	    block (if block-type "\n\n" ""))
       (when (member m matchers)
 	(goto-char (point-min))
 	(while (re-search-forward re nil t)
@@ -16540,7 +16540,7 @@ Some of the options can be changed using the variable
 				'(org-protected t))))
 		(add-text-properties (match-beginning n) (match-end n)
 				     '(org-protected t))))
-	     ((or (eq processing-type 'dvipng) t)
+	     ((eq processing-type 'dvipng)
 	      ;; Process to an image
 	      (setq txt (match-string n)
 		    beg (match-beginning n) end (match-end n)
@@ -16596,7 +16596,156 @@ Some of the options can be changed using the variable
 		(insert (org-add-props link
 			    (list 'org-latex-src
 				  (replace-regexp-in-string
-				   "\"" "" txt)))))))))))))
+				   "\"" "" txt)
+				  'org-latex-src-embed-type
+				  (if block-type 'paragraph 'character))))))
+	     ((eq processing-type 'mathml)
+	      ;; Process to MathML
+	      (unless executables-checked
+		(unless (save-match-data (org-format-latex-mathml-available-p))
+		  (error "LaTeX to MathML converter not configured"))
+		(setq executables-checked t))
+	      (setq txt (match-string n)
+		    beg (match-beginning n) end (match-end n)
+		    cnt (1+ cnt))
+	      (if msg (message msg cnt))
+	      (goto-char beg)
+	      (delete-region beg end)
+	      (insert (org-format-latex-as-mathml
+		       txt block-type prefix dir)))
+	     (t
+	      (error "Unknown conversion type %s for latex fragments"
+		     processing-type)))))))))
+
+(defcustom org-latex-to-mathml-jar-file nil
+  "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
+Use this to specify additional executable file say a jar file.
+
+When using MathToWeb as the converter, specify the full-path to
+your mathtoweb.jar file."
+  :group 'org-latex
+  :type '(choice
+	  (const :tag "None" nil)
+	  (file :tag "JAR file" :must-match t)))
+
+(defcustom org-latex-to-mathml-convert-command nil
+  "Command to convert LaTeX fragments to MathML.
+Replace format-specifiers in the command as noted below and use
+`shell-command' to convert LaTeX to MathML.
+%j: 	Executable file in fully expanded form as specified by
+ 	`org-latex-to-mathml-jar-file'.
+%I:     Input LaTeX file in fully expanded form
+%o:     Output MathML file
+This command is used by `org-create-math-formula'.
+
+When using MathToWeb as the converter, set this to
+\"java -jar %j -unicode -force -df %o %I\"."
+  :group 'org-latex
+  :type '(choice
+	  (const :tag "None" nil)
+	  (string :tag "\nShell command")))
+
+(defun org-format-latex-mathml-available-p ()
+  "Return t if `org-latex-to-mathml-convert-command' is usable."
+  (save-match-data
+    (when (and (boundp 'org-latex-to-mathml-convert-command)
+	       org-latex-to-mathml-convert-command)
+      (let ((executable (car (split-string
+			      org-latex-to-mathml-convert-command))))
+	(when (executable-find executable)
+	  (if (string-match
+	       "%j" org-latex-to-mathml-convert-command)
+	      (file-readable-p org-latex-to-mathml-jar-file)
+	    t))))))
+
+(defun org-create-math-formula (latex-frag &optional mathml-file)
+  "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
+Use `org-latex-to-mathml-convert-command'.  If the conversion is
+sucessful, return the portion between \"<math...> </math>\"
+elements otherwise return nil.  When MATHML-FILE is specified,
+write the results in to that file.  When invoked as an
+interactive command, prompt for LATEX-FRAG, with initial value
+set to the current active region and echo the results for user
+inspection."
+  (interactive (list (let ((frag (when (region-active-p)
+				   (buffer-substring-no-properties
+				    (region-beginning) (region-end)))))
+		       (read-string "LaTeX Fragment: " frag nil frag))))
+  (unless latex-frag (error "Invalid latex-frag"))
+  (let* ((tmp-in-file (file-relative-name
+		       (make-temp-name (expand-file-name "ltxmathml-in"))))
+	 (ignore (write-region latex-frag nil tmp-in-file))
+	 (tmp-out-file (file-relative-name
+			(make-temp-name (expand-file-name  "ltxmathml-out"))))
+	 (cmd (format-spec
+	       org-latex-to-mathml-convert-command
+	       `((?j . ,(shell-quote-argument
+			 (expand-file-name org-latex-to-mathml-jar-file)))
+		 (?I . ,(shell-quote-argument tmp-in-file))
+		 (?o . ,(shell-quote-argument tmp-out-file)))))
+	 mathml shell-command-output)
+    (when (org-called-interactively-p 'any)
+      (unless (org-format-latex-mathml-available-p)
+	(error "LaTeX to MathML converter not configured")))
+    (message "Running %s" cmd)
+    (setq shell-command-output (shell-command-to-string cmd))
+    (setq mathml
+	  (when (file-readable-p tmp-out-file)
+	    (with-current-buffer (find-file-noselect tmp-out-file t)
+	      (goto-char (point-min))
+	      (when (re-search-forward
+		     (concat
+		      (regexp-quote
+		       "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
+		      "\\(.\\|\n\\)*"
+		      (regexp-quote "</math>")) nil t)
+		(prog1 (match-string 0) (kill-buffer))))))
+    (cond
+     (mathml
+      (setq mathml
+	    (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
+      (when mathml-file
+	(write-region mathml nil mathml-file))
+      (when (org-called-interactively-p 'any)
+	(message mathml)))
+     ((message "LaTeX to MathML conversion failed")
+      (message shell-command-output)))
+    (delete-file tmp-in-file)
+    (when (file-exists-p tmp-out-file)
+      (delete-file tmp-out-file))
+    mathml))
+
+(defun org-format-latex-as-mathml (latex-frag latex-frag-type
+					      prefix &optional dir)
+  "Use `org-create-math-formula' but check local cache first."
+  (let* ((absprefix (expand-file-name prefix dir))
+	 (print-length nil) (print-level nil)
+	 (formula-id (concat
+		      "formula-"
+		      (sha1
+		       (prin1-to-string
+			(list latex-frag
+			      org-latex-to-mathml-convert-command)))))
+	 (formula-cache (format "%s-%s.mathml" absprefix formula-id))
+	 (formula-cache-dir (file-name-directory formula-cache)))
+
+    (unless (file-directory-p formula-cache-dir)
+      (make-directory formula-cache-dir t))
+
+    (unless (file-exists-p formula-cache)
+      (org-create-math-formula latex-frag formula-cache))
+
+    (if (file-exists-p formula-cache)
+	;; Successful conversion.  Return the link to MathML file.
+	(org-add-props
+	    (format  "[[file:%s]]" (file-relative-name formula-cache dir))
+	    (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
+		  'org-latex-src-embed-type (if latex-frag-type
+						'paragraph 'character)))
+      ;; Failed conversion.  Return the LaTeX fragment verbatim
+      (add-text-properties
+       0 (1- (length latex-frag)) '(org-protected t) latex-frag)
+      latex-frag)))
 
 ;; This function borrows from Ganesh Swami's latex2png.el
 (defun org-create-formula-image (string tofile options buffer)
-- 
1.7.2.3


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: latex-mathml.org --]
[-- Type: text/x-org, Size: 795 bytes --]

#+TITLE:     latex-mathml.org
#+AUTHOR:    Jambunathan K
#+EMAIL:     kjambunathan@gmail.com
#+DATE:      2011-09-09 Fri
#+DESCRIPTION:
#+KEYWORDS:
#+LANGUAGE:  en
#+OPTIONS:   H:3 num:t toc:t \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t
#+OPTIONS:   TeX:t LaTeX:t skip:nil d:nil todo:t pri:nil tags:not-in-toc

#+EXPORT_SELECT_TAGS: export
#+EXPORT_EXCLUDE_TAGS: noexport
#+LINK_UP:   
#+LINK_HOME: 
#+XSLT:

* LaTeX Fragments

** LaTeX Fragment1
#   See org-format-latex-options

    There is a equation down below.

   \begin{equation}
     e = \frac{1}{2}mv^2
   \end{equation}

** LaTeX Fragment2

#+CAPTION: Radicals
#+LABEL: Equation:1
   \begin{equation}
   x=\sqrt{b}
   \end{equation}

   If $a^2=b$ and \( b=2 \), then the solution must be either $$
   a=+\sqrt{2} $$ or \[ a=-\sqrt{2} \].

[-- Attachment #4: latex-mathml.odt --]
[-- Type: application/vnd.oasis.opendocument.text, Size: 11436 bytes --]

[-- Attachment #5: latex-dvipng.odt --]
[-- Type: application/vnd.oasis.opendocument.text, Size: 12342 bytes --]

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

* Re: [PATCH] org.el: Add support for LaTeX to MathML conversion
  2011-09-08 19:57 ` [PATCH] org.el: Add support for LaTeX to MathML conversion Jambunathan K
@ 2011-09-17 19:35   ` Jambunathan K
  0 siblings, 0 replies; 3+ messages in thread
From: Jambunathan K @ 2011-09-17 19:35 UTC (permalink / raw)
  To: emacs-orgmode

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


> 2. Currently there is NO support for numbered and displayed equations
>    which go like this
>
>    x = y             (1)
>    y = z             (2)

This is no longer true for equations that are embedded as
MathML. Equations that are attached with Labels will be typeset as
Numbered Display Equations.

See the attached file for an illustrative export.


[-- Attachment #2: latex-mathml-display-equations.odt --]
[-- Type: application/vnd.oasis.opendocument.text, Size: 11380 bytes --]

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: latex-mathml-display-equations.org --]
[-- Type: text/x-org, Size: 777 bytes --]

#+TITLE:     latex-mathml-display-equations.org
#+AUTHOR:    Jambunathan K
#+EMAIL:     kjambunathan@gmail.com
#+DATE:      2011-09-18 Sun
#+DESCRIPTION:
#+KEYWORDS:
#+LANGUAGE:  en
#+OPTIONS:   H:3 num:t toc:nil \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t
#+OPTIONS:   TeX:t LaTeX:t skip:nil d:nil todo:t pri:nil tags:not-in-toc

#+EXPORT_SELECT_TAGS: export
#+EXPORT_EXCLUDE_TAGS: noexport
#+LINK_UP:   
#+LINK_HOME: 
#+XSLT:

* LaTeX Fragment
  The equation down below has just a label.

#+CAPTION: Kinetic Energy
#+LABEL: Equation:2
  \begin{equation}
  x=\sqrt{b}
  \end{equation}

  If $a^2=b$ and \( b=2 \), then the solution must be either \[
  a=+\sqrt{2} \] or \[ a=-\sqrt{2} \].

* Reference to LaTeX Fragments
  This is a reference to LaTeX Fragment2 \ref{Equation:2}.


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

end of thread, other threads:[~2011-09-17 19:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-08 15:38 [odt][ANN] Embed links to mathml files as ODF formula Jambunathan K
2011-09-08 19:57 ` [PATCH] org.el: Add support for LaTeX to MathML conversion Jambunathan K
2011-09-17 19:35   ` Jambunathan K

Code repositories for project(s) associated with this 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).