Warning: this is an htmlized version!
The original is here, and
the conversion rules are here.
/*
 * This file:
 *   https://anggtwu.net/bad-foundations/sa-and-ga.mac.html
 *   https://anggtwu.net/bad-foundations/sa-and-ga.mac
 *           (find-angg "bad-foundations/sa-and-ga.mac")
 *                            (find-baf "sa-and-ga.mac")
 *   Author: Eduardo Ochs <eduardoochs@gmail.com>
 *  Version: 2026mar03
 * Licensse: GPL v2
 *
 * Functions to save LaTeXified Maxima objects into "\def"s.
 * Actually I usually save them into "\sa"s, that are better
 * than "\def"s; "\sa"s are explained below.
 *
 * Loaded by: (find-baf "bad-foundations.mac" "load")
 *      Uses: (find-baf "bad-foundations.lisp" "ee_writefile")
 *   Used by: (find-baf "edrx-myintegrates.mac" "latex")
 *
 * Index:
 * «.sa»		(to "sa")
 * «.sa-test»		(to "sa-test")
 * «.introduction»	(to "introduction")
*/


/* «sa»  (to ".sa")
*/
sa_bigstr : "";
sa_addline    (line) := sa_bigstr : ?format(false, "~a~a~%", sa_bigstr, line);
sa_generated_by(str) := (sa_addline("% Generated by:"),
                         sa_addline(concat("% ", str)));

sa__     (name,body) := ?format(false, "\\sa {~a} {~a}", name, body)$
sa_      (name,body) := sa_addline(sa__(name,body));
sa       (name,expr) := (sa_(name,tex1(expr)), expr);

sa_def__ (name,body) := ?format(false, "\\def\\~a{~a}", name, body)$
sa_def_  (name,body) := sa_addline(sa_def__(name,body));
sa_def   (name,expr) := (sa_def_(name,tex1(expr)), expr);

sa_bigstr_writeto (fname) := ee_writefile(fname, sa_bigstr);

/*
** «sa-test»  (to ".sa-test")
* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
load("bad-foundations.mac")$
o1 : RCV _sss_ [g(x)=x^2, gp(x)=2*x];

sa_generated_by  ("<sa-test>");
sa_def           ("foobar",  o1);
sa               ("foo bar", o1);
sa_bigstr;
sa_bigstr_writeto("/tmp/o");

*/


/* «introduction»  (to ".introduction")
 * Introduction
 * ============
 * I created big figure in p.15 -
 *
 *   (find-bafpage 15 "Here's a way to visualize how this substitution works:")
 *   (find-baftext 15 "Here's a way to visualize how this substitution works:")
 *
 * in several steps. First I created it in Maxima; then I ran a line
 * that converted it to LaTeX and saved that into the file "/tmp/o",
 * that contained a "\def\foobar{...}"; then I inserted the contents
 * of "/tmp/o" into my "2026-bad-foundations.tex"; and after that
 * "\def\foobar{...}" I inserted a "$$\foobar$$".
 *
 * I lied a bit above - instead of "\def\foobar{...}" and "$$\foobar$$"
 * I used something like "\sa{foo bar}" and "\ga{foo bar}", that let me
 * use (some) non-alphanumeric characters in the names of my definitions.
 *
 * Now the real story - and a working example. We will start by this:

* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
  load("bad-foundations.mac")$
  o1   : matrix([a,b],[c,d])$
  sa_def ("foobar",  o1);
  sa     ("foo bar", o1);
  sa_bigstr;
  sa_bigstr_writeto("/tmp/o");

 * Here is the log of the eepitch block above, slightly reformatted:
 *
 *
 *   (%i2) o1   : matrix([a,b],[c,d])$
 *   (%i3) sa_def ("foobar",  o1);
 *                                      ┌      ┐
 *                                      │ a  b │
 *   (%o3)                              │      │
 *                                      │ c  d │
 *                                      └      ┘
 *   (%i4) sa     ("foo bar", o1);
 *                                      ┌      ┐
 *                                      │ a  b │
 *   (%o4)                              │      │
 *                                      │ c  d │
 *                                      └      ┘
 *   (%i5) sa_bigstr;
 *   (%o5)
 *          \def \foobar  {\begin{pmatrix}a&b\cr c&d\cr \end{pmatrix}}
 *          \sa {foo bar} {\begin{pmatrix}a&b\cr c&d\cr \end{pmatrix}}
 *   
 *   (%i6) sa_bigstr_writeto("/tmp/o");
 *   (%o6)                      Wrote 115 bytes to /tmp/o
 * 
 * 
 * ...and its explanation:
 * 
 * 1. I created a complex Maxima object `o1'.
 * 
 * 2. Then `sa_def("foobar", o1);' saved its LaTeXification into
 *    \def\foobar{...}, but displayed `o1', not its LaTeXification.
 * 
 * 3. The `sa("foo bar", o1);' did something similar, but it used a
 *    "\sa {foo bar} {...}" instead of a "\def\foobar{...}"
 * 
 * 4. The line "sa_bigstr;" showed the LaTeX code that we saved
 *    (as a string with newlines, a.k.a. "a big string").
 * 
 * 5. The line `sa_bigstr_writeto("/tmp/o");' saved the string stored in
 *    `sa_bigstr' into the file "/tmp/o".
 * 
 * 
 * Now try this - but replace the "xpdf" by your favorite PDF viewer:


* (eepitch-shell)
* (eepitch-kill)
* (eepitch-shell)
  cd /tmp/
  cat > sa-and-ga-demo.tex <<'%%%'
    \documentclass{article}
    \usepackage{amsmath}
    \begin{document}
    %
    % Define `sa' and `ga':
    \def\ga                  #1{\csname myarg #1\endcsname}       % get arg
    \def\sa#1#2{\expandafter\def\csname myarg #1\endcsname{#2}}   % set arg
    %
    % The contents of "/tmp/o":
    \def \foobar  {\begin{pmatrix}a&b\cr c&d\cr \end{pmatrix}}
    \sa {foo bar} {\begin{pmatrix}a&b\cr c&d\cr \end{pmatrix}}
    %
    $\foobar
     \ga{foo bar}
    $
  \end{document}
%%%
  pdflatex sa-and-ga-demo.tex
  xpdf     sa-and-ga-demo.pdf


 * Compare:
 * 
 *    \def\foobar{...}  % stores    in   \foobar
 *        \foobar       % retrieves from \foobar
 *    \sa{foo bar}{...} % stores    in   \csname myarg foo bar\endcsname
 *    \ga{foo bar}      % retrieves from \csname myarg foo bar\endcsname
 * 
 * "\csname" and the trick with "\expandafter", are explained in
 * p.40 of the TeXBook - but in a part with two "dangerous bend"
 * signs. Knuth explains the dangeround bends in page v of the
 * introduction - they mean, roughly, "don't read that paragraph unless
 * you need to"... so: I'm using some dirty tricks here, sorry... =(
 *
 * I started using \sa and \ga for this, many years ago:
 *   (find-es "tex" "more-than-9-args")
*/