ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Mojca Miklavec <mojca.miklavec.lists@gmail.com>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: Lua and module gnuplot: Manipulate Gnuplot script with Lua
Date: Mon, 12 Dec 2011 23:07:13 +0100	[thread overview]
Message-ID: <CALBOmsaM=CwM6qfxypE21=VaPV_PcZjX-woZs=8Yt718w+19Ag@mail.gmail.com> (raw)
In-Reply-To: <1323725964.26949.90.camel@mattotaupa>

On Mon, Dec 12, 2011 at 22:39, Paul Menzel wrote:
> Dear ConTeXt folks,
>
>
> I have two coefficients saved in a text file
>
>        -0.157737135681261
>        -0.211443105668896
>
> and I want to use them to plot a line with gnuplot.
>
> Having several of those files I want to use Lua to create the formula
> for the line for me.
>
> I have not yet figured out how to read values from a file in Lua and
> just tried to print a constant to the gnuplot command.
>
> I tried the following.
>
>        \usemodule[gnuplot]
>
>        \startGNUPLOTscript[lua]
>           plot \ctxlua{context(1.2 * x + 3)}
>        \stopGNUPLOTscript

This won't work. The reason is that contents between
\startGNUPLOTscript and \stopGNUPLOTscript are not parsed by ConTeXt
on purpose.

It was very very very painful if not impossible to get it right with
parsing. I often do things like
    \def\E#1{$10^{#1}$}

    \startGNUPLOTscript[name]
    set logscale y
    set format y "\\E{%T}"
    plot sin(x) t '$\sin(x) [\E{3}]$' # usually something else with
logscale, but just to show the point
    \stopGNUPLOTscript

and if ConTeXt starts parsing this it becomes a pure nightmare.
Realize that it can easily happen that ConTeXt then writes out
    set logscale y
    set format y "<some junk leftovers from \\>
    E<ungrouped { since % starts comment>
    plot sin(x) t '\math{\mathrm{sin}(x) [{10^{3}}]}' <arbitrary junk
from expansion ...>



However, \startGNUPLOTinclusions is parsed and you could in theory
misuse that, but it would be very bad coding practice.

It would be a lot better to come up with some proposal how to change
the interface to enable what you want to do. (To be honest, it is
still on my todo list to migrate to Aditya's filter module in the
background, but I don't find the courage to start since I have no idea
how many bugs will creep in.)

But a serious question: what *exactly* do you want to do with lua that
you are unable to do with some (even though ugly) gnuplot trickery?
Gnuplot does have some basic programmable capabilities. I can imagine
that you should be able to do something like:

# params.dat
a=-0.157737135681261
b=-0.211443105668896

\startGNUPLOTinclusions
f(x,a,b)=a*x+b
\stopGNUPLOTinclusions

\startGNUPLOTscript[name]
load 'params.dat'
plot f(x,a,b)
\stopGNUPLOTscript

And if needed you can create params.dat on the fly (but I believe that
the same should be doable even if you start with just pure data values
and do some dirty tricks in gnuplot).

Yet another trick could be to create a text file on the fly with lua
(if you really really really need that) and then your script only says
something like.

# bla.plt created on the fly with lua scripts
plot 1.2 * x + 3

\startGNUPLOTscript[bla]
load 'bla.plt'
\stopGNUPLOTscript

I leave creating a suitable 'bla.plt' with lua to others ...

Mojca
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

  reply	other threads:[~2011-12-12 22:07 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-12 21:39 Paul Menzel
2011-12-12 22:07 ` Mojca Miklavec [this message]
2011-12-12 22:13   ` Mojca Miklavec
2011-12-12 22:20     ` Hans Hagen
2011-12-12 22:44     ` Paul Menzel
2011-12-12 22:35   ` Paul Menzel
2011-12-13  9:10     ` Hans Hagen
2011-12-13  6:52   ` Aditya Mahajan
2011-12-12 22:17 ` Hans Hagen
2011-12-12 22:27   ` Mojca Miklavec
2011-12-13  6:45     ` Aditya Mahajan

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CALBOmsaM=CwM6qfxypE21=VaPV_PcZjX-woZs=8Yt718w+19Ag@mail.gmail.com' \
    --to=mojca.miklavec.lists@gmail.com \
    --cc=ntg-context@ntg.nl \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 NNTP newsgroup(s).