ソースを参照

LaTeX export: Better analysis of math environments

We now use the export preprocessor to identify all math snippets in
advance, this is superior to calling org-inside-LaTeX-fragment-p from
the inside.
Carsten Dominik 16 年 前
コミット
3bfed1ed35
5 ファイル変更41 行追加18 行削除
  1. 1 3
      ChangeLog
  2. 6 6
      doc/org.texi
  3. 7 0
      lisp/ChangeLog
  4. 26 8
      lisp/org-export-latex.el
  5. 1 1
      lisp/org.el

+ 1 - 3
ChangeLog

@@ -1,6 +1,4 @@
-2008-12-31  Carsten Dominik  <carsten.dominik@gmail.com>
-
-	* 
+2009-01-06  Carsten Dominik  <carsten.dominik@gmail.com>
 
 	* Makefile (LISPF): Add org-footnote.el
 

+ 6 - 6
doc/org.texi

@@ -7055,12 +7055,12 @@ Environments of any kind.  The only requirement is that the
 whitespace.
 @item
 Text within the usual La@TeX{} math delimiters.  To avoid conflicts with
-currency specifications, single @samp{$} characters are only recognized
-as math delimiters if the enclosed text contains at most two line breaks,
-is directly attached to the @samp{$} characters with no whitespace in
-between, and if the closing @samp{$} is followed by whitespace or
-punctuation.  For the other delimiters, there is no such restriction, so
-when in doubt, use @samp{\(...\)} as inline math delimiters.
+currency specifications, single @samp{$} characters are only recognized as
+math delimiters if the enclosed text contains at most two line breaks, is
+directly attached to the @samp{$} characters with no whitespace in between,
+and if the closing @samp{$} is followed by whitespace, punctuation or a dash.
+For the other delimiters, there is no such restriction, so when in doubt, use
+@samp{\(...\)} as inline math delimiters.
 @end itemize
 
 @noindent For example:

+ 7 - 0
lisp/ChangeLog

@@ -1,5 +1,12 @@
 2009-01-06  Carsten Dominik  <carsten.dominik@gmail.com>
 
+	* org-export-latex.el (org-export-latex-special-chars): Fix
+	problems with interpreting dollar signs.
+	(org-inside-latex-math-p): New function.
+	(org-export-latex-preprocess): Protect all the math fragments.
+
+	* org.el (org-latex-regexps): Allow a dash after a dollar.
+
 	* org-w3m.el (org-w3m-copy-for-org-mode): Always deactivate the
 	mark after copying.
 

+ 26 - 8
lisp/org-export-latex.el

