emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [babel] Variable support for ob-maxima
@ 2011-03-23 15:04 Thomas Holst
  0 siblings, 0 replies; 3+ messages in thread
From: Thomas Holst @ 2011-03-23 15:04 UTC (permalink / raw)
  To: emacs-orgmode@gnu.org

[-- 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"))
 

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-03-24  7:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <.ywodd3lh3mjt@de.bosch.com>
2011-03-23 17:44 ` [babel] Variable support for ob-maxima Eric Schulte
2011-03-24  7:25   ` Thomas Holst
2011-03-23 15:04 Thomas Holst

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).