Message Boards Message Boards

Freeform section of a notebook sent to a function for evaluation?

Posted 4 years ago

Hello, Is it possible to define a custom section of a notebook ( think for instance to type plain text ) and pass this section to a function as an argument (and escape the double quotes). For instance:

Cell #10: this is a section of this Mathematica notebook. these "contents" will be passed to the function below.

Cell #11: CustomFunction["this is a section of this Mathematica notebook.\nthese \"contents\" will be passed to the function below."]

Where of course modifying the contest of the freeform section would change the argument of CustomFunction[]. Or is there a construct like CustomFunction[%] which would indicate the contents of the cell above? Or perhaps CustomFunction[Cell[10]] or such?

Any ideas?

POSTED BY: jerome ibanes
12 Replies
Posted 4 years ago

Thank you Hans, I'll investigate; many thanks!

POSTED BY: jerome ibanes
Posted 4 years ago

Thanks Hans, it would have been nice; maybe with a button instead?

POSTED BY: jerome ibanes
Posted 4 years ago

A Button has to be pressed to do its job. So the process is then no longer fully automatic. Two, manual, actions are required:

  • Edit the string argument in the text cell (always required unless the argument is modified programmatically)
  • Press the button to trig evaluation and get the result

But if this "dual action" is not a problem there are several ways to go ahead. With functions such as Manipulate, InputField, DialogInput. And others.

POSTED BY: Hans Milton
Posted 4 years ago

This is perfect, thank you so much Hans!

Finally, can I have the "evaluating cell" automatically updated when the "text cell" is updated?

POSTED BY: jerome ibanes
Posted 4 years ago

Jerome, I cannot find a simple way to automatically trig evaluation when contents of the text cell are changed. Have experimented a bit with the cell option CellEventActions together with Dynamic. But no luck.

Maybe someone else has ideas.

POSTED BY: Hans Milton
Posted 4 years ago

Try this version:

f[NotebookLocate@"Arguments"; First@NotebookRead@EvaluationNotebook[]]

1 2 3

It passes the single argument as a string to the function.

POSTED BY: Hans Milton
Posted 4 years ago

Thank you, that's a huge leap forward. How can I possibly make the following just print 1 2 3 (understand that I intend to replace the Print[] function with something else when this will be finalized).

Thank you.

enter image description here

Note: the cell with 1 2 3 is formatted as you mentioned above (read: style type text and cell argument type), that part seems to work I think.

POSTED BY: jerome ibanes
Posted 4 years ago

Your screenshot indicates that you have defined the "Arguments" cell to be an Input cell (or rather that it was made to be an Input cell when you copy/pasted it). But it needs to be defined as a Text cell. Since it is supposed to contain strings, not expressions that can be evaluated.

Cell type can be changed by selecting the cell and then rightclick:

enter image description here

POSTED BY: Hans Milton
Posted 4 years ago

enter image description here

POSTED BY: jerome ibanes
Posted 4 years ago

One possible approach is to use the option CellTags for the cell containing the string arguments. In this example the arguments are in a text cell with the tag Arguments. Default stylesheet.

enter image description here

In[1]:= f[a_, b_] := a^b

In[2]:= f[Sequence @@ ToExpression[NotebookLocate@"Arguments";First@NotebookRead@EvaluationNotebook[]]]
Out[2]= 243
POSTED BY: Hans Milton
Posted 4 years ago

Thanks, that helps already, now can "args = {"3", "5"};" be something like:

{"3","5"};

in a single cell, probably with a plain-text stylesheet, like if a portion of the notebook was to be considered not-to-be evaluated, but to serve as string arguments to a function?

POSTED BY: jerome ibanes
Posted 4 years ago

Maybe:

Arguments

args = {"3", "5"};

Function

f[a_, b_] := a^b

Evaluation

In[3]:= f[Sequence @@ ToExpression[args]]

Out[3]= 243
POSTED BY: Hans Milton
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract