From mboxrd@z Thu Jan 1 00:00:00 1970 From: Thomas Holst Subject: [babel] Variable support for ob-maxima Date: Wed, 23 Mar 2011 16:04:54 +0100 Message-ID: <38669.8577357039$1300892795@news.gmane.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=Multipart0o0o0o0o0o0o0o" Return-path: Received: from [140.186.70.92] (port=44229 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Q2PcP-0005gM-9z for emacs-orgmode@gnu.org; Wed, 23 Mar 2011 11:05:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Q2PcK-0008Hh-Jf for emacs-orgmode@gnu.org; Wed, 23 Mar 2011 11:05:04 -0400 Received: from smtp2-v.fe.bosch.de ([139.15.237.6]:55353) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Q2PcK-0008HL-BH for emacs-orgmode@gnu.org; Wed, 23 Mar 2011 11:05:00 -0400 Received: from vsmta6.fe.internet.bosch.com (unknown [10.4.98.30]) by imta16.fe.bosch.de (Postfix) with ESMTP id 75D8627EF2 for ; Wed, 23 Mar 2011 16:04:58 +0100 (CET) Received: from si-hub01.de.bosch.com (vsgw4.fe.internet.bosch.com [10.4.98.12]) by vsmta6.fe.internet.bosch.com (Postfix) with ESMTP id 17EF439D0BE1 for ; Wed, 23 Mar 2011 16:04:58 +0100 (CET) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: "emacs-orgmode@gnu.org" --=Multipart0o0o0o0o0o0o0o Content-Type: text/plain 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 --=Multipart0o0o0o0o0o0o0o Content-Type: text/x-patch Content-Disposition: attachment; filename="ob-maxima.el.patch" Content-Description: Variable support for ob-maxima --- 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")) --=Multipart0o0o0o0o0o0o0o--