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

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?

Maybe:

Arguments

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

Function

f[a_, b_] := a^b

Evaluation

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

Out[3]= 243

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?

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.

Taggedcell.png

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

In[2]:= f[Sequence @@ ToExpression[NotebookLocate@"Arguments";First@NotebookRead@EvaluationNotebook[]]]
Out[2]= 243

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:

Freeform arguments 2.nb (3.27 KB)

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.

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.

Try this version:

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

1 2 3

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

This is perfect, thank you so much Hans!

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

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.

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

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.

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