* Evaluating simple inline expressions in org-mode
@ 2016-02-03 13:45 Gary Oberbrunner
2016-02-03 13:58 ` Ken Mankoff
2016-02-03 16:09 ` Eric S Fraga
0 siblings, 2 replies; 8+ messages in thread
From: Gary Oberbrunner @ 2016-02-03 13:45 UTC (permalink / raw)
To: Orgmode Mailing List
[-- Attachment #1: Type: text/plain, Size: 561 bytes --]
I'd like to just have a few inline formulas evaluated in my org-mode
document (I'm exporting to LaTeX if that matters). Something like this:
=========================
# a constant:
# pi = 3.14159
The value of interest is {{{2*$pi}}}.
=========================
I want that to export as:
The value of interest is 6.28318.
Of course I made up that {{{}}} syntax, that's not a real thing. I've seen
org-mode tables with calc, but I'm hoping for something simpler, just
inline math expression eval. (I'm fine with elisp, calc, whatever.)
Possible?
--
Gary
[-- Attachment #2: Type: text/html, Size: 851 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Evaluating simple inline expressions in org-mode
2016-02-03 13:45 Evaluating simple inline expressions in org-mode Gary Oberbrunner
@ 2016-02-03 13:58 ` Ken Mankoff
2016-02-03 16:09 ` Eric S Fraga
1 sibling, 0 replies; 8+ messages in thread
From: Ken Mankoff @ 2016-02-03 13:58 UTC (permalink / raw)
To: Gary Oberbrunner; +Cc: Orgmode Mailing List
On 2016-02-03 at 08:45, Gary Oberbrunner <garyo@oberbrunner.com> wrote:
> I'd like to just have a few inline formulas evaluated in my org-mode
> document (I'm exporting to LaTeX if that matters). Something like this:
>
> =========================
> # a constant:
> # pi = 3.14159
>
> The value of interest is {{{2*$pi}}}.
> =========================
>
> I want that to export as:
>
> The value of interest is 6.28318.
The following works for me:
#+BEGIN_SRC python :results output :exports none :session foo
val = 2
#+END_SRC
#+RESULTS:
The value of interest is src_python[:session foo]{np.round(2*val,3)} {{{results(=4=)}}} and src_octave{2*pi} {{{results(=6.283185307179586=)}}}
You don't need the code block + session to set up a value for your example with pi, since Python and Octave know what pi is, but I demonstrate it here because you might want to work with something other than pi.
Python needs a [:session] to work for some reason on my system, but that is also beneficial so you can set up your variables. Note that your :session code block could take in an Org table as a header argument and define them in code from the table, so your actual variables are defined in an Org table, not in code. This is beginning to approach true literate programming...
-k.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Evaluating simple inline expressions in org-mode
2016-02-03 13:45 Evaluating simple inline expressions in org-mode Gary Oberbrunner
2016-02-03 13:58 ` Ken Mankoff
@ 2016-02-03 16:09 ` Eric S Fraga
2016-02-03 16:14 ` Gary Oberbrunner
2016-02-04 7:01 ` H. Dieter Wilhelm
1 sibling, 2 replies; 8+ messages in thread
From: Eric S Fraga @ 2016-02-03 16:09 UTC (permalink / raw)
To: Gary Oberbrunner; +Cc: Orgmode Mailing List
On Wednesday, 3 Feb 2016 at 08:45, Gary Oberbrunner wrote:
[...]
> Of course I made up that {{{}}} syntax, that's not a real thing. I've seen
> org-mode tables with calc, but I'm hoping for something simpler, just
> inline math expression eval. (I'm fine with elisp, calc, whatever.)
As Ken has shown¸ you can use babel for inline evaluation. Note that
Emacs calc is one of the languages supported by babel so src_calc(2*pi)
works as well. I'm not sure how you define variables in calc however...
--
: Eric S Fraga (0xFFFCF67D), Emacs 25.0.90.1, Org release_8.3.3-535-g7213aa
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Evaluating simple inline expressions in org-mode
2016-02-03 16:09 ` Eric S Fraga
@ 2016-02-03 16:14 ` Gary Oberbrunner
2016-02-04 7:01 ` H. Dieter Wilhelm
1 sibling, 0 replies; 8+ messages in thread
From: Gary Oberbrunner @ 2016-02-03 16:14 UTC (permalink / raw)
To: Gary Oberbrunner, Orgmode Mailing List
[-- Attachment #1: Type: text/plain, Size: 944 bytes --]
Thanks Ken, that works great! With session as a file property (#+PROPERTY:
header-args:python :session foo) the inline expressions aren't even _all_
that verbose. And I can use arbitrary python, not just math. Very nice!
On Wed, Feb 3, 2016 at 11:09 AM, Eric S Fraga <e.fraga@ucl.ac.uk> wrote:
> On Wednesday, 3 Feb 2016 at 08:45, Gary Oberbrunner wrote:
>
> [...]
>
> > Of course I made up that {{{}}} syntax, that's not a real thing. I've
> seen
> > org-mode tables with calc, but I'm hoping for something simpler, just
> > inline math expression eval. (I'm fine with elisp, calc, whatever.)
>
> As Ken has shown¸ you can use babel for inline evaluation. Note that
> Emacs calc is one of the languages supported by babel so src_calc(2*pi)
> works as well. I'm not sure how you define variables in calc however...
> --
> : Eric S Fraga (0xFFFCF67D), Emacs 25.0.90.1, Org release_8.3.3-535-g7213aa
>
--
Gary
[-- Attachment #2: Type: text/html, Size: 1430 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Evaluating simple inline expressions in org-mode
2016-02-03 16:09 ` Eric S Fraga
2016-02-03 16:14 ` Gary Oberbrunner
@ 2016-02-04 7:01 ` H. Dieter Wilhelm
2016-02-04 8:38 ` Eric S Fraga
1 sibling, 1 reply; 8+ messages in thread
From: H. Dieter Wilhelm @ 2016-02-04 7:01 UTC (permalink / raw)
To: emacs-orgmode
Eric S Fraga <e.fraga@ucl.ac.uk> writes:
> On Wednesday, 3 Feb 2016 at 08:45, Gary Oberbrunner wrote:
>
> [...]
>
>> Of course I made up that {{{}}} syntax, that's not a real thing. I've seen
>> org-mode tables with calc, but I'm hoping for something simpler, just
>> inline math expression eval. (I'm fine with elisp, calc, whatever.)
>
> As Ken has shown¸ you can use babel for inline evaluation. Note that
> Emacs calc is one of the languages supported by babel so src_calc(2*pi)
It must be src_calc{2*pi} though.
> works as well. I'm not sure how you define variables in calc however...
You could write something like src_calc[:var x=3.41]{2*x} :-)
Dieter
--
Best wishes
H. Dieter Wilhelm
Darmstadt, Germany
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Evaluating simple inline expressions in org-mode
2016-02-04 7:01 ` H. Dieter Wilhelm
@ 2016-02-04 8:38 ` Eric S Fraga
2016-02-07 15:45 ` H. Dieter Wilhelm
0 siblings, 1 reply; 8+ messages in thread
From: Eric S Fraga @ 2016-02-04 8:38 UTC (permalink / raw)
To: H. Dieter Wilhelm; +Cc: emacs-orgmode
On Thursday, 4 Feb 2016 at 08:01, H. Dieter Wilhelm wrote:
> It must be src_calc{2*pi} though.
Indeed. I mistyped. <blush>
>> works as well. I'm not sure how you define variables in calc however...
>
> You could write something like src_calc[:var x=3.41]{2*x} :-)
Yes but can you save a variable for use in a later expression?
--
: Eric S Fraga (0xFFFCF67D), Emacs 25.0.90.1, Org release_8.3.3-535-g7213aa
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Evaluating simple inline expressions in org-mode
2016-02-04 8:38 ` Eric S Fraga
@ 2016-02-07 15:45 ` H. Dieter Wilhelm
2016-02-08 9:33 ` Eric S Fraga
0 siblings, 1 reply; 8+ messages in thread
From: H. Dieter Wilhelm @ 2016-02-07 15:45 UTC (permalink / raw)
To: emacs-orgmode
>>> works as well. I'm not sure how you define variables in calc however...
>>
>> You could write something like src_calc[:var x=3.41]{2*x} :-)
>
> Yes but can you save a variable for use in a later expression?
Well, at the moment I don't know another way as defining the calc
variables on the lisp level:
#+BEGIN_SRC emacs-lisp
(setq var-foo 9.81
var-x 3.14
var-bar ...)
#+END_SRC
Note: `var-' is a special prefix for the calc variables `foo', `x',
...
And then something like
src_calc{2*x}
is working.
--
Best wishes
H. Dieter Wilhelm
Darmstadt, Germany
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Evaluating simple inline expressions in org-mode
2016-02-07 15:45 ` H. Dieter Wilhelm
@ 2016-02-08 9:33 ` Eric S Fraga
0 siblings, 0 replies; 8+ messages in thread
From: Eric S Fraga @ 2016-02-08 9:33 UTC (permalink / raw)
To: H. Dieter Wilhelm; +Cc: emacs-orgmode
On Sunday, 7 Feb 2016 at 16:45, H. Dieter Wilhelm wrote:
>>>> works as well. I'm not sure how you define variables in calc however...
>>>
>
>>> You could write something like src_calc[:var x=3.41]{2*x} :-)
>>
>> Yes but can you save a variable for use in a later expression?
>
> Well, at the moment I don't know another way as defining the calc
> variables on the lisp level:
>
> #+BEGIN_SRC emacs-lisp
> (setq var-foo 9.81
> var-x 3.14
> var-bar ...)
> #+END_SRC
>
> Note: `var-' is a special prefix for the calc variables `foo', `x',
Thanks for this. Very useful to know. Using this, I've created a macro
which will be quite useful for me in preparing some lecture notes with
simple calculations for illustration:
#+begin_src org
,#+macro: calculate $2 \leftarrow src_calc[:exports code]{$1} = src_emacs-lisp{(setq var-$2 (string-to-number (calc-eval "evalv($1)")))}
An example:
{{{calculate(x+y,z)}}}
#+end_src
--
: Eric S Fraga (0xFFFCF67D), Emacs 25.0.90.1, Org release_8.3.3-535-g7213aa
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2016-02-08 11:06 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-02-03 13:45 Evaluating simple inline expressions in org-mode Gary Oberbrunner
2016-02-03 13:58 ` Ken Mankoff
2016-02-03 16:09 ` Eric S Fraga
2016-02-03 16:14 ` Gary Oberbrunner
2016-02-04 7:01 ` H. Dieter Wilhelm
2016-02-04 8:38 ` Eric S Fraga
2016-02-07 15:45 ` H. Dieter Wilhelm
2016-02-08 9:33 ` Eric S Fraga
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).