Warning: this is an htmlized version!
The original is here, and
the conversion rules are here.
;; This file:
;;   http://anggtwu.net/MAXIMA/2025-displa-tex.lisp.html
;;   http://anggtwu.net/MAXIMA/2025-displa-tex.lisp
;;          (find-angg "MAXIMA/2025-displa-tex.lisp")
;;  Author: Eduardo Ochs <eduardoochs@gmail.com>
;;     See: http://anggtwu.net/eev-maxima.html#LaTeX-instead-of-TeX
;; Version: 2025dec14
;;
;; This file redefines the `display' and `tex' properties of some
;; standard Maxima operators and defines several new operations with
;; non-trivial `display' and `tex' properties.
;;
;; Based on: (find-angg "MAXIMA/2025-Integrate.lisp")
;;           (find-angg "MAXIMA/lazynouns.lisp")
;;           (find-angg "MAXIMA/barematrix2.lisp")
;;
;; «.matrix»			(to "matrix")
;; «.bmatrix»			(to "bmatrix")
;; «.matrix-tests»		(to "matrix-tests")
;; «.barematrix»		(to "barematrix")
;; «.barematrix-tests»		(to "barematrix-tests")
;; «.quotient»			(to "quotient")
;; «.quotient-tests»		(to "quotient-tests")
;; «.lazy-nouns»		(to "lazy-nouns")
;; «.lazy-nouns-tests»		(to "lazy-nouns-tests")
;; «.lazy-ops»			(to "lazy-ops")
;; «.lazy-ops-tests»		(to "lazy-ops-tests")
;; «.display_delegate»		(to "display_delegate")
;; «.display_delegate-tests»	(to "display_delegate-tests")
;; «.becomes»			(to "becomes")
;; «.becomes-tests»		(to "becomes-tests")
;; «.underbrace»		(to "underbrace")
;; «.underbrace-tests»		(to "underbrace-tests")
;; «.verbatimbox»		(to "verbatimbox")
;; «.verbatimmatrix»		(to "verbatimmatrix")
;; «.verbatimbox-tests»		(to "verbatimbox-tests")
;; «.verbatimmatrix-tests»	(to "verbatimmatrix-tests")


;; «matrix»   (to ".matrix")
;; «bmatrix»  (to ".bmatrix")
;; In this block we redefine `tex-matrix' to use LaTeX instead of TeX:
;;
;;   (%i1) tex1(matrix([42]));
;;   (%o1) \ifx\endpmatrix\undefined\pmatrix{\else\begin{pmatrix}\fi 42\cr   <-- TeX
;;         \ifx\endpmatrix\undefined}\else\end{pmatrix}\fi 
;;   (%i2) load("2025-displa-tex.lisp")$
;;   (%i3) tex1(matrix([42]));
;;   (%o3)           \begin{pmatrix}42\cr \end{pmatrix}                      <-- LaTeX
;;
;; and we also define a `bmatrix', that is displayed like a `matrix'
;; but is `tex'ed using square brackets instead of parentheses.
;;
;; See: (find-maximagitfile "src/mactex.lisp" "(defun tex-matrix")
;;      (find-maximagitfile "src/mactex.lisp" "(defun tex-matrix" "ifx")
;;
;; In the code below a "matrix" looks like
;;   ((mmatrix) ((mlist) a b) ...)
;; and a "row" looks like this:
;;              ((mlist) a b).
;;
(defprop $matrix  tex-matrix  tex)
(defprop $bmatrix tex-bmatrix tex)
(setf (get '$bmatrix 'dimension) 'dim-$matrix)

(defun tex-matrixrow (row) (tex-list (cdr row) nil (list "\\cr ") "&"))
(defun tex-matrixbody (matrix) (mapcan #'tex-matrixrow (cdr matrix)))

(defun tex-matrix (matrix l r)
  `(,@l
    "\\begin{pmatrix}"
    ,@(tex-matrixbody matrix)
    "\\end{pmatrix}"
    ,@r))

