Reproducible Research "Plugin"

Thanks, I really have to try this solution! It’s very simple and works without any fancy texmacs plugins.

Taking this nice approach of @pireddag to the extreme one can then have just TeXmacs macros for the parameters and then include in the pramble an external small documents which is automatically generated by python and which do the only job of defining such macros giving them the right computed values. This would avoid to use scheme and extern macros for this job.

1 Like

I can’t seem to find a Scheme function to define TeXmacs macros or to set environment variables. Is this possible?

I do not know if hat exists or not, but if it does not exist maybe the reason is that you can put the Scheme tree for assignment as argument of a stree->tree function.

(stree->tree '(assign "b" "bb"))

I thought that this would make the code for the “reproducible research” document easier

I would get Python to generate data in a different form:

(stree->tree '(assign "p-value" "0.032"))
(stree->tree '(assign "nr-of-successes" "245"))
(stree->tree '(assign "ratio" (frac "a" "b")))

but it did not work in the test that I did.
It worked in a Scheme session, but not loading a Scheme file.

1 Like

Ah, I see the problem now. I was using texmacs-expand, which has a similar effect, but the effects only appear below the session, not for the whole document. It is the same with stree->tree

I think the reason why it did not work for me with load, both in the preamble within an extern and inside the Scheme session, is that the command needs to insert the tree inside the document, and neither extern nor load do that.
I do not know (did not yet learn) how to do that with the tree-manipulation routines. If one does that, one could obtain the definitions from a file written with the Scheme syntax alone.

I find it “unnatural” that the scope of an environment variable starts from the point in the document where it is defined :wink: would find it more natural if it were the whole document.

This way I could just add

<extern|p-value>

to my document, right? If so, then this is very elegant! Sorry for not trying it out, I’m at a work computer without texmacs right now…

The only disadvantage of this option is that it doesn’t make it easy to distinguish visually “fixed” values from dynamic values. the <tag|id|value> macro adds a little flag that is useful. But I could have my expressions expand to things using the <tag|...|...> macros, so it’s ok. I’m quite happy to generate (simple) scheme code from Python so that I don’t have to duplicate functionality between Scheme and Python.

I should namespace these identifiers somehow, though, to avoid overwriting default macros.

Either <p-value> or <value|p-value> would work, and also <tag|1|<value|p-value>> (I do not know what the id argument of tag does, though, I put 1 as a plausible value that crossed my mind).

The obstacle that I am not able to overcome is how to inject the trees you generate with the Scheme commands in the external files inside TeXmacs (I know where it is described—Scheme developer manual, I tried time ago and I did not succeed—crashed TeXmacs). But maybe @jeroen and @mgubi know.

Edit: on a second thought, this (once it works :wink: ) looks equivalent to the suggestion of @mgubi above.

You can use a prefix for your macros, also look at \merge and \compound which allow you to create new tags programmatically, that is you can still define your \tag macro with an argument myval which inside just invoke a \myprefix-$myval macro. This can be done using \merge{myprefix-}{\myval}.

I think you need to use

<extern|(lambda () p-value)>

extern only takes functions, so you need to put it in a lambda.

I think I had not gotten the meaning of @tmbb 's question.

Now that I (maybe) do, I propose a refinement

 (tm-define (p-value) "0.032")

(p-value is defined as a function with no arguments)

with

<extern|p-value>

I thought we can also conveniently wrap it in another macro which adds the tag tag, maybe

<assign|t-ext|<macro|body|<tag|<arg|body>|<extern|<arg|body>>>>>

but it does not work, when I apply this, the argument gets prepended with a line break, and the error

<unnamed port>: In expression (#<eof>):
<unnamed port>: Wrong type to apply: #<eof>

is displayed in the terminal.
But writing by hand

<tag|ratio|<extern|ratio>>

seems to work.

I think you have to use quasi and unquote to construct this properly.

Small variation on the approach of @pireddag : one could just have a table maybe defined in scheme and then lookup the various values with \extern, instead of having a tm-define for every new parameter. The table is read from scheme with an extern in the preamble, or with some command which can be run with a key-binding, so that one can refresh the values.

Do you mean

<assign|t-ext-t|<macro|body|<quasiquote|<tag|<arg|body>|<unquote|<extern|<arg|body>>>>>>>

??
It works better than my first attempt, but tag has not effect on the output. I do not understand the logic of what I am writing.

Not quite, maybe I would try

<assign|t-ext-t|<macro|body|<tag|<arg|body>|<quasiquote|<extern|<unquote|<arg|body>>>>>>>

Or maybe

<assign|t-ext-t|<macro|body|<quasiquote|<tag|<arg|body>|<extern|<unquote|<arg|body>>>>>>>

That would be

<assign|show-var|<macro|var|<extern|(lambda (var) (stree-\<gtr\>tree
(assoc-ref var-list (tree-\<gtr\>stree var))))|<arg|var>>>>

that works with a list of variables written

(tm-define var-list '(("a" . 2) ("b" . 3)))

and loaded with another command (the list of variables must have the same name that appears inside the extern).
Maybe this is a good solution, that does not fill (pollute :wink: )the environment with variables. It would be nice to make it work inside a tag too; I tried your suggestions and the output of extern is still typeset without a flag.

Do you want a flag? Then maybe one has to add it explicitly no? For me the following works

(tm-define var-list '(("a" . 2) ("b" . 3)))
(tm-define (my-lookup var) (stree->tree (assoc-ref var-list (tree->stree var))))

and in the document

<assign|my-lookup|<macro|var|<flag|<arg|var>|#aaf><extern|my-lookup|<arg|var>>>>

or (maybe just the first form suffice)

<assign|my-lookup-2|<macro|var|<flag|<arg|var>|#aaf><quasi|<extern|my-lookup|<unquote|<arg|var>>>>>>

@tmbb wrote that he was using tag to get the flag (message), then I got taken by tag and did not think about looking for another way to show the flag.

So: @tmbb, here is a good way of keeping a document synchronized with the output of a program. You can put the definition of my-lookup in a module, var-list gets composed by Python and the corresponding file either loaded with an extern macro in your preamble or loaded with a command assigned to a key combination (it is also possible to add the update command to the menu and I expect it is possible to redefine “Update all” to include the re-loading of the file).
var-list has to be a list of pairs, hence the dot.