@@ -883,10 +883,8 @@ See the `org-export-latex.el' code for a complete conversion table."
 	    (unless (get-text-property (match-beginning 2) 'org-protected)
 	      (cond ((member (match-string 2) '("\\$" "$"))
 		     (if (equal (match-string 2) "\\$")
-			 (replace-match (concat (match-string 1) "$"
-						(match-string 3)) t t)
-		       (replace-match (concat (match-string 1) "\\$"
-					      (match-string 3)) t t)))
+			 nil
+		       (replace-match "\\$" t t)))
 		    ((member (match-string 2) '("&" "%" "#"))
 		     (if (equal (match-string 1) "\\")
 			 (replace-match (match-string 2) t t)
@@ -904,12 +902,12 @@ See the `org-export-latex.el' code for a complete conversion table."
 			       (org-export-latex-protect-string
 				(concat (match-string 1) "\\~{}")) t t))))
 		    ((member (match-string 2) '("{" "}"))
-		     (unless (save-match-data (org-inside-LaTeX-fragment-p))
+		     (unless (save-match-data (org-inside-latex-math-p))
 		       (if (equal (match-string 1) "\\")
 			   (replace-match (match-string 2) t t)
 			 (replace-match (concat (match-string 1) "\\"
 						(match-string 2)) t t)))))
-	      (unless (save-match-data (org-inside-LaTeX-fragment-p))
+	      (unless (save-match-data (org-inside-latex-math-p))
 		(cond ((equal (match-string 2) "\\")
 		       (replace-match (or (save-match-data
 					    (org-export-latex-treat-backslash-char
@@ -923,7 +921,8 @@ See the `org-export-latex.el' code for a complete conversion table."
 					     (match-string 2)
 					     (match-string 1)
 					     (match-string 3))) "") t t)))))))
-	'("^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
+	'(;"^\\([^\n$]*?\\|^\\)\\(\\\\?\\$\\)\\([^\n$]*\\)$"
+	  "\\(\\(\\\\?\\$\\)\\)"
 	  "\\([a-za-z0-9]+\\|[ \t\n]\\|\\b\\|\\\\\\)\\(_\\|\\^\\)\\([a-za-z0-9]+\\|[ \t\n]\\|[:punct:]\\|{[a-za-z0-9]+}\\|([a-za-z0-9]+)\\)"
 	  "\\(.\\|^\\)\\(\\\\\\)\\([ \t\n]\\|[a-zA-Z&#%{}\"]+\\)"
 	  "\\(.\\|^\\)\\(&\\)"
@@ -937,6 +936,9 @@ See the `org-export-latex.el' code for a complete conversion table."
 	  ;; (?\> . "\\textgreater{}")
 	  )))
 
+(defun org-inside-latex-math-p ()
+  (get-text-property (point) 'org-latex-math))
+
 (defun org-export-latex-treat-sub-super-char
   (subsup char string-before string-after)
   "Convert the \"_\" and \"^\" characters to LaTeX.
@@ -949,7 +951,7 @@ Convert CHAR depending on STRING-BEFORE and STRING-AFTER."
 	      (string-match "\\S-+" string-after))
 	 (cond ((eq 'org-link (get-text-property 0 'face char))
 		(concat string-before "\\" char string-after))
-	       ((save-match-data (org-inside-LaTeX-fragment-p))
+	       ((save-match-data (org-inside-latex-math-p))
 		(if subsup
 		    (cond ((eq 1 (length string-after))
 			   (concat string-before char string-after))
@@ -1248,6 +1250,22 @@ If TIMESTAMPS, convert timestamps, otherwise delete them."
 		    (point-max))))
       (add-text-properties start end '(org-protected t))))
 
+  ;; Preserve math snippets
+
+  (let* ((matchers (plist-get org-format-latex-options :matchers))
+	 (re-list org-latex-regexps)
+	 beg end re e m n block off)
+    ;; 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 off (if (equal m "$") 1 0))
+      (when (and (member m matchers) (not (equal m "begin")))
+	(goto-char (point-min))
+	(while (re-search-forward re nil t)
+	  (setq beg (+ (match-beginning 0) off) end (- (match-end 0) off))
+	  (add-text-properties beg end '(org-protected t org-latex-math t))))))
+
   ;; Convert LaTeX to \LaTeX{}
   (goto-char (point-min))
   (let ((case-fold-search nil) rpl)

+ 1 - 1
lisp/org.el

@@ -12444,7 +12444,7 @@ The images can be removed again with \\[org-ctrl-c-ctrl-c]."
   '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
     ;; ("$" "\\([ 	(]\\|^\\)\\(\\(\\([$]\\)\\([^ 	\r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ 	\r\n,.$]\\)\\4\\)\\)\\([ 	.,?;:'\")]\\|$\\)" 2 nil)
     ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
-    ("$" "\\([^$]\\)\\(\\(\\$\\([^ 	\r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ 	\r\n,.$]\\)\\$\\)\\)\\([ 	.,?;:'\")\000]\\|$\\)" 2 nil)
+    ("$" "\\([^$]\\)\\(\\(\\$\\([^ 	\r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ 	\r\n,.$]\\)\\$\\)\\)\\([- 	.,?;:'\")\000]\\|$\\)" 2 nil)
     ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
     ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
     ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))