(defun tex-bmatrix (matrix l r)
  `(,@l
    "\\begin{bmatrix}"
    ,@(tex-matrixbody matrix)
    "\\end{bmatrix}"
    ,@r))

#|
** «matrix-tests»  (to ".matrix-tests")
* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
tex1(matrix([42]));
load("2025-displa-tex.lisp");
tex1(matrix([42]));
tex1(bmatrix([42]));
     bmatrix([42]);

to_lisp();
  (tex-matrixrow #$[2,a,"b"]$)
  (tex-matrixbody #$[[a,b],[c,d]]$)
  (to-maxima)

|#


;; «barematrix»  (to ".barematrix")
;; See: (find-es "maxima" "display_matrix_brackets")
;;      (find-maximamsg "59245670 202510 12" "RDodier: display_matrix_brackets")
;;      (find-maximamsg "59271831 202512 11" "RDodier: display_matrix_padding")
;;
;; A `barematrix' is a `matrix' without the outer parentheses. In
;; recent versions of Maxima - after 2025oct12 - barematrixes and
;; matrixes are displayed differently, but in versions older than that
;; the variable `display_matrix_brackets' is ignored and barematrixes
;; and matrixes are displayed in the same way.
;;
;; In 2025dec11 Robert Dodier made the variables
;; `display_matrix_padding_vertical' and
;; `display_matrix_padding_horizontal' control if matrixes are
;; displayed with or without space between lines and columns. A
;; `barematrixnp' is a `matrix' that is "bare" is the sense above, of
;; "no outer brackets", and also has no padding, vertical or
;; horizontal. In versions of Maxima prior to 2025oct12 `matrix'es,
;; `barematrix'es and `barematrixnp's are displayed in the same way.
;;
(setf (get '$barematrix   'dimension) 'dim-$barematrix)
(setf (get '$barematrixnp 'dimension) 'dim-$barematrixnp)
(defprop $barematrix   tex-barematrix tex)
(defprop $barematrixnp tex-barematrix tex) ; will change in the future

(defun dim-$barematrix (form result)
  (let (($display_matrix_brackets nil))
    (dim-$matrix form result)))

(defun dim-$barematrixnp (form result)
  (let (($display_matrix_brackets nil)
	($display_matrix_padding_vertical nil)
	($display_matrix_padding_horizontal nil))
    (dim-$matrix form result)))

(defun tex-barematrix (matrix l r)
  `(,@l
    "\\begin{matrix}"
    ,@(tex-matrixbody matrix)
    "\\end{matrix}"
    ,@r))

#|
** «barematrix-tests»  (to ".barematrix-tests")
* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
load("2025-displa-tex.lisp");
M1 : barematrix  ([a,b],[c,d]);
M2 : barematrixnp([a,b],[c,d]);
M3 : matrix     ([M1,e,f],[g,h,i],[j,k,M2]);
tex1(M1);
apply('barematrix,   map("[", ["ab", "cd", "ef"]));
apply('barematrixnp, map("[", ["ab", "cd", "ef"]));

|#



;; «quotient»  (to ".quotient")
;; In this block we redefine `tex-mquotient' to use LaTeX instead of TeX:
;;
;;   (%i1) tex1(a/b);
;;   (%o1)                            {{a}\over{b}}   <-- TeX
;;   (%i2) load("2025-displa-tex.lisp")$
;;   (%i3) tex1(a/b);
;;   (%o3)                            {\frac{a}{b}}   <-- LaTeX
;;
(defun tex-mquotient (q l r)
  (twoargcheck q)
  `(,@(tex (cadr  q) `(,@l "{\\frac{") () 'mparen 'mparen)
    ,@(tex (caddr q) (list "}{") `("}}" ,@r) 'mparen 'mparen)
    ))

#|
** «quotient-tests»  (to ".quotient-tests")
* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
tex1(a/b);
load("2025-displa-tex.lisp");
tex1(a/b);

|#


;; «lazy-nouns»  (to ".lazy-nouns")
;; Supersedes: (find-angg "MAXIMA/lazynouns.lisp")
;; except for the comments - lazynouns.lisp has better comments.

(defun my-copy-properties (tosymbol fromsymbol keys)
  (loop for key in keys
	do (setf (get tosymbol key) (get fromsymbol key))))

(my-copy-properties '$_diff      '%derivative '(dimension tex))
(my-copy-properties '$_integrate '%integrate  '(dimension tex))
(my-copy-properties '$_at        '%at         '(dimension tex))
(my-copy-properties '$_limit     '%limit      '(dimension tex))


#|
** «lazy-nouns-tests»  (to ".lazy-nouns-tests")
* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
load("2025-displa-tex.lisp");

[o1,o2] : [2*x^3, 1];
matrix(["", "diff",    "'diff",     "_diff"],
       [o1, diff(o1,x), 'diff(o1,x), _diff(o1,x)],
       [o2, diff(o2,x), 'diff(o2,x), _diff(o2,x)]);

[o1,o2] : [2*f(x), 2*x];
matrix(["", "integrate",    "'integrate",     "_integrate"],
       [o1, integrate(o1,x), 'integrate(o1,x), _integrate(o1,x)],
       [o2, integrate(o2,x), 'integrate(o2,x), _integrate(o2,x)]);

 at(a,b=c);
'at(a,b=c);
_at(a,b=c);
_limit(a,x,x0);

|#


;; «lazy-ops»  (to ".lazy-ops")
;; See: (find-angg "MAXIMA/lazynouns.lisp")

($infix "*." 120)
($nary  "*.")
(my-copy-properties '|$*.| 'mtimes
		    '($nary lbp rbp
		      dimension dissym
		      tex texsym))

($infix "/." 120 120)
($nary  "/.")				; why?
(my-copy-properties '|$/.| 'mquotient
		    '($nary lbp rbp
		      dimension dissym
		      tex texsym))

($infix "+." 100 134)
($nary  "+.")
(my-copy-properties '|$+.| 'mplus
		    '($nary lbp rbp
		      dimension dissym
		      tex texsym))

($infix "-." 100 134)
($nary  "-.")
(my-copy-properties '|$-.| 'mminus
		    '($nary lbp rbp
		      dimension dissym
		      tex texsym))

#|
** «lazy-ops-tests»  (to ".lazy-ops-tests")
* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
load("2025-displa-tex.lisp");

|#




;; «display_delegate»  (to ".display_delegate")
;; The Maxima function `$display_delegate', defined below, implements
;; another way to define how Maxima objects will be displayed - for
;; example,
;;
;;   display_delegate('ccc, 'ccc_translate);
;; 
;; tells Maxima that an object like ccc(x,y,z) should be displayed by
;; first running ccc_translate(ccc(x,y,z)) and then delegating the
;; displaying to the result.
;;
;; Note that `display_delegate-3' and `display_delegate-2' are Lisp
;; macros written in a low-level way.
;;
(defun display_delegate-3 ($CCC $CCC_TRANSLATE DIM-CCC)
  `(progn
     ;;
     (setf (get ',$CCC 'dimension) ',DIM-CCC)
     (defun ,DIM-CCC (o result)
       (let* ((list-o (list '(mlist simp) o))
	      (translated-o ($apply ',$CCC_TRANSLATE list-o))
	      (op-translated-o (caar translated-o))
	      (dim-translated-o (get op-translated-o 'dimension)))
	 (funcall dim-translated-o translated-o result)))
     ;;
     ))

(defun display_delegate-2 ($CCC $CCC_TRANSLATE)
  (let ((DIM-CCC (intern (format nil "DIM-~a" (symbol-name $CCC)))))
    (display_delegate-3 $CCC $CCC_TRANSLATE DIM-CCC)))

(defun $display_delegate ($CCC &optional $CCC_TRANSLATE)
  (if $CCC_TRANSLATE
      (eval (display_delegate-2 $CCC $CCC_TRANSLATE))
      (setf (get $CCC 'dimension) nil)))

#|
** «display_delegate-tests»  (to ".display_delegate-tests")
* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
load("2025-displa-tex.lisp");
o : _At2(g(u),u,2,3);

display_delegate('_At2, '_At2_translate);
_At2_translate (o) := apply(_At2_, args(o))$
_At2_   (fx,x,a,b) := barematrix(["", "|", x=b],
                                 [fx, "|", ""],
                                 ["", "|", x=a])$
o;

|#


;; «becomes»  (to ".becomes")
;; Used by: (find-angg "MAXIMA/2025-1-s.mac" "_s_")
;;     See: (find-es "maxima" "becomes")
(displa-def $becomes dimension-infix " := " 80 80)
(defprop $becomes tex-infix tex)
(defprop $becomes (" := ") texsym)

#|
** «becomes-tests»  (to ".becomes-tests")
* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
load("2025-displa-tex.lisp");
o : becomes(f(x),42*x);
tex1(o);

|#




;; «underbrace»  (to ".underbrace")
;; Old version: (find-angg "MAXIMA/dim-underbrace.lisp")
;; New version: (find-angg "MAXIMA/edrxbox-examples.lisp" "underbrace")
;;         See: (find-angg "MAXIMA/2025-1-s.mac" "_ssu_")
;;     Moved!!!
;;
(defprop $underbrace tex-underbrace tex)

(defun tex-underbrace (uexpr l r)
  (let* ((args        (cdr uexpr))
	 (top-expr    (first args))
	 (bottom-expr (second args))
	 (top-tex     ($tex1 top-expr))
	 (bottom-tex  ($tex1 bottom-expr)))
  `(,@l
    ,(format nil "\\underbrace{~a}_{~a}" top-tex bottom-tex)
    ,@r)))

#|
** «underbrace-tests»  (to ".underbrace-tests")
* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
load("2025-displa-tex.lisp");
load("edrxbox.lisp");
load("edrxbox-examples.lisp");
mybox(o) := [box(o)];
o1 : 23 / 456789;
o2 : a[b]^c;
o3 : underbrace(o1,o2);
mybox(o3);
tex1(o3);

|#




;; «verbatimbox»  (to ".verbatimbox")
;; «verbatimmatrix»  (to ".verbatimmatrix")
;; Old version: (find-angg "MAXIMA/dim-verbatimbox.lisp")
;; New version: (find-angg "MAXIMA/edrxbox-examples.lisp" "verbatimbox")
;; Explanation: (find-lisptree "lisptree-middle.lisp" "verbatimmatrix")

(defun dim-$verbatimmatrix (form result)
  (let* ((lines (rest form))
	 (strings (map 'list #'second lines))
	 (verbatimbox `(($verbatimbox) ,@strings)))
    (dim-$verbatimbox verbatimbox result)))

(setf (get '$verbatimmatrix 'dimension)
      'dim-$verbatimmatrix)

#|
** «verbatimbox-tests»     (to ".verbatimbox-tests")
** «verbatimmatrix-tests»  (to ".verbatimmatrix-tests")
* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
load("edrxbox.lisp");
load("edrxbox-examples.lisp");
load("2025-displa-tex.lisp");

o1 : matrix        (["f__."], ["|  |"], ["a  b"]);
o2 : barematrix    (["f__."], ["|  |"], ["a  b"]);
o3 : verbatimbox   ( "f__.",   "|  |",   "a  b" );
o4 : verbatimmatrix(["f__."], ["|  |"], ["a  b"]);
[o1, o2, o3, o4];
[box(o1), box(o2), box(o3), box(o4)];

** (find-lisptree "lisptree.mac")
load  ("~/lisptree/lisptree.mac");
lisptree_matrix :         'matrix$  lisptree2(f(a,b));
lisptree_matrix :     'barematrix$  lisptree2(f(a,b));
lisptree_matrix : 'verbatimmatrix$  lisptree2(f(a,b));

|#






;; Local Variables:
;; coding:  utf-8-unix
;; End: