emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Thomas Holst <thomas.holst@de.bosch.com>
To: "emacs-orgmode@gnu.org" <emacs-orgmode@gnu.org>
Subject: [babel] Variable support for ob-maxima
Date: Wed, 23 Mar 2011 16:04:54 +0100	[thread overview]
Message-ID: <38669.8577357039$1300892795@news.gmane.org> (raw)

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

Hello,

recent org-mode versions have support for maxima via org-babel. But
there is no support vor variables. I implemented basic support for
variables. A header =var: eq="x^2"= is translated to:

#+begin_src maxima
  eq : x^2;
#+end_src

I attached a patch to this eMail.

Now I can use the output from one maxima block and make a LaTeX equation
out of it with one maxima code block and reuse the output and make further
maipulations with it. I find it dificult to explain what I want to do,
so here is an example:

* Reuse Output of maxima code blocks

  #+source: eq1()
  #+begin_src maxima :exports none :results output verbatim
    display2d:false;
    eq1: x**2;
    print(eq1);
  #+end_src
  #+results: eq1
  : x^2 

  Pretty print equation with LaTeX:

  #+source: eq1-latex
  #+begin_src maxima :exports results :results output latex :var eq=eq1()
    print("\\begin{equation}");
    print(tex1(eq));
    print("\\end{equation}");
  #+end_src

  #+results: eq1-latex
  #+BEGIN_LaTeX
  \begin{equation} 
  x^2 
  \end{equation} 
  #+END_LaTeX

  Do some further calculation / maipulation to equation

  #+source: eq2()
  #+begin_src maxima :exports none :results output verbatim :var eq=eq1()
    display2d:false;
    eq2 : eq + sin(x);
    print(eq2);
  #+end_src
  #+results: eq5
  : sin(x)+x^2 

  Pretty print second equation:

  #+source: eq2-latex
  #+begin_src maxima :exports results :results output latex :var eq=eq2()
    print("\\begin{equation}");
    print(tex1(eq));
    print("\\end{equation}");
  #+end_src

  #+results:
  #+BEGIN_LaTeX
  \begin{equation} 
  \sin x+x^2 
  \end{equation} 
  #+END_LaTeX

With this workflow I have all org features for docmentation of math or
engineering works.

I am a lisp beginner so my lisp code may not be the best. If there are
better ways to accomplish variable support please let me know.

-- 
Best regards
  Thomas

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Variable support for ob-maxima --]
[-- Type: text/x-patch, Size: 2654 bytes --]

--- a/ob-maxima.el	2011-03-17 08:37:24.977544600 +0100
+++ b/ob-maxima.el	2011-03-23 14:13:26.780223600 +0100
@@ -47,29 +47,48 @@
   "Expand a block of Maxima code according to its header arguments."
   body)
 
+;; This function expands the body of a source code block by doing
+;; things like prepending argument definitions to the body, it should
+;; be called by the `org-babel-execute:maxima' function below.
+(defun org-babel-expand-body:maxima (body params &optional processed-params)
+  "Expand BODY according to PARAMS, return the expanded body."
+  (concat
+   (mapconcat ;; define any variables
+    (lambda (pair)
+      (format "%s : %s;"
+              (car pair) (org-babel-maxima-var-to-maxima (cdr pair))))
+    (mapcar #'cdr (org-babel-get-header params :var)) "\n") "\n" body "\n"))
+
 (defun org-babel-execute:maxima (body params)
   "Execute a block of Maxima entries with org-babel.  This function is
 called by `org-babel-execute-src-block'."
   (message "executing Maxima source code block")
   (let* ((result-params (split-string (or (cdr (assoc :results params)) "")))
-	 (cmdline (cdr (assoc :cmdline params)))
-	 (in-file (org-babel-temp-file "maxima-"))
-	 (cmd (format "maxima --very-quiet -r 'batchload(%S)$' %s"
-		      in-file cmdline)))
-    (with-temp-file in-file (insert body))
+         (cmdline (cdr (assoc :cmdline params)))
+         (in-file (org-babel-temp-file "maxima-"))
+         (cmd (format "maxima --very-quiet -r 'batchload(%S)$' %s"
+                      in-file cmdline)))
+    (with-temp-file in-file
+      (insert (org-babel-expand-body:maxima body params)))
     (message cmd)
     ((lambda (raw) ;; " | grep -v batch | grep -v 'replaced' | sed '/^$/d' "
        (mapconcat
-	#'identity
-	(delq nil
-	      (mapcar (lambda (line)
-			(unless (or (string-match "batch" line)
-				    (string-match "^rat: replaced .*$" line)
-				    (= 0 (length line)))
-			  line))
-		      (split-string raw "[\r\n]"))) "\n"))
+        #'identity
+        (delq nil
+              (mapcar (lambda (line)
+                        (unless (or (string-match "batch" line)
+                                    (string-match "^rat: replaced .*$" line)
+                                    (= 0 (length line)))
+                          line))
+                      (split-string raw "[\r\n]"))) "\n"))
      (org-babel-eval cmd ""))))
 
+
+(defun org-babel-maxima-var-to-maxima (var)
+  "Convert an elisp var into a string of template source code
+specifying a var of the same value."
+  (format "%s" var))
+
 (defun org-babel-prep-session:maxima (session params)
   (error "Maxima does not support sessions"))
 

             reply	other threads:[~2011-03-23 15:05 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-23 15:04 Thomas Holst [this message]
     [not found] <.ywodd3lh3mjt@de.bosch.com>
2011-03-23 17:44 ` Eric Schulte
2011-03-24  7:25   ` Thomas Holst

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='38669.8577357039$1300892795@news.gmane.org' \
    --to=thomas.holst@de.bosch.com \
    --cc=emacs-orgmode@gnu.org \
    --subject='Re: [babel] Variable support for ob-maxima' \
    /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

Code repositories for project(s) associated with this 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).