aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJay Belanger2005-01-31 06:30:37 +0000
committerJay Belanger2005-01-31 06:30:37 +0000
commitddaad6092eb67e0922a76c516e3badfe6ddb22f7 (patch)
tree348450128d39fcd935a469ed416b7948131c3d73
parentad1c32c76f21950d53291fc5b9c6397ca358e982 (diff)
downloademacs-ddaad6092eb67e0922a76c516e3badfe6ddb22f7.tar.gz
emacs-ddaad6092eb67e0922a76c516e3badfe6ddb22f7.zip
(math-compose-expr, math-compose-rows): Add LaTeX support.
(math-compose-expr): Add support for special functions.
-rw-r--r--lisp/calc/calccomp.el116
1 files changed, 68 insertions, 48 deletions
diff --git a/lisp/calc/calccomp.el b/lisp/calc/calccomp.el
index e76b3a34e09..dc46159b09b 100644
--- a/lisp/calc/calccomp.el
+++ b/lisp/calc/calccomp.el
@@ -79,7 +79,8 @@
79 79
80 80
81(defun math-compose-expr (a prec) 81(defun math-compose-expr (a prec)
82 (let ((math-compose-level (1+ math-compose-level))) 82 (let ((math-compose-level (1+ math-compose-level))
83 spfn)
83 (cond 84 (cond
84 ((or (and (eq a math-comp-selected) a) 85 ((or (and (eq a math-comp-selected) a)
85 (and math-comp-tagged 86 (and math-comp-tagged
@@ -89,10 +90,13 @@
89 (list 'tag a (math-compose-expr a prec)))) 90 (list 'tag a (math-compose-expr a prec))))
90 ((and (not (consp a)) (not (integerp a))) 91 ((and (not (consp a)) (not (integerp a)))
91 (concat "'" (prin1-to-string a))) 92 (concat "'" (prin1-to-string a)))
93 ((setq spfn (assq (car-safe a) math-expr-special-function-mapping))
94 (setq spfn (cdr spfn))
95 (funcall (car spfn) a spfn))
92 ((math-scalarp a) 96 ((math-scalarp a)
93 (if (or (eq (car-safe a) 'frac) 97 (if (or (eq (car-safe a) 'frac)
94 (and (nth 1 calc-frac-format) (Math-integerp a))) 98 (and (nth 1 calc-frac-format) (Math-integerp a)))
95 (if (memq calc-language '(tex eqn math maple c fortran pascal)) 99 (if (memq calc-language '(tex latex eqn math maple c fortran pascal))
96 (let ((aa (math-adjust-fraction a)) 100 (let ((aa (math-adjust-fraction a))
97 (calc-frac-format nil)) 101 (calc-frac-format nil))
98 (math-compose-expr (list '/ 102 (math-compose-expr (list '/
@@ -265,34 +269,44 @@
265 (append '(horiz "\\matrix{ ") 269 (append '(horiz "\\matrix{ ")
266 (math-compose-tex-matrix (cdr a)) 270 (math-compose-tex-matrix (cdr a))
267 '(" }")) 271 '(" }"))
268 (if (and (eq calc-language 'eqn) 272 (if (and (eq calc-language 'latex)
269 (math-matrixp a)) 273 (math-matrixp a))
270 (append '(horiz "matrix { ") 274 (if (memq calc-language-option '(-2 0 2))
271 (math-compose-eqn-matrix 275 (append '(vleft 0 "\\begin{pmatrix}")
272 (cdr (math-transpose a))) 276 (math-compose-tex-matrix (cdr a))
273 '("}")) 277 '("\\end{pmatrix}"))
274 (if (and (eq calc-language 'maple) 278 (append '(horiz "\\begin{pmatrix} ")
275 (math-matrixp a)) 279 (math-compose-tex-matrix (cdr a))
276 (list 'horiz 280 '(" \\end{pmatrix}")))
277 "matrix(" 281 (if (and (eq calc-language 'eqn)
278 math-comp-left-bracket 282 (math-matrixp a))
279 (math-compose-vector (cdr a) 283 (append '(horiz "matrix { ")
284 (math-compose-eqn-matrix
285 (cdr (math-transpose a)))
286 '("}"))
287 (if (and (eq calc-language 'maple)
288 (math-matrixp a))
289 (list 'horiz
290 "matrix("
291 math-comp-left-bracket
292 (math-compose-vector (cdr a)
293 (concat math-comp-comma " ")
294 math-comp-vector-prec)
295 math-comp-right-bracket
296 ")")
297 (list 'horiz
298 math-comp-left-bracket
299 (math-compose-vector (cdr a)
280 (concat math-comp-comma " ") 300 (concat math-comp-comma " ")
281 math-comp-vector-prec) 301 math-comp-vector-prec)
282 math-comp-right-bracket 302 math-comp-right-bracket)))))
283 ")")
284 (list 'horiz
285 math-comp-left-bracket
286 (math-compose-vector (cdr a)
287 (concat math-comp-comma " ")
288 math-comp-vector-prec)
289 math-comp-right-bracket))))
290 (list 'horiz 303 (list 'horiz
291 math-comp-left-bracket 304 math-comp-left-bracket
292 (math-compose-vector (list (nth 1 a) (nth 2 a) (nth 3 a)) 305 (math-compose-vector (list (nth 1 a) (nth 2 a) (nth 3 a))
293 (concat math-comp-comma " ") 306 (concat math-comp-comma " ")
294 math-comp-vector-prec) 307 math-comp-vector-prec)
295 math-comp-comma (if (eq calc-language 'tex) " \\ldots" " ...") 308 math-comp-comma (if (memq calc-language '(tex latex))
309 " \\ldots" " ...")
296 math-comp-comma " " 310 math-comp-comma " "
297 (list 'break math-compose-level) 311 (list 'break math-compose-level)
298 (math-compose-expr (nth (1- (length a)) a) 312 (math-compose-expr (nth (1- (length a)) a)
@@ -326,12 +340,14 @@
326 (let ((v (rassq (nth 2 a) math-expr-variable-mapping))) 340 (let ((v (rassq (nth 2 a) math-expr-variable-mapping)))
327 (if v 341 (if v
328 (symbol-name (car v)) 342 (symbol-name (car v))
329 (if (and (eq calc-language 'tex) 343 (if (and (memq calc-language '(tex latex))
330 calc-language-option 344 calc-language-option
331 (not (= calc-language-option 0)) 345 (not (= calc-language-option 0))
332 (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'" 346 (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'"
333 (symbol-name (nth 1 a)))) 347 (symbol-name (nth 1 a))))
334 (format "\\hbox{%s}" (symbol-name (nth 1 a))) 348 (if (eq calc-language 'latex)
349 (format "\\text{%s}" (symbol-name (nth 1 a)))
350 (format "\\hbox{%s}" (symbol-name (nth 1 a))))
335 (if (and math-compose-hash-args 351 (if (and math-compose-hash-args
336 (let ((p calc-arg-values)) 352 (let ((p calc-arg-values))
337 (setq v 1) 353 (setq v 1)
@@ -359,7 +375,7 @@
359 (if (eq calc-language 'maple) "" 375 (if (eq calc-language 'maple) ""
360 (if (memq (nth 1 a) '(0 1)) "(" "[")) 376 (if (memq (nth 1 a) '(0 1)) "(" "["))
361 (math-compose-expr (nth 2 a) 0) 377 (math-compose-expr (nth 2 a) 0)
362 (if (eq calc-language 'tex) " \\ldots " 378 (if (memq calc-language '(tex latex)) " \\ldots "
363 (if (eq calc-language 'eqn) " ... " " .. ")) 379 (if (eq calc-language 'eqn) " ... " " .. "))
364 (math-compose-expr (nth 3 a) 0) 380 (math-compose-expr (nth 3 a) 0)
365 (if (eq calc-language 'maple) "" 381 (if (eq calc-language 'maple) ""
@@ -404,7 +420,7 @@
404 (math-compose-expr (nth 2 a) 0) 420 (math-compose-expr (nth 2 a) 0)
405 "]]")) 421 "]]"))
406 ((and (eq (car a) 'calcFunc-sqrt) 422 ((and (eq (car a) 'calcFunc-sqrt)
407 (eq calc-language 'tex)) 423 (memq calc-language '(tex latex)))
408 (list 'horiz 424 (list 'horiz
409 "\\sqrt{" 425 "\\sqrt{"
410 (math-compose-expr (nth 1 a) 0) 426 (math-compose-expr (nth 1 a) 0)
@@ -440,7 +456,7 @@
440 (math-comp-height a1) 456 (math-comp-height a1)
441 a1 '(rule ?-) a2))) 457 a1 '(rule ?-) a2)))
442 ((and (memq (car a) '(calcFunc-sum calcFunc-prod)) 458 ((and (memq (car a) '(calcFunc-sum calcFunc-prod))
443 (eq calc-language 'tex) 459 (memq calc-language '(tex latex))
444 (= (length a) 5)) 460 (= (length a) 5))
445 (list 'horiz (if (eq (car a) 'calcFunc-sum) "\\sum" "\\prod") 461 (list 'horiz (if (eq (car a) 'calcFunc-sum) "\\sum" "\\prod")
446 "_{" (math-compose-expr (nth 2 a) 0) 462 "_{" (math-compose-expr (nth 2 a) 0)
@@ -495,7 +511,7 @@
495 (integerp (nth 2 a))) 511 (integerp (nth 2 a)))
496 (let ((c (math-compose-expr (nth 1 a) -1))) 512 (let ((c (math-compose-expr (nth 1 a) -1)))
497 (if (> prec (nth 2 a)) 513 (if (> prec (nth 2 a))
498 (if (eq calc-language 'tex) 514 (if (memq calc-language '(tex latex))
499 (list 'horiz "\\left( " c " \\right)") 515 (list 'horiz "\\left( " c " \\right)")
500 (if (eq calc-language 'eqn) 516 (if (eq calc-language 'eqn)
501 (list 'horiz "{left ( " c " right )}") 517 (list 'horiz "{left ( " c " right )}")
@@ -633,13 +649,13 @@
633 (make-list (nth 1 a) c)))))) 649 (make-list (nth 1 a) c))))))
634 ((and (eq (car a) 'calcFunc-evalto) 650 ((and (eq (car a) 'calcFunc-evalto)
635 (setq calc-any-evaltos t) 651 (setq calc-any-evaltos t)
636 (memq calc-language '(tex eqn)) 652 (memq calc-language '(tex latex eqn))
637 (= math-compose-level (if math-comp-tagged 2 1)) 653 (= math-compose-level (if math-comp-tagged 2 1))
638 (= (length a) 3)) 654 (= (length a) 3))
639 (list 'horiz 655 (list 'horiz
640 (if (eq calc-language 'tex) "\\evalto " "evalto ") 656 (if (memq calc-language '(tex latex)) "\\evalto " "evalto ")
641 (math-compose-expr (nth 1 a) 0) 657 (math-compose-expr (nth 1 a) 0)
642 (if (eq calc-language 'tex) " \\to " " -> ") 658 (if (memq calc-language '(tex latex)) " \\to " " -> ")
643 (math-compose-expr (nth 2 a) 0))) 659 (math-compose-expr (nth 2 a) 0)))
644 (t 660 (t
645 (let ((op (and (not (eq calc-language 'unform)) 661 (let ((op (and (not (eq calc-language 'unform))
@@ -651,7 +667,7 @@
651 (/= (nth 3 op) -1)) 667 (/= (nth 3 op) -1))
652 (cond 668 (cond
653 ((> prec (or (nth 4 op) (min (nth 2 op) (nth 3 op)))) 669 ((> prec (or (nth 4 op) (min (nth 2 op) (nth 3 op))))
654 (if (and (eq calc-language 'tex) 670 (if (and (memq calc-language '(tex latex))
655 (not (math-tex-expr-is-flat a))) 671 (not (math-tex-expr-is-flat a)))
656 (if (eq (car-safe a) '/) 672 (if (eq (car-safe a) '/)
657 (list 'horiz "{" (math-compose-expr a -1) "}") 673 (list 'horiz "{" (math-compose-expr a -1) "}")
@@ -668,7 +684,7 @@
668 (math-compose-expr a -1) 684 (math-compose-expr a -1)
669 " right )}"))) 685 " right )}")))
670 (list 'horiz "(" (math-compose-expr a 0) ")")))) 686 (list 'horiz "(" (math-compose-expr a 0) ")"))))
671 ((and (eq calc-language 'tex) 687 ((and (memq calc-language '(tex latex))
672 (memq (car a) '(/ calcFunc-choose calcFunc-evalto)) 688 (memq (car a) '(/ calcFunc-choose calcFunc-evalto))
673 (>= prec 0)) 689 (>= prec 0))
674 (list 'horiz "{" (math-compose-expr a -1) "}")) 690 (list 'horiz "{" (math-compose-expr a -1) "}"))
@@ -694,7 +710,7 @@
694 (and (equal (car op) "^") 710 (and (equal (car op) "^")
695 (eq (math-comp-first-char lhs) ?-) 711 (eq (math-comp-first-char lhs) ?-)
696 (setq lhs (list 'horiz "(" lhs ")"))) 712 (setq lhs (list 'horiz "(" lhs ")")))
697 (and (eq calc-language 'tex) 713 (and (memq calc-language '(tex latex))
698 (or (equal (car op) "^") (equal (car op) "_")) 714 (or (equal (car op) "^") (equal (car op) "_"))
699 (not (and (stringp rhs) (= (length rhs) 1))) 715 (not (and (stringp rhs) (= (length rhs) 1)))
700 (setq rhs (list 'horiz "{" rhs "}"))) 716 (setq rhs (list 'horiz "{" rhs "}")))
@@ -761,7 +777,7 @@
761 ((or (> prec (or (nth 4 op) (nth 2 op))) 777 ((or (> prec (or (nth 4 op) (nth 2 op)))
762 (and (not (eq (assoc (car op) math-expr-opers) op)) 778 (and (not (eq (assoc (car op) math-expr-opers) op))
763 (> prec 0))) ; don't write x% + y 779 (> prec 0))) ; don't write x% + y
764 (if (and (eq calc-language 'tex) 780 (if (and (memq calc-language '(tex latex))
765 (not (math-tex-expr-is-flat a))) 781 (not (math-tex-expr-is-flat a)))
766 (list 'horiz "\\left( " 782 (list 'horiz "\\left( "
767 (math-compose-expr a -1) 783 (math-compose-expr a -1)
@@ -786,7 +802,7 @@
786 ((and op (= (length a) 2) (= (nth 2 op) -1)) 802 ((and op (= (length a) 2) (= (nth 2 op) -1))
787 (cond 803 (cond
788 ((eq (nth 3 op) 0) 804 ((eq (nth 3 op) 0)
789 (let ((lr (and (eq calc-language 'tex) 805 (let ((lr (and (memq calc-language '(tex latex))
790 (not (math-tex-expr-is-flat (nth 1 a)))))) 806 (not (math-tex-expr-is-flat (nth 1 a))))))
791 (list 'horiz 807 (list 'horiz
792 (if lr "\\left" "") 808 (if lr "\\left" "")
@@ -799,7 +815,7 @@
799 (if lr "\\right" "") 815 (if lr "\\right" "")
800 (car (nth 1 (memq op math-expr-opers)))))) 816 (car (nth 1 (memq op math-expr-opers))))))
801 ((> prec (or (nth 4 op) (nth 3 op))) 817 ((> prec (or (nth 4 op) (nth 3 op)))
802 (if (and (eq calc-language 'tex) 818 (if (and (memq calc-language '(tex latex))
803 (not (math-tex-expr-is-flat a))) 819 (not (math-tex-expr-is-flat a)))
804 (list 'horiz "\\left( " 820 (list 'horiz "\\left( "
805 (math-compose-expr a -1) 821 (math-compose-expr a -1)
@@ -836,6 +852,7 @@
836 ( pascal . math-compose-pascal ) 852 ( pascal . math-compose-pascal )
837 ( fortran . math-compose-fortran ) 853 ( fortran . math-compose-fortran )
838 ( tex . math-compose-tex ) 854 ( tex . math-compose-tex )
855 ( latex . math-compose-latex )
839 ( eqn . math-compose-eqn ) 856 ( eqn . math-compose-eqn )
840 ( math . math-compose-math ) 857 ( math . math-compose-math )
841 ( maple . math-compose-maple )))) 858 ( maple . math-compose-maple ))))
@@ -866,20 +883,22 @@
866 (symbol-name func)))) 883 (symbol-name func))))
867 (if (memq calc-language '(c fortran pascal maple)) 884 (if (memq calc-language '(c fortran pascal maple))
868 (setq func (math-to-underscores func))) 885 (setq func (math-to-underscores func)))
869 (if (and (eq calc-language 'tex) 886 (if (and (memq calc-language '(tex latex))
870 calc-language-option 887 calc-language-option
871 (not (= calc-language-option 0)) 888 (not (= calc-language-option 0))
872 (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'" func)) 889 (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'" func))
873 (if (< (prefix-numeric-value calc-language-option) 0) 890 (if (< (prefix-numeric-value calc-language-option) 0)
874 (setq func (format "\\%s" func)) 891 (setq func (format "\\%s" func))
875 (setq func (format "\\hbox{%s}" func)))) 892 (setq func (if (eq calc-language 'latex)
893 (format "\\text{%s}" func)
894 (format "\\hbox{%s}" func)))))
876 (if (and (eq calc-language 'eqn) 895 (if (and (eq calc-language 'eqn)
877 (string-match "[^']'+\\'" func)) 896 (string-match "[^']'+\\'" func))
878 (let ((n (- (length func) (match-beginning 0) 1))) 897 (let ((n (- (length func) (match-beginning 0) 1)))
879 (setq func (substring func 0 (- n))) 898 (setq func (substring func 0 (- n)))
880 (while (>= (setq n (1- n)) 0) 899 (while (>= (setq n (1- n)) 0)
881 (setq func (concat func " prime"))))) 900 (setq func (concat func " prime")))))
882 (cond ((and (eq calc-language 'tex) 901 (cond ((and (eq calc-language '(tex latex))
883 (or (> (length a) 2) 902 (or (> (length a) 2)
884 (not (math-tex-expr-is-flat (nth 1 a))))) 903 (not (math-tex-expr-is-flat (nth 1 a)))))
885 (setq left "\\left( " 904 (setq left "\\left( "
@@ -889,11 +908,13 @@
889 (not (math-tex-expr-is-flat (nth 1 a))))) 908 (not (math-tex-expr-is-flat (nth 1 a)))))
890 (setq left "{left ( " 909 (setq left "{left ( "
891 right " right )}")) 910 right " right )}"))
892 ((and (or (and (eq calc-language 'tex) 911 ((and (or (and (memq calc-language '(tex latex))
893 (eq (aref func 0) ?\\)) 912 (eq (aref func 0) ?\\))
894 (and (eq calc-language 'eqn) 913 (and (eq calc-language 'eqn)
895 (memq (car a) math-eqn-special-funcs))) 914 (memq (car a) math-eqn-special-funcs)))
896 (not (string-match "\\hbox{" func)) 915 (not (or
916 (string-match "\\hbox{" func)
917 (string-match "\\text{" func)))
897 (= (length a) 2) 918 (= (length a) 2)
898 (or (Math-realp (nth 1 a)) 919 (or (Math-realp (nth 1 a))
899 (memq (car (nth 1 a)) '(var *)))) 920 (memq (car (nth 1 a)) '(var *))))
@@ -968,7 +989,7 @@
968 (if (<= count 0) 989 (if (<= count 0)
969 (if (< count 0) 990 (if (< count 0)
970 (math-compose-rows (cdr a) -1 nil) 991 (math-compose-rows (cdr a) -1 nil)
971 (cons (concat (if (eq calc-language 'tex) " \\ldots" " ...") 992 (cons (concat (if (memq calc-language '(tex latex)) " \\ldots" " ...")
972 math-comp-comma) 993 math-comp-comma)
973 (math-compose-rows (cdr a) -1 nil))) 994 (math-compose-rows (cdr a) -1 nil)))
974 (cons (list 'horiz 995 (cons (list 'horiz
@@ -983,9 +1004,8 @@
983 1004
984(defun math-compose-tex-matrix (a) 1005(defun math-compose-tex-matrix (a)
985 (if (cdr a) 1006 (if (cdr a)
986 (cons (math-compose-vector (cdr (car a)) " & " 0) 1007 (cons (append (math-compose-vector (cdr (car a)) " & " 0) '(" \\\\ "))
987 (cons " \\\\ " 1008 (math-compose-tex-matrix (cdr a)))
988 (math-compose-tex-matrix (cdr a))))
989 (list (math-compose-vector (cdr (car a)) " & " 0)))) 1009 (list (math-compose-vector (cdr (car a)) " & " 0))))
990 1010
991(defun math-compose-eqn-matrix (a) 1011(defun math-compose-eqn-matrix (a)