emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Leo Butler <Leo.Butler@umanitoba.ca>
To: Org Mode Mailing List <emacs-orgmode@gnu.org>
Subject: [TIP] Exporting Maxima results to LaTeX
Date: Tue, 7 Feb 2023 21:40:54 +0000	[thread overview]
Message-ID: <87o7q5rw62.fsf@t14.reltub.ca> (raw)

[-- Attachment #1: Type: text/plain, Size: 118 bytes --]

Attached is a self-documented setup to do what the subject line says.
Comments/suggestions welcome.

Best,
Leo


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: tip.org --]
[-- Type: text/x-org; name="maxima-tip.org", Size: 3910 bytes --]

#+TITLE: Tip for exporting Maxima results to LaTeX
#+AUTHOR: Leo Butler
#+OPTIONS: H:2 toc:nil num:nil tags:nil todo:nil
#+LATEX_CLASS: article
#+LATEX_HEADER: \usepackage{color}
#+LATEX_COMPILER: lualatex
#+STARTUP: beamer
#+PROPERTY: header-args:maxima :eval never-export :exports results :results raw drawer

* Goal
Generate @@latex:\LaTeX{}@@ code from Maxima code.

* Setup
** maxima-init.lisp
The command =org-babel-execute:maxima= in =lisp/ob-maxima.el= uses the Maxima command ~batchload~ to execute Maxima code. This is a very tight-lipped loader, so we over-write ~batchload~ with ~batch~. We also ~load~ an init file:

#+begin_example
,#+begin_src maxima :tangle maxima-init.lisp :exports none
  (defun $batchload (file) (mfuncall '$batch file))
  ($load "./maxima-init.mac")
,#+end_src
#+end_example

On tangling, this produces the ~common-lisp~ output file ~maxima-init.lisp~. It will be pre-loaded into Maxima.

#+begin_src maxima :tangle maxima-init.lisp :exports none
  (defun $batchload (file) (mfuncall '$batch file))
  ($load "./maxima-init.mac")
#+end_src

** maxima-init.mac
Next, we need to create an init file for Maxima that will provide an output printer that produces @@latex:\LaTeX{}@@ output. One option would be to use the ~imaxima~ printer. Here is another option that uses the ~alt-display~ package.
The code replaces the default printer with ~org_tex_display~. It also sets the ~epilog~ prompt, so that the final ~#+begin_example~ is terminated.

#+begin_example
,#+begin_src maxima :tangle maxima-init.mac :exports none
  load("alt-display.mac") $
  set_prompt('epilog,printf(false,"~%#+end_example")) $
  define_alt_display(org_tex_display(x),
    block([], printf(true,"#+end_example~%#+begin_export latex~%"),
      printf(true,"\\textcolor{blue}{(\\~a~d)} ",outchar,linenum-1), tex(second(x)), printf(true,"~&#+end_export~%#+begin_example~%(input) "))) $
  set_alt_display(2,org_tex_display) $
  display2d:true $
  printf(true,"#+begin_example~%(input) ") $
  linenum : 0 $
,#+end_src
#+end_example

#+begin_src maxima :tangle maxima-init.mac :exports none
  load("alt-display.mac") $
  set_prompt('epilog,printf(false,"~%#+end_example")) $
  define_alt_display(org_tex_display(x),
    block([], printf(true,"#+end_example~%#+begin_export latex~%"),
      printf(true,"\\textcolor{blue}{(\\~a~d)} ",outchar,linenum-1), tex(second(x)), printf(true,"~&#+end_export~%#+begin_example~%(input) "))) $
  set_alt_display(2,org_tex_display) $
  display2d:true $
  printf(true,"#+begin_example~%(input) ") $
  linenum : 0 $
#+end_src

* An example
Here is an example that computes the derivative of a composite function.

#+name: chain-rule
#+begin_src maxima :exports results :results raw drawer :cmdline -p ./maxima-init.lisp
  (gradef(f(u,v),f_1(u,v),f_2(u,v)), 'done);
  diff(f(x^2-y^2,x*y),x);
  diff(f(x^2-y^2,x*y),y);
#+end_src

#+RESULTS: chain-rule
:results:
#+begin_example
(input) 
read and interpret /tmp/babel-hhTrJS/maxima-0m0DnH.max
(gradef(f(u,v),f_1(u,v),f_2(u,v)),'done)
#+end_example
#+begin_export latex
\textcolor{blue}{(\%o1)} $$\mathbf{done}$$
#+end_export
#+begin_example
(input) 
diff(f(x^2-y^2,x*y),x)
#+end_example
#+begin_export latex
\textcolor{blue}{(\%o2)} $$y\,f_{2}\left(x^2-y^2 , x\,y\right)+2\,x\,f_{1}\left(x^2-y^2 , x\,y
 \right)$$
#+end_export
#+begin_example
(input) 
diff(f(x^2-y^2,x*y),y)
#+end_example
#+begin_export latex
\textcolor{blue}{(\%o3)} $$x\,f_{2}\left(x^2-y^2 , x\,y\right)-2\,y\,f_{1}\left(x^2-y^2 , x\,y
 \right)$$
#+end_export
#+begin_example
(input) 
gnuplot_close()
#+end_example
:end:

** Two annoyances
The initial line =read and interpret...= and that final, dangling
input line with ~gnuplot_close()~ are nuisances. They can be easily
suppressed, but that requires patching ~ob-maxima.el~. That's another
story.

[-- Attachment #3: maxima-tip.pdf --]
[-- Type: application/pdf, Size: 34392 bytes --]

             reply	other threads:[~2023-02-07 21:46 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-07 21:40 Leo Butler [this message]
2023-02-07 22:13 ` [TIP] Exporting Maxima results to LaTeX Thomas S. Dye
2023-02-08 15:43 ` Max Nikulin
2023-02-08 16:11   ` Fraga, Eric
2023-02-08 20:40   ` Leo Butler
2023-02-11 11:39     ` Max Nikulin
2023-02-14 15:13       ` Leo Butler

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87o7q5rw62.fsf@t14.reltub.ca \
    --to=leo.butler@umanitoba.ca \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).