Eliminate spurious redefinition of derivabbrev in Ctensor, fix documentation of diagm...
[maxima/cygwin.git] / src / mactex.lisp
blobb14d45c8e6c968d519ad11c392ae76e713989df5
1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
3 (in-package :maxima)
5 ;; TeX-printing
6 ;; (c) copyright 1987, Richard J. Fateman
7 ;; small corrections and additions: Andrey Grozin, 2001
8 ;; additional additions: Judah Milgram (JM), September 2001
9 ;; additional corrections: Barton Willis (BLW), October 2001
11 ;; Usage: tex(d8,"/tmp/foo.tex"); tex(d10,"/tmp/foo.tex"); ..
12 ;; to append lines d8 and d10 to the tex file. If given only
13 ;; one argument the result goes to standard output.
15 ;; Extract from permission letter to wfs:
16 ;; Date: Sat, 2 Apr 88 18:06:16 PST
17 ;; From: fateman%vangogh.Berkeley.EDU@ucbvax.Berkeley.EDU (Richard Fateman)
18 ;; To: wfs@rascal.ics.UTEXAS.EDU
19 ;; Subject: about tex...
20 ;; You have my permission to put it in NESC or give it to anyone
21 ;; else who might be interested in it....
23 ;; source language:
24 ;; There are changes by wfs to allow use inside MAXIMA which runs
25 ;; in COMMON LISP. For original FRANZ LISP version contact rfw.
27 ;; intended environment: vaxima (Vax or Sun). Parser should be
28 ;; equivalent (in lbp/rbp data) to 1986 NESC Vaxima.
29 ;;;(provide 'tex)
30 ;;;(in-package 'tex)
31 ;;;(export '($tex $texinit))
32 ;;;;; we'd like to just
33 ;;;(import '(user::$bothcases user::lbp user::rbp user::nformat))
34 ;;;(use-package 'user)
36 ;; March, 1987
38 ;; Method:
40 ;; Producing TeX from a macsyma internal expression is done by
41 ;; a reversal of the parsing process. Fundamentally, a
42 ;; traversal of the expression tree is produced by the tex programs,
43 ;; with appropriate substitutions and recognition of the
44 ;; infix / prefix / postfix / matchfix relations on symbols. Various
45 ;; changes are made to this so that TeX will like the results.
46 ;; It is important to understand the binding powers of the operators
47 ;; in Macsyma, in mathematics, and in TeX so that parentheses will
48 ;; be inserted when necessary. Because TeX has different kinds of
49 ;; groupings (e.g. in superscripts, within sqrts), not all
50 ;; parentheses are explicitly need.
52 ;; Instructions:
53 ;; in macsyma, type tex(<expression>); or tex(<label>); or
54 ;; tex(<expr-or-label>, <file-name>); In the case of a label,
55 ;; a left-equation-number will be produced.
56 ;; in case a file-name is supplied, the output will be sent
57 ;; (perhaps appended) to that file.
59 (declare-top (special lop rop $labels $inchar))
61 (defvar *tex-environment-default* '("$$" . "$$"))
63 (defun $set_tex_environment_default (env-open env-close)
64 (setq env-open ($sconcat env-open))
65 (setq env-close ($sconcat env-close))
66 (setq *tex-environment-default* `(,env-open . ,env-close))
67 ($get_tex_environment_default))
69 (defun $get_tex_environment_default ()
70 `((mlist) ,(car *tex-environment-default*) ,(cdr *tex-environment-default*)))
72 (defun $set_tex_environment (x env-open env-close)
73 (setq env-open ($sconcat env-open))
74 (setq env-close ($sconcat env-close))
75 (if (getopr x) (setq x (getopr x)))
76 (setf (get x 'tex-environment) `(,env-open . ,env-close))
77 ($get_tex_environment x))
79 (defun $get_tex_environment (x)
80 (if (getopr x) (setq x (getopr x)))
81 (let ((e (get-tex-environment x)))
82 `((mlist) ,(car e) ,(cdr e))))
84 (defun get-tex-environment (x)
85 (cond
86 ((symbolp x)
87 (or (get x 'tex-environment) *tex-environment-default*))
88 ((atom x)
89 *tex-environment-default*)
91 (get-tex-environment (caar x)))))
93 (setf (get 'mdefine 'tex-environment)
94 `(,(format nil "~%\\begin{verbatim}~%") . ,(format nil ";~%\\end{verbatim}~%")))
96 (setf (get 'mdefmacro 'tex-environment)
97 `(,(format nil "~%\\begin{verbatim}~%") . ,(format nil ";~%\\end{verbatim}~%")))
99 (setf (get 'mlabel 'tex-environment)
100 `(,(format nil "~%\\begin{verbatim}~%") . ,(format nil ";~%\\end{verbatim}~%")))
102 ;; top level command the result of tex'ing the expression x.
103 ;; Lots of messing around here to get C-labels verbatim printed
104 ;; and function definitions verbatim "ground"
106 (defmspec $tex(l) ;; mexplabel, and optional filename or stream
107 ;;if filename or stream supplied but 'nil' then return a string
108 (let ((args (cdr l)))
109 (cond ((and (cdr args) (null (cadr args)))
110 (let ((*standard-output* (make-string-output-stream)))
111 (apply 'tex1 args)
112 (get-output-stream-string *standard-output*)
115 (t (apply 'tex1 args)))))
117 (defun quote-chars (sym ch-str)
118 (let* ((strsym (string sym))
119 (pos (position-if #'(lambda (c) (find c ch-str)) strsym)))
120 (if pos
121 (concatenate 'string (subseq strsym 0 pos) "\\" (subseq strsym pos (1+ pos))
122 (quote-chars (subseq strsym (1+ pos)) ch-str))
123 strsym)))
125 (defun quote-% (sym)
126 (quote-chars sym "$%&_"))
128 (defun tex1 (mexplabel &optional filename-or-stream) ;; mexplabel, and optional filename or stream
129 (prog (mexp texport x y itsalabel need-to-close-texport)
130 (reset-ccol)
131 (cond ((null mexplabel)
132 (displa " No eqn given to TeX")
133 (return nil)))
134 ;; collect the file-name, if any, and open a port if needed
135 (setq filename-or-stream (meval filename-or-stream))
136 (setq texport
137 (cond
138 ((null filename-or-stream) *standard-output*)
139 ((eq filename-or-stream t) *standard-output*)
140 ((streamp filename-or-stream) filename-or-stream)
142 (setq need-to-close-texport t)
143 (open (namestring (maxima-string filename-or-stream))
144 :direction :output
145 :if-exists :append
146 :if-does-not-exist :create))))
147 ;; go back and analyze the first arg more thoroughly now.
148 ;; do a normal evaluation of the expression in macsyma
149 (setq mexp (meval mexplabel))
150 (cond ((member mexplabel $labels :test #'eq) ; leave it if it is a label
151 (setq mexplabel (concatenate 'string "(" (print-invert-case (stripdollar mexplabel))
152 ")"))
153 (setq itsalabel t))
154 (t (setq mexplabel nil))) ;flush it otherwise
156 ;; maybe it is a function?
157 (cond((symbolp (setq x mexp)) ;;exclude strings, numbers
158 (setq x ($verbify x))
159 (cond ((setq y (mget x 'mexpr))
160 (setq mexp (list '(mdefine) (cons (list x) (cdadr y)) (caddr y))))
161 ((setq y (mget x 'mmacro))
162 (setq mexp (list '(mdefmacro) (cons (list x) (cdadr y)) (caddr y))))
163 ((setq y (mget x 'aexpr))
164 (setq mexp (list '(mdefine) (cons (list x 'array) (cdadr y)) (caddr y)))))))
165 (cond ((and (null(atom mexp))
166 (member (caar mexp) '(mdefine mdefmacro) :test #'eq))
167 (format texport (car (get-tex-environment (caar mexp))))
168 (cond (mexplabel (format texport "~a " mexplabel)))
169 (mgrind mexp texport) ;write expression as string
170 (format texport (cdr (get-tex-environment (caar mexp)))))
171 ((and
172 itsalabel ;; but is it a user-command-label?
173 ;; THE FOLLOWING TESTS SEEM PRETTY STRANGE --
174 ;; WHY CHECK INITIAL SUBSTRING IF SYMBOL IS ON THE $LABELS LIST ??
175 ;; PROBABLY IT IS A HOLDOVER FROM THE DAYS WHEN LABELS WERE C AND D INSTEAD OF %I AND %O
176 (<= (length (string $inchar)) (length (string mexplabel)))
177 (string= (subseq (maybe-invert-string-case (string $inchar)) 1 (length (string $inchar)))
178 (subseq (string mexplabel) 1 (length (string $inchar))))
179 ;; Check to make sure it isn't an outchar in disguise
180 (not
181 (and
182 (<= (length (string $outchar)) (length (string mexplabel)))
183 (string= (subseq (maybe-invert-string-case (string $outchar)) 1 (length (string $outchar)))
184 (subseq (string mexplabel) 1 (length (string $outchar)))))))
185 ;; aha, this is a C-line: do the grinding:
186 (format texport (car (get-tex-environment 'mlabel)))
187 (format texport "~a" mexplabel)
188 (mgrind mexp texport) ;write expression as string
189 (format texport (cdr (get-tex-environment 'mlabel))))
191 (if mexplabel (setq mexplabel (quote-% mexplabel)))
192 ; display the expression for TeX now:
193 (myprinc (car (get-tex-environment mexp)) texport)
194 (mapc #'(lambda (x) (myprinc x texport))
195 ;;initially the left and right contexts are
196 ;; empty lists, and there are implicit parens
197 ;; around the whole expression
198 (tex mexp nil nil 'mparen 'mparen))
199 (cond (mexplabel
200 (format texport "\\leqno{\\tt ~a}" mexplabel)))
201 (format texport (cdr (get-tex-environment mexp)))))
202 (terpri texport)
203 (if need-to-close-texport
204 (close texport))
205 (return mexplabel)))
207 ;;; myprinc is an intelligent low level printing routine. it keeps track of
208 ;;; the size of the output for purposes of allowing the TeX file to
209 ;;; have a reasonable line-line. myprinc will break it at a space
210 ;;; once it crosses a threshold.
211 ;;; this has nothign to do with breaking the resulting equations.
213 ;;- arg: chstr - string or number to princ
214 ;;- scheme: This function keeps track of the current location
215 ;;- on the line of the cursor and makes sure
216 ;;- that a value is all printed on one line (and not divided
217 ;;- by the crazy top level os routines)
219 (let ((ccol 1))
220 (defun reset-ccol () (setq ccol 1))
222 (defun myprinc (chstr &optional (texport nil))
223 (prog (chlst)
224 (cond ((and (> (+ (length (setq chlst (exploden chstr))) ccol) 70.)
225 (or (stringp chstr) (equal chstr '| |)))
226 (terpri texport) ;would have exceeded the line length
227 (setq ccol 1.)
228 (myprinc " " texport))) ; lead off with a space for safetyso we split it up.
229 (do ((ch chlst (cdr ch))
230 (colc ccol (1+ colc)))
231 ((null ch) (setq ccol colc))
232 (write-char (car ch) texport)))))
234 (defun tex (x l r lop rop)
235 ;; x is the expression of interest; l is the list of strings to its
236 ;; left, r to its right. lop and rop are the operators on the left
237 ;; and right of x in the tree, and will determine if parens must
238 ;; be inserted
239 (setq x (nformat x))
240 (cond ((atom x) (tex-atom x l r))
241 ((or (<= (tex-lbp (caar x)) (tex-rbp lop)) (> (tex-lbp rop) (tex-rbp (caar x))))
242 (tex-paren x l r))
243 ;; special check needed because macsyma notates arrays peculiarly
244 ((member 'array (cdar x) :test #'eq) (tex-array x l r))
245 ;; dispatch for object-oriented tex-ifiying
246 ((get (caar x) 'tex) (funcall (get (caar x) 'tex) x l r))
247 (t (tex-function x l r nil))))
249 (defun tex-atom (x l r) ;; atoms: note: can we lose by leaving out {}s ?
250 (append l
251 (list (cond ((numberp x) (texnumformat x))
252 ((and (symbolp x) (or (get x 'texword) (get (get x 'reversealias) 'texword))))
253 ((stringp x)
254 (tex-string (quote-% (if $stringdisp (concatenate 'string "``" x "''") x))))
255 ((characterp x) (tex-char x))
256 ((not ($mapatom x))
257 (let ((x (if (member (marray-type x) '(array hash-table $functional))
258 ($sconcat x)
259 (format nil "~A" x))))
260 (tex-string (quote-chars (if $stringdisp (concatenate 'string "``" x "''") x)
261 "#$%&_"))))
263 (t (tex-stripdollar (or (get x 'reversealias) x)))))
266 (defun tex-string (x)
267 (cond ((equal x "") "")
268 ((eql (elt x 0) #\\) x)
269 (t (concatenate 'string "\\mbox{ " x " }"))))
271 (defun tex-char (x)
272 (if (eql x #\|) "\\mbox{\\verb/|/}"
273 (concatenate 'string "\\mbox{\\verb|" (string x) "|}")))
275 (defvar *tex-translations* nil)
276 ;; '(("ab" . "a")("x" . "x")) would cause AB12 and X3 C4 to print a_{12} and x_3 C_4
278 ;; Read forms from file F1 and output them to F2
279 (defun tex-forms (f1 f2 &aux tem (eof *mread-eof-obj*))
280 (with-open-file (st f1)
281 (loop while (not (eq (setq tem (mread-raw st eof)) eof))
282 do (tex1 (third tem) f2))))
284 (defun tex-stripdollar (x)
285 (let ((s (maybe-invert-string-case (symbol-name (tex-stripdollar0 x)))))
286 (if (> (length s) 1)
287 (concatenate 'string "{\\it " s "}")
288 s)))
290 (defun tex-stripdollar0 (sym &aux )
291 (or (symbolp sym) (return-from tex-stripdollar0 sym))
292 (let* ((pname (quote-% (stripdollar sym)))
293 (l (length pname))
294 (begin-sub
295 (loop for i downfrom (1- l)
296 when (not (digit-char-p (aref pname i)))
297 do (return (1+ i))))
298 (tem (make-array (+ l 4) :element-type ' #.(array-element-type "abc") :fill-pointer 0)))
299 (loop for i below l
301 (cond ((eql i begin-sub)
302 (let ((a (assoc tem *tex-translations* :test 'equal)))
303 (cond (a
304 (setq a (cdr a))
305 (setf (fill-pointer tem) 0)
306 (loop for i below (length a)
308 (vector-push (aref a i) tem)))))
309 (vector-push #\_ tem)
310 (unless (eql i (- l 1))
311 (vector-push #\{ tem)
312 (setq begin-sub t))))
313 (vector-push (aref pname i) tem)
314 finally
315 (cond ((eql begin-sub t)
316 (vector-push #\} tem))))
317 (intern tem)))
319 (defun strcat (&rest args)
320 (apply #'concatenate 'string (mapcar #'string args)))
322 ;; 10/14/87 RJF convert 1.2e20 to 1.2 \cdot 10^{20}
323 ;; 03/30/01 RLT make that 1.2 \times 10^{20}
324 (defun texnumformat(atom)
325 (let (r firstpart exponent)
326 (cond ((integerp atom)
327 (coerce (exploden atom) 'string))
329 (setq r (exploden atom))
330 (setq exponent (member 'e r :test #'string-equal)) ;; is it ddd.ddde+EE
331 (cond
332 ((null exponent)
333 (coerce r 'string))
335 (setq firstpart
336 (nreverse (cdr (member 'e (reverse r) :test #'string-equal))))
337 (strcat (apply #'strcat firstpart )
338 " \\times 10^{"
339 (apply #'strcat (cdr exponent))
340 "}")))))))
342 (defun tex-paren (x l r)
343 (tex x (append l '("\\left(")) (cons "\\right)" r) 'mparen 'mparen))
345 (defun tex-array (x l r)
346 (let ((f))
347 (if (eq 'mqapply (caar x))
348 (setq f (cadr x)
349 x (cdr x)
350 l (tex f (append l (list "\\left(")) (list "\\right)") 'mparen 'mparen))
351 (setq f (caar x)
352 l (tex f l nil lop 'mfunction)))
353 (setq
354 r (nconc (tex-list (cdr x) nil (list "}") ",") r))
355 (nconc l (list "_{") r )))
357 ;; we could patch this so sin x rather than sin(x), but instead we made sin a prefix
358 ;; operator
360 (defun tex-function (x l r op) op
361 (setq l (tex (caar x) l nil 'mparen 'mparen)
362 r (tex (cons '(mprogn) (cdr x)) nil r 'mparen 'mparen))
363 (nconc l r))
365 ;; set up a list , separated by symbols (, * ...) and then tack on the
366 ;; ending item (e.g. "]" or perhaps ")"
368 (defun tex-list (x l r sym)
369 (if (null x) r
370 (do ((nl))
371 ((null (cdr x))
372 (setq nl (nconc nl (tex (car x) l r 'mparen 'mparen)))
374 (setq nl (nconc nl (tex (car x) l (list sym) 'mparen 'mparen))
375 x (cdr x)
376 l nil))))
378 (defun tex-prefix (x l r)
379 (tex (cadr x) (append l (texsym (caar x))) r (caar x) rop))
381 (defun tex-infix (x l r)
382 ;; check for 2 args
383 (if (or (null (cddr x)) (cdddr x)) (wna-err (caar x)))
384 (setq l (tex (cadr x) l nil lop (caar x)))
385 (tex (caddr x) (append l (texsym (caar x))) r (caar x) rop))
387 (defun tex-postfix (x l r)
388 (tex (cadr x) l (append (texsym (caar x)) r) lop (caar x)))
390 (defun tex-nary (x l r)
391 (let* ((op (caar x)) (sym (texsym op)) (y (cdr x)) (ext-lop lop) (ext-rop rop))
392 (cond ((null y) (tex-function x l r t)) ; this should not happen
393 ((null (cdr y)) (tex-function x l r t)) ; this should not happen, too
394 (t (do ((nl) (lop ext-lop op) (rop op (if (null (cdr y)) ext-rop op)))
395 ((null (cdr y)) (setq nl (append nl (tex (car y) l r lop rop))) nl)
396 (setq nl (append nl (tex (car y) l sym lop rop))
397 y (cdr y)
398 l nil))))))
400 (defun tex-nofix (x l r) (tex (car (texsym (caar x))) l r (caar x) rop))
402 (defun tex-matchfix (x l r)
403 (setq l (append l (car (texsym (caar x))))
404 ;; car of texsym of a matchfix operator is the lead op
405 r (append (list (nth 1 (texsym (caar x)))) r)
406 ;; cdr is the trailing op
407 x (tex-list (cdr x) nil r (or (nth 2 (texsym (caar x))) " , ")))
408 (append l x))
410 (defun texsym (x)
411 (or (get x 'texsym) (get x 'strsym)
412 (get x 'dissym)
413 (stripdollar x)))
415 (defun texword (x)
416 (or (get x 'texword)
417 (stripdollar x)))
419 (defprop bigfloat tex-bigfloat tex)
421 ; For 1.2345b678, generate TeX output 1.2345_B \times 10^{678} .
422 ; If the exponent is 0, then ... \times 10^{0} is generated
423 ; (no attempt to strip off zero exponent).
425 (defun tex-bigfloat (x l r)
426 (let ((formatted (fpformat x)))
427 ; There should always be a '|b| or '|B| in the FPFORMAT output.
428 ; Play it safe -- check anyway.
429 (if (or (find '|b| formatted) (find '|B| formatted))
430 (let*
431 ((spell-out-expt
432 (append
433 (apply #'append
434 (mapcar
435 #'(lambda (e) (if (or (eq e '|b|) (eq e '|B|))
436 '("_B" | | "\\times" | | "10^{")
437 (list e)))
438 formatted))
439 '(|}|))))
440 (append l spell-out-expt r))
441 (append l formatted r))))
443 (defprop mprog "\\mathbf{block}\\;" texword)
444 (defprop %erf "\\mathrm{erf}" texword)
445 (defprop $erf "\\mathrm{erf}" texword) ;; etc for multicharacter names
446 (defprop $true "\\mathbf{true}" texword)
447 (defprop $false "\\mathbf{false}" texword)
448 (defprop $done "\\mathbf{done}" texword)
450 (defprop mprogn tex-matchfix tex) ;; mprogn is (<progstmnt>, ...)
451 (defprop mprogn (("\\left(") "\\right)") texsym)
453 (defprop mlist tex-matchfix tex)
454 (defprop mlist (("\\left[ ")" \\right] ") texsym)
456 ;;absolute value
457 (defprop mabs tex-matchfix tex)
458 (defprop mabs (("\\left| ")"\\right| ") texsym)
460 (defprop mqapply tex-mqapply tex)
462 (defun tex-mqapply (x l r)
463 (setq l (tex (cadr x) l (list "(" ) lop 'mfunction)
464 r (tex-list (cddr x) nil (cons ")" r) ","))
465 (append l r)) ;; fixed 9/24/87 RJF
467 (defprop $%i "i" texword)
468 (defprop $%e "e" texword)
469 (defprop $inf "\\infty " texword)
470 (defprop $minf " -\\infty " texword)
471 (defprop %laplace "\\mathcal{L}" texword)
473 (defprop $alpha "\\alpha" texword)
474 (defprop $beta "\\beta" texword)
475 (defprop $gamma "\\gamma" texword)
476 (defprop %gamma "\\gamma" texword)
478 (defprop %gamma tex-gamma tex)
479 (defun tex-gamma (x l r)
480 (tex (cadr x) (append l '("\\Gamma\\left(")) (append '("\\right)") r) 'mparen 'mparen))
482 (defprop $%gamma "\\gamma" texword)
483 (defprop %gamma_incomplete "\\Gamma" texword)
484 (defprop %gamma_incomplete_regularized "Q" texword)
485 (defprop %gamma_incomplete_generalized "\\Gamma" texword)
486 (defprop $gamma_greek "\\gamma" texword)
487 (defprop $delta "\\delta" texword)
488 (defprop $epsilon "\\varepsilon" texword)
489 (defprop $zeta "\\zeta" texword)
490 (defprop $eta "\\eta" texword)
491 (defprop $theta "\\vartheta" texword)
492 (defprop $iota "\\iota" texword)
493 (defprop $kappa "\\kappa" texword)
494 (defprop lambda "\\lambda" texword)
495 (defprop $lambda "\\lambda" texword)
496 (defprop $mu "\\mu" texword)
497 (defprop $nu "\\nu" texword)
498 (defprop $xi "\\xi" texword)
499 (defprop $omicron " o" texword)
500 (defprop $%pi "\\pi" texword)
501 (defprop $pi "\\pi" texword)
502 (defprop $rho "\\rho" texword)
503 (defprop $sigma "\\sigma" texword)
504 (defprop $tau "\\tau" texword)
505 (defprop $upsilon "\\upsilon" texword)
506 (defprop $phi "\\varphi" texword)
507 (defprop $chi "\\chi" texword)
508 (defprop $psi "\\psi" texword)
509 (defprop $omega "\\omega" texword)
511 (defprop |$Alpha| "{\\rm A}" texword)
512 (defprop |$Beta| "{\\rm B}" texword)
513 (defprop |$Gamma| "\\Gamma" texword)
514 (defprop |$Delta| "\\Delta" texword)
515 (defprop |$Epsilon| "{\\rm E}" texword)
516 (defprop |$Zeta| "{\\rm Z}" texword)
517 (defprop |$Eta| "{\\rm H}" texword)
518 (defprop |$Theta| "\\Theta" texword)
519 (defprop |$Iota| "{\\rm I}" texword)
520 (defprop |$Kappa| "{\\rm K}" texword)
521 (defprop |$Lambda| "\\Lambda" texword)
522 (defprop |$Mu| "{\\rm M}" texword)
523 (defprop |$Nu| "{\\rm N}" texword)
524 (defprop |$Xi| "\\Xi" texword)
525 (defprop |$Omicron| "{\\rm O}" texword)
526 (defprop |$Pi| "\\Pi" texword)
527 (defprop |$Rho| "{\\rm P}" texword)
528 (defprop |$Sigma| "\\Sigma" texword)
529 (defprop |$Tau| "{\\rm T}" texword)
530 (defprop |$Upsilon| "\\Upsilon" texword)
531 (defprop |$Phi| "\\Phi" texword)
532 (defprop |$Chi| "{\\rm X}" texword)
533 (defprop |$Psi| "\\Psi" texword)
534 (defprop |$Omega| "\\Omega" texword)
536 (defprop mquote tex-prefix tex)
537 (defprop mquote ("\\mbox{{}'{}}") texsym)
539 (defprop msetq tex-infix tex)
540 (defprop msetq (":") texsym)
542 (defprop mset tex-infix tex)
543 (defprop mset ("::") texsym)
545 (defprop mdefine tex-infix tex)
546 (defprop mdefine (":=") texsym)
548 (defprop mdefmacro tex-infix tex)
549 (defprop mdefmacro ("::=") texsym)
551 (defprop marrow tex-infix tex)
552 (defprop marrow ("\\rightarrow ") texsym)
554 (defprop mfactorial tex-postfix tex)
555 (defprop mfactorial ("!") texsym)
557 (defprop mexpt tex-mexpt tex)
559 (defprop %sum 110. tex-rbp) ;; added by BLW, 1 Oct 2001
560 (defprop %product 115. tex-rbp) ;; added by BLW, 1 Oct 2001
562 ;; If the number contains a exponent marker when printed, we need to
563 ;; put parens around it.
564 (defun numneedsparen (number)
565 (unless (integerp number)
566 (let ((r (exploden number)))
567 (member 'e r :test #'string-equal))))
569 ;; insert left-angle-brackets for mncexpt. a^<n> is how a^^n looks.
570 (defun tex-mexpt (x l r)
571 (let((nc (eq (caar x) 'mncexpt))) ; true if a^^b rather than a^b
572 ;; here is where we have to check for f(x)^b to be displayed
573 ;; as f^b(x), as is the case for sin(x)^2 .
574 ;; which should be sin^2 x rather than (sin x)^2 or (sin(x))^2.
575 ;; yet we must not display (a+b)^2 as +^2(a,b)...
576 ;; or (sin(x))^(-1) as sin^(-1)x, which would be arcsine x
577 (cond ;; this whole clause
578 ;; should be deleted if this hack is unwanted and/or the
579 ;; time it takes is of concern.
580 ;; it shouldn't be too expensive.
581 ((and (eq (caar x) 'mexpt) ; don't do this hack for mncexpt
582 (let*
583 ((fx (cadr x)) ; this is f(x)
584 (f (and (not (atom fx)) (atom (caar fx)) (caar fx))) ; this is f [or nil]
585 (bascdr (and f (cdr fx))) ; this is (x) [maybe (x,y..), or nil]
586 (expon (caddr x)) ;; this is the exponent
587 (doit (and
588 f ; there is such a function
589 (member (getcharn f 1) '(#\% #\$)) ;; insist it is a % or $ function
590 (not (member 'array (cdar fx) :test #'eq)) ; fix for x[i]^2
591 (not (member f '(%sum %product %derivative %integrate %at
592 %lsum %limit $pderivop) :test #'eq)) ;; what else? what a hack...
593 (or (and (atom expon) (not (numberp expon))) ; f(x)^y is ok
594 (and (atom expon) (numberp expon) (> expon 0))))))
595 ; f(x)^3 is ok, but not f(x)^-1, which could
596 ; inverse of f, if written f^-1 x
597 ; what else? f(x)^(1/2) is sqrt(f(x)), ??
598 (cond (doit
599 (setq l (tex `((mexpt) ,f ,expon) l nil 'mparen 'mparen))
600 (if (and (null (cdr bascdr))
601 (eq (get f 'tex) 'tex-prefix))
602 (setq r (tex (car bascdr) nil r f 'mparen))
603 (setq r (tex (cons '(mprogn) bascdr) nil r 'mparen 'mparen))))
604 (t nil))))) ; won't doit. fall through
605 (t (setq l (cond ((or ($bfloatp (cadr x))
606 (and (numberp (cadr x)) (numneedsparen (cadr x))))
607 ; ACTUALLY THIS TREATMENT IS NEEDED WHENEVER (CAAR X) HAS GREATER BINDING POWER THAN MTIMES ...
608 (tex (cadr x) (append l '("\\left(")) '("\\right)") lop (caar x)))
609 (t (tex (cadr x) l nil lop (caar x))))
610 r (if (mmminusp (setq x (nformat (caddr x))))
611 ;; the change in base-line makes parens unnecessary
612 (if nc
613 (tex (cadr x) '("^ {-\\langle ") (cons "\\rangle }" r) 'mparen 'mparen)
614 (tex (cadr x) '("^ {- ") (cons " }" r) 'mminus 'mparen))
615 (if nc
616 (tex x (list "^{\\langle ") (cons "\\rangle}" r) 'mparen 'mparen)
617 (if (and (integerp x) (< x 10))
618 (tex x (list "^")(cons "" r) 'mparen 'mparen)
619 (tex x (list "^{")(cons "}" r) 'mparen 'mparen)))))))
620 (append l r)))
622 (defprop mncexpt tex-mexpt tex)
624 (defprop mnctimes tex-nary tex)
625 (defprop mnctimes ("\\cdot ") texsym)
627 (defprop mtimes tex-nary tex)
628 (defprop mtimes ("\\,") texsym)
630 (defprop %sqrt tex-sqrt tex)
632 (defun tex-sqrt(x l r)
633 ;; format as \\sqrt { } assuming implicit parens for sqr grouping
634 (tex (cadr x) (append l '("\\sqrt{")) (append '("}") r) 'mparen 'mparen))
636 ;; macsyma doesn't know about cube (or nth) roots,
637 ;; but if it did, this is what it would look like.
638 (defprop $cubrt tex-cubrt tex)
640 (defun tex-cubrt (x l r)
641 (tex (cadr x) (append l '("\\root 3 \\of{")) (append '("}") r) 'mparen 'mparen))
643 (defprop mquotient tex-mquotient tex)
644 (defprop mquotient ("\\over") texsym)
646 (defun tex-mquotient (x l r)
647 (if (or (null (cddr x)) (cdddr x)) (wna-err (caar x)))
648 (setq l (tex (cadr x) (append l '("{{")) nil 'mparen 'mparen)
649 ;the divide bar groups things
650 r (tex (caddr x) (list "}\\over{") (append '("}}")r) 'mparen 'mparen))
651 (append l r))
653 (defprop $matrix tex-matrix tex)
655 (defun tex-matrix(x l r) ;;matrix looks like ((mmatrix)((mlist) a b) ...)
656 (append l `("\\pmatrix{")
657 (mapcan #'(lambda(y)
658 (tex-list (cdr y) nil (list "\\cr ") "&"))
659 (cdr x))
660 '("}") r))
662 ;; macsyma sum or prod is over integer range, not low <= index <= high
663 ;; TeX is lots more flexible .. but
665 (defprop %sum tex-sum tex)
666 (defprop %lsum tex-lsum tex)
667 (defprop %product tex-sum tex)
669 ;; easily extended to union, intersect, otherops
671 (defun tex-lsum(x l r)
672 (let ((op (cond ((eq (caar x) '%lsum) "\\sum_{")
673 ;; extend here
675 ;; gotta be one of those above
676 ;; 4th arg of tex is changed from mparen to (caar x)
677 ;; to reflect the operator preceedance correctly.
678 ;; This change improves the how to put paren.
679 (s1 (tex (cadr x) nil nil (caar x) rop)) ;; summand
680 (index ;; "index = lowerlimit"
681 (tex `((min simp) , (caddr x), (cadddr x)) nil nil 'mparen 'mparen)))
682 (append l `( ,op ,@index "}}{" ,@s1 "}") r)))
684 (defun tex-sum(x l r)
685 (let ((op (cond ((eq (caar x) '%sum) "\\sum_{")
686 ((eq (caar x) '%product) "\\prod_{")
687 ;; extend here
689 ;; gotta be one of those above
690 ;; 4th arg of tex is changed from mparen to (caar x)
691 ;; to reflect the operator preceedance correctly.
692 ;; This change improves the how to put paren.
693 (s1 (tex (cadr x) nil nil (caar x) rop)) ;; summand
694 (index ;; "index = lowerlimit"
695 (tex `((mequal simp) ,(caddr x),(cadddr x)) nil nil 'mparen 'mparen))
696 (toplim (tex (car(cddddr x)) nil nil 'mparen 'mparen)))
697 (append l `( ,op ,@index "}^{" ,@toplim "}{" ,@s1 "}") r)))
699 (defprop %integrate tex-int tex)
700 (defun tex-int (x l r)
701 (let ((s1 (tex (cadr x) nil nil 'mparen 'mparen)) ;;integrand delims / & d
702 (var (tex (caddr x) nil nil 'mparen rop))) ;; variable
703 (cond((= (length x) 3)
704 (append l `("\\int {" ,@s1 "}{\\;d" ,@var "}") r))
705 (t ;; presumably length 5
706 (let ((low (tex (nth 3 x) nil nil 'mparen 'mparen))
707 ;; 1st item is 0
708 (hi (tex (nth 4 x) nil nil 'mparen 'mparen)))
709 (append l `("\\int_{" ,@low "}^{" ,@hi "}{" ,@s1 "\\;d" ,@var "}") r))))))
711 (defprop %limit tex-limit tex)
713 (defun tex-limit (x l r)
714 (let*
715 ;; limit function
716 ((s1 (tex (cadr x) nil nil 'mparen rop))
717 (direction (fifth x))
718 ;; the thing underneath "limit"
719 (subfun
720 (subst (or (and (eq direction '$plus) "\\downarrow ")
721 (and (eq direction '$minus) "\\uparrow ")
722 "\\rightarrow ")
724 (tex `((mequal simp) ,(caddr x),(cadddr x))
725 nil nil 'mparen 'mparen))))
726 (append l `("\\lim_{" ,@subfun "}{" ,@s1 "}") r)))
728 (defprop %at tex-at tex)
730 ;; e.g. at(diff(f(x)),x=a)
731 (defun tex-at (x l r)
732 (let ((s1 (tex (cadr x) nil nil lop rop))
733 (sub (tex (caddr x) nil nil 'mparen 'mparen)))
734 (append l '("\\left.") s1 '("\\right|_{") sub '("}") r)))
736 (defprop mbox tex-mbox tex)
738 ;; \boxed is defined in amsmath.sty,
739 ;; \newcommand{\boxed}[1]{\fbox{\m@th$\displaystyle#1$}}
741 (defun tex-mbox (x l r)
742 (append l '("\\boxed{") (tex (cadr x) nil nil 'mparen 'mparen) '("}") r))
744 (defprop mlabox tex-mlabox tex)
746 (defun tex-mlabox (x l r)
747 (append l '("\\stackrel{") (tex (caddr x) nil nil 'mparen 'mparen)
748 '("}{\\boxed{") (tex (cadr x) nil nil 'mparen 'mparen) '("}}") r))
750 ;;binomial coefficients
752 (defprop %binomial tex-choose tex)
754 (defun tex-choose (x l r)
755 (append l
756 '("{{")
757 (tex (cadr x) nil nil 'mparen 'mparen)
758 '("}\\choose{")
759 (tex (caddr x) nil nil 'mparen 'mparen)
760 '("}}")
763 (defprop rat tex-rat tex)
764 (defun tex-rat(x l r) (tex-mquotient x l r))
766 (defprop mplus tex-mplus tex)
768 (defun tex-mplus (x l r)
769 ;(declare (fixnum w))
770 (cond ((member 'trunc (car x) :test #'eq) (setq r (cons "+\\cdots " r))))
771 (cond ((null (cddr x))
772 (if (null (cdr x))
773 (tex-function x l r t)
774 (tex (cadr x) (cons "+" l) r 'mplus rop)))
775 (t (setq l (tex (cadr x) l nil lop 'mplus)
776 x (cddr x))
777 (do ((nl l) (dissym))
778 ((null (cdr x))
779 (if (mmminusp (car x)) (setq l (cadar x) dissym (list "-"))
780 (setq l (car x) dissym (list "+")))
781 (setq r (tex l dissym r 'mplus rop))
782 (append nl r))
783 (if (mmminusp (car x)) (setq l (cadar x) dissym (list "-"))
784 (setq l (car x) dissym (list "+")))
785 (setq nl (append nl (tex l dissym nil 'mplus 'mplus))
786 x (cdr x))))))
788 (defprop mminus tex-prefix tex)
789 (defprop mminus ("-") texsym)
791 ;; MIN = "Maxima in", apparently -- not to be confused with the least value of a set.
792 ;; MIN is not known to the parser, although it seems stuff like "x in S" could make use of MIN.
794 (defprop min tex-infix tex)
795 (defprop min ("\\in{") texsym)
796 (defprop min 80. tex-lbp)
797 (defprop min 80. tex-rbp)
799 (defprop mequal tex-infix tex)
800 (defprop mequal (=) texsym)
802 (defprop mnotequal tex-infix tex)
803 (defprop mnotequal ("\\neq ") texsym)
805 (defprop mgreaterp tex-infix tex)
806 (defprop mgreaterp (>) texsym)
808 (defprop mgeqp tex-infix tex)
809 (defprop mgeqp ("\\geq ") texsym)
811 (defprop mlessp tex-infix tex)
812 (defprop mlessp (<) texsym)
814 (defprop mleqp tex-infix tex)
815 (defprop mleqp ("\\leq ") texsym)
817 (defprop mnot tex-prefix tex)
818 (defprop mnot ("\\neg ") texsym)
820 (defprop mand tex-nary tex)
821 (defprop mand ("\\land ") texsym)
823 (defprop mor tex-nary tex)
824 (defprop mor ("\\lor ") texsym)
826 ;; make sin(x) display as sin x , but sin(x+y) as sin(x+y)
827 ;; etc
829 (defun tex-setup (x)
830 (let((a (car x))
831 (b (cadr x)))
832 (setf (get a 'tex) 'tex-prefix)
833 (setf (get a 'texword) b) ;This means "sin" will always be roman
834 (setf (get a 'texsym) (list b))
835 (setf (get a 'tex-rbp) 130)))
838 ;; I WONDER IF ALL BUILT-IN FUNCTIONS SHOULD BE SET IN ROMAN TYPE
839 (defprop $atan2 "{\\rm atan2}" texword)
841 ;; JM 09/01 expand and re-order to follow table of "log-like" functions,
842 ;; see table in Lamport, 2nd edition, 1994, p. 44, table 3.9.
843 ;; I don't know if these are Latex-specific so you may have to define
844 ;; them if you use plain Tex.
846 (mapc #'tex-setup
848 (%acos "\\arccos ")
849 (%asin "\\arcsin ")
850 (%atan "\\arctan ")
852 ; Latex's arg(x) is ... ?
853 (%cos "\\cos ")
854 (%cosh "\\cosh ")
855 (%cot "\\cot ")
856 (%coth "\\coth ")
857 (%csc "\\csc ")
858 ; Latex's "deg" is ... ?
859 (%determinant "\\det ")
860 (%dim "\\dim ")
861 (%exp "\\exp ")
862 (%gcd "\\gcd ")
863 ; Latex's "hom" is ... ?
864 (%inf "\\inf ") ; many will prefer "\\infty". Hmmm.
865 ; Latex's "ker" is ... ?
866 ; Latex's "lg" is ... ?
867 ; lim is handled by tex-limit.
868 ; Latex's "liminf" ... ?
869 ; Latex's "limsup" ... ?
870 (%ln "\\ln ")
871 (%log "\\log ")
872 (%max "\\max ")
873 (%min "\\min ")
874 ; Latex's "Pr" ... ?
875 (%sec "\\sec ")
876 (%sin "\\sin ")
877 (%sinh "\\sinh ")
878 ; Latex's "sup" ... ?
879 (%tan "\\tan ")
880 (%tanh "\\tanh ")
881 ;; (%erf "{\\rm erf}") this would tend to set erf(x) as erf x. Unusual
882 ;(%laplace "{\\cal L}")
884 ; Maxima built-in functions which do not have corresponding TeX symbols.
886 (%asec "{\\rm arcsec}\\; ")
887 (%acsc "{\\rm arccsc}\\; ")
888 (%acot "{\\rm arccot}\\; ")
890 (%sech "{\\rm sech}\\; ")
891 (%csch "{\\rm csch}\\; ")
893 (%asinh "{\\rm asinh}\\; ")
894 (%acosh "{\\rm acosh}\\; ")
895 (%atanh "{\\rm atanh}\\; ")
897 (%asech "{\\rm asech}\\; ")
898 (%acsch "{\\rm acsch}\\; ")
899 (%acoth "{\\rm acoth}\\; ")
901 )) ;; etc
903 (defprop mcond tex-mcond tex)
904 (defprop %mcond tex-mcond tex)
906 (defprop %del tex-prefix tex)
907 (defprop %del ("d") texsym)
909 (defprop %derivative tex-derivative tex)
910 (defun tex-derivative (x l r)
911 (tex (if $derivabbrev
912 (tex-dabbrev x)
913 (tex-d x '$d)) l r lop rop ))
915 (defun tex-d(x dsym) ;dsym should be $d or "$\\partial"
916 ;; format the macsyma derivative form so it looks
917 ;; sort of like a quotient times the deriva-dand.
918 (let*
919 ((arg (cadr x)) ;; the function being differentiated
920 (difflist (cddr x)) ;; list of derivs e.g. (x 1 y 2)
921 (ords (odds difflist 0)) ;; e.g. (1 2)
922 (vars (odds difflist 1)) ;; e.g. (x y)
923 (numer `((mexpt) ,dsym ((mplus) ,@ords))) ; d^n numerator
924 (denom (cons '(mtimes)
925 (mapcan #'(lambda(b e)
926 `(,dsym ,(simplifya `((mexpt) ,b ,e) nil)))
927 vars ords))))
928 `((mtimes)
929 ((mquotient) ,(simplifya numer nil) ,denom)
930 ,arg)))
932 (defun tex-dabbrev (x)
933 ;; Format diff(f,x,1,y,1) so that it looks like
934 ;; f
935 ;; x y
936 (let*
937 ((arg (cadr x)) ;; the function being differentiated
938 (difflist (cddr x)) ;; list of derivs e.g. (x 1 y 2)
939 (ords (odds difflist 0)) ;; e.g. (1 2)
940 (vars (odds difflist 1))) ;; e.g. (x y)
941 (append
942 (if (symbolp arg)
943 `((,arg array))
944 `((mqapply array) ,arg))
945 (if (and (= (length vars) 1)
946 (= (car ords) 1))
947 vars
948 `(((mtimes) ,@(mapcan #'(lambda (var ord)
949 (make-list ord :initial-element var))
950 vars ords)))))))
952 (defun odds(n c)
953 ;; if c=1, get the odd terms (first, third...)
954 (cond ((null n) nil)
955 ((= c 1)(cons (car n)(odds (cdr n) 0)))
956 ((= c 0)(odds (cdr n) 1))))
959 ;; The format of MCOND expressions is documented above the definition
960 ;; of DIM-MCOND in displa.lisp. Here are some examples:
962 ;; ((%mcond) $a $b t nil) <==> 'if a then b
963 ;; ((%mcond) $a $b t $d) <==> 'if a then b else d
964 ;; ((%mcond) $a $b $c nil t nil) <==> 'if a then b elseif c then false
965 ;; ((%mcond) $a $b $c $d t nil) <==> 'if a then b elseif c then d
966 ;; ((%mcond) $a $b $c $d t $f) <==> 'if a then b elseif c then d else f
968 ;; Note that DIM-MCOND omits display of the final "else" in three
969 ;; cases illustrated below, so we do the same here:
971 ;; ((%mcond) $a $b $c $d t $false) <==> '(if a then b elseif c then d)
972 ;; ((%mcond) $a $b $c $d t nil) <==> 'if a then b elseif c then d
973 ;; ((%mcond) $a $b $c $d) ==> 'if a then b elseif c then d
975 ;; The first two cases occur in practice, as can be seen by evaluating
976 ;; ?print('(if a then b)) and ?print(if a then b). The parser
977 ;; produces the first case, which is transformed into the second case
978 ;; during evaluation. The third case is handled equivalently by the
979 ;; evaluator and DIM-MCOND, and might plausibly be created by some
980 ;; code, so we handle it here as well.
982 ;; The use of '$false (instead of nil) may be a hack that is no longer
983 ;; needed. For more information on this, search for $false in
984 ;; PARSE-CONDITION of nparse.lisp and DIM-MCOND of displa.lisp. Also
985 ;; see the mailing list thread with subject "Bugs in tex-mcond" which
986 ;; took place in January 2011. -MHW
988 (defun tex-mcond (x l r)
989 (labels
990 ((recurse (x l)
991 (append
992 (tex (car x) l '("\\;\\mathbf{then}\\;") 'mparen 'mparen)
993 (cond ((member (cddr x) '(() (t nil) (t $false)) :test #'equal)
994 (tex (second x) nil r 'mcond rop))
995 ((and (eq (third x) t) (null (nthcdr 4 x)))
996 (append
997 (tex (second x) nil nil 'mparen 'mparen)
998 (tex (fourth x) '("\\;\\mathbf{else}\\;") r 'mcond rop)))
999 (t (append
1000 (tex (second x) nil nil 'mparen 'mparen)
1001 (recurse (cddr x) '("\\;\\mathbf{elseif}\\;"))))))))
1002 (append l (recurse (cdr x) '("\\mathbf{if}\\;")))))
1004 (defprop mdo tex-mdo tex)
1005 (defprop mdoin tex-mdoin tex)
1007 (defprop %mdo tex-mdo tex)
1008 (defprop %mdoin tex-mdoin tex)
1010 (defun tex-lbp(x)(cond((get x 'tex-lbp))(t(lbp x))))
1011 (defun tex-rbp(x)(cond((get x 'tex-rbp))(t(rbp x))))
1013 ;; these aren't quite right
1015 (defun tex-mdo (x l r)
1016 (tex-list (texmdo x) l r "\\;"))
1018 (defun tex-mdoin (x l r)
1019 (tex-list (texmdoin x) l r "\\;"))
1021 (defun texmdo (x)
1022 (nconc (cond ((second x) `("\\mathbf{for}" ,(second x))))
1023 (cond ((equal 1 (third x)) nil)
1024 ((third x) `("\\mathbf{from}" ,(third x))))
1025 (cond ((equal 1 (fourth x)) nil)
1026 ((fourth x) `("\\mathbf{step}" ,(fourth x)))
1027 ((fifth x) `("\\mathbf{next}" ,(fifth x))))
1028 (cond ((sixth x) `("\\mathbf{thru}" ,(sixth x))))
1029 (cond ((null (seventh x)) nil)
1030 ((eq 'mnot (caar (seventh x)))
1031 `("\\mathbf{while}" ,(cadr (seventh x))))
1032 (t `("\\mathbf{unless}" ,(seventh x))))
1033 `("\\mathbf{do}" ,(eighth x))))
1035 (defun texmdoin (x)
1036 (nconc `("\\mathbf{for}" ,(second x) "\\mathbf{in}" ,(third x))
1037 (cond ((sixth x) `("\\mathbf{thru}" ,(sixth x))))
1038 (cond ((null (seventh x)) nil)
1039 ((eq 'mnot (caar (seventh x)))
1040 `("\\mathbf{while}" ,(cadr (seventh x))))
1041 (t `("\\mathbf{unless}" ,(seventh x))))
1042 `("\\mathbf{do}" ,(eighth x))))
1044 (defprop mtext tex-mtext tex)
1045 (defprop text-string tex-mtext tex)
1046 (defprop mlabel tex-mlabel tex)
1047 (defprop spaceout tex-spaceout tex)
1049 ;; Additions by Marek Rychlik (rychlik@u.arizona.edu)
1050 ;; This stuff handles setting of LET rules
1052 (defprop | --> | "\\longrightarrow " texsym)
1053 (defprop #.(intern (format nil " ~A " 'where)) "\\;\\mathbf{where}\\;" texsym)
1055 ;; end of additions by Marek Rychlik
1057 (defun tex-try-sym (x)
1058 (if (symbolp x)
1059 (let ((tx (get x 'texsym))) (if tx tx x))
1062 (defun tex-mtext (x l r)
1063 (tex-list (map 'list #'tex-try-sym (cdr x)) l r ""))
1065 (defun tex-mlabel (x l r)
1066 (tex (caddr x)
1067 (append l
1068 (if (cadr x)
1069 (list (format nil "\\mbox{\\tt\\red(~A) \\black}" (tex-stripdollar (cadr x))))
1070 nil))
1071 r 'mparen 'mparen))
1073 (defun tex-spaceout (x l r)
1074 (append l (cons (format nil "\\hspace{~dmm}" (* 3 (cadr x))) r)))
1076 ;; run some code initialize file before $tex is run
1077 (defun $texinit(file)
1078 (declare (ignore file))
1079 '$done)
1081 ;; this just prints a \\end on the file; this is something a TeXnician would
1082 ;; probably have no trouble spotting, and will generally be unnecessary, since
1083 ;; we anticipate almost all use of tex would be involved in inserting this
1084 ;; stuff into larger files that would have their own \\end or equivalent.
1085 (defun $texend(filename)
1086 (with-open-file (st (stripdollar filename) :direction :output
1087 :if-exists :append :if-does-not-exist :create)
1088 (format st "\\end~%"))
1089 '$done)
1091 ;; Construct a Lisp function and attach it to the TEX property of
1092 ;; operator OP. The constructed function calls a Maxima function F
1093 ;; to generate TeX output for OP.
1094 ;; F must take 1 argument (an expression which has operator OP)
1095 ;; and must return a string (the TeX output).
1097 (defun make-maxima-tex-glue (op f)
1098 (let
1099 ((glue-f (gensym))
1100 (f-body `(append l
1101 (list
1102 (let ((f-x (mfuncall ',f x)))
1103 (if (stringp f-x) f-x
1104 (merror (intl:gettext "tex: function ~s did not return a string.~%") ($sconcat ',f)))))
1105 r)))
1106 (setf (symbol-function glue-f) (coerce `(lambda (x l r) ,f-body) 'function))
1107 (setf (get op 'tex) glue-f))
1110 ;; Convenience function to allow user to process expression X
1111 ;; and get a string (TeX output for X) in return.
1113 (defun $tex1 (x) (reduce #'strcat (tex x nil nil 'mparen 'mparen)))
1115 ;; Undone and trickier:
1116 ;; handle reserved symbols stuff, just in case someone
1117 ;; has a macsyma variable named (yuck!!) \over or has a name with
1118 ;; {} in it.
1119 ;; Maybe do some special hacking for standard notations for
1120 ;; hypergeometric fns, alternative summation notations 0<=n<=inf, etc.
1122 ;;Undone and really pretty hard: line breaking
1124 ;; The texput function was written by Barton Willis.
1126 (defun $texput (e s &optional tx)
1128 (cond
1129 ((stringp e)
1130 (setq e ($verbify e)))
1131 ((not (symbolp e))
1132 (merror (intl:gettext "texput: first argument must be a string or a symbol; found: ~M") e)))
1134 (setq s (if ($listp s) (margs s) (list s)))
1136 (cond
1137 ((null tx)
1138 ;; texput was called as texput(op, foo) where foo is a string
1139 ;; or a symbol; when foo is a string, assign TEXWORD property,
1140 ;; when foo is a symbol, construct glue function to call
1141 ;; the Maxima function named by foo.
1142 (let ((s0 (nth 0 s)))
1143 (if (stringp s0)
1144 (putprop e s0 'texword)
1145 (make-maxima-tex-glue e s0)))) ;; assigns TEX property
1146 ((eq tx '$matchfix)
1147 (putprop e 'tex-matchfix 'tex)
1148 (cond ((< (length s) 2)
1149 (merror (intl:gettext "texput: expected a list of two items for matchfix operator.")))
1150 ((= (length s) 2)
1151 (putprop e (list (list (first s)) (second s)) 'texsym))
1153 (putprop e (list (list (first s)) (second s) (third s)) 'texsym)))
1154 `((mlist) ,@s))
1156 ((eq tx '$nofix)
1157 (putprop e 'tex-nofix 'tex)
1158 (putprop e s 'texsym)
1159 (car s))
1161 ((eq tx '$prefix)
1162 (putprop e 'tex-prefix 'tex)
1163 (when (null (get e 'grind))
1164 (putprop e 180 'tex-rbp))
1165 (putprop e s 'texsym)
1166 (car s))
1168 ((eq tx '$infix)
1169 (putprop e 'tex-infix 'tex)
1170 (when (null (get e 'grind))
1171 (putprop e 180 'tex-lbp)
1172 (putprop e 180 'tex-rbp))
1173 (putprop e s 'texsym)
1174 (car s))
1176 ((eq tx '$nary)
1177 (putprop e 'tex-nary 'tex)
1178 (when (null (get e 'grind))
1179 (putprop e 180 'tex-lbp)
1180 (putprop e 180 'tex-rbp))
1181 (putprop e s 'texsym)
1182 (car s))
1184 ((eq tx '$postfix)
1185 (putprop e 'tex-postfix 'tex)
1186 (when (null (get e 'grind))
1187 (putprop e 180 'tex-lbp))
1188 (putprop e s 'texsym)
1189 (car s))))