Re: [O] [babel] Variable support for ob-maxima

2011-03-24 Thread Thomas Holst
Hi Eric,

ยท Eric Schulte  wrote:
> Thomas Holst  writes:

[... snip ...]

> Great, thanks for sharing this patch, I'd very much like to apply this.
> Have you seen the instructions for contributing to Org-mode [1]?  Any
> patch over 10 lines long requires signed FSF copyright attribution
> papers.  Please let me know if you are able to complete the copyright
> assignment, once that is done I will apply this patch.

I will look at the FSF papers this evening. I would be really happy to
contribute to org-mode and babel.

I will send a notice once the papers are signed.

>>
>> 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:
>>
>
> The example below like a nice application, you may also want to use
> "begin_src latex" blocks to display equations resulting from maxima code
> blocks.

I will look into this. The example shows my first attempt to accomplish
my goal of using maxima as math tool and pretty print the equations with
LaTeX code. All within org mode - of course!

Best regards ...
  Thomas



Re: [O] [babel] Variable support for ob-maxima

2011-03-23 Thread Eric Schulte
Thomas Holst  writes:

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

Great, thanks for sharing this patch, I'd very much like to apply this.
Have you seen the instructions for contributing to Org-mode [1]?  Any
patch over 10 lines long requires signed FSF copyright attribution
papers.  Please let me know if you are able to complete the copyright
assignment, once that is done I will apply this patch.

>
> 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:
>

The example below like a nice application, you may also want to use
"begin_src latex" blocks to display equations resulting from maxima code
blocks.

Cheers -- Eric

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

Footnotes: 
[1]  http://orgmode.org/worg/org-contribute.php



[O] [babel] Variable support for ob-maxima

2011-03-23 Thread Thomas Holst
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
--- 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"))