1
$\begingroup$

If have a function t[x_] which is used extensively in a long notebook, but which has a variety of definitions, and which is used in a variety of ways. I'm looking for a way to assign my chosen definition across the entire notebook, from a drop-down menu in the first cell.

For example, say I wish to assign t[x_] one of the following definitions:

t[x_]:=ArcTan[x]
t[x_]:=Tanh[x]^k
t[x_]:=Erf[x]/Pi

And say that somewhere further down the notebook, there is a cell that reads:

func[n]=Sum[t[n - c^b], {b, 1, c}]

I want it (indeed, all cells with the function t in them) to change depending on my choice in the notebook's first cell, so that func[n]=Sum[t[n - c^b], {b, 1, c}] then reads one of

func[n]=Sum[ArcTan[n - c^b], {b, 1, c}]
func[n]=Sum[Tanh[n - c^b]^k, {b, 1, c}]
func[n]=Sum[Erf[n - c^b]/Pi, {b, 1, c}]

Note that there are several pages of such cells. I don't want the cells to be evaluated, because a lot of them are just notes of algebraic workings. I just want the cells as written to change in response to the choice I made at the top - and I want it to include changes of variable such as x -> (n - c^b).

Is this possible? How would I do it?

UPDATE:

There are of course lots of cells that contain evaluatable instructions - Table, Plot, etc. I don't want these to evaluate immediately as it could take forever. Presumably, if the content of the cell to be evaluated changes, then the 'result' cell will become greyed out. That's fine because it will indicate to me that the cell awaits evaluation.

$\endgroup$

1 Answer 1

2
$\begingroup$

I'd put it in DockedCells, though:

ActionMenu["Define f", {
  Sin :> (ClearAll[f]; f[x_] := Sin[x]),
  Cos :> (ClearAll[f]; f[x_] := Cos[x])}]

E.g.:

SetOptions[EvaluationNotebook[], 
 DockedCells -> {Cell[
    BoxData@MakeBoxes[
      Row[{ActionMenu[
         "Define t", {
          Sin :> (ClearAll[t]; t[x_] := Sin[x]), 
          Cos :> (ClearAll[t]; t[x_] := Cos[x])}],
        " := ", 
        Style[Dynamic@t[HoldForm@x], "Input"]}],
      StandardForm], 
    "DockedCell"]}
 ]
$\endgroup$
4
  • $\begingroup$ Hi @Michael E2. Thanks for this. It creates the drop-down (and DockedCells is really handy, thank you!), but it still doesn't actually change the entry in the other cells in the notebook from t[x] to (say) Sin[x]... Maybe that's not possible? $\endgroup$ Aug 9, 2019 at 8:26
  • $\begingroup$ @RichardBurke-Ward Ah, I didn't understand that was what you wanted. I thought once you changed it, all the t[x]'s would be gone and you couldn't change it again. That didn't make sense to me. Do you want other t-function expressions to change, too, such as t[0] or t[y]? What about t itself: should it change or are there none to worry about? Have you considered using the find and replace box to replace all instances of t[x]? In any case I've done something like it to generate individualized random-coefficient problem sets, so I'm pretty sure you can change all the t[x] instances once. $\endgroup$
    – Michael E2
    Aug 9, 2019 at 11:09
  • $\begingroup$ Might a notebook template object be useful to you in this context? (My randomly-generated notebook code predates NotebookTemplate[].) $\endgroup$
    – Michael E2
    Aug 9, 2019 at 11:28
  • $\begingroup$ I'll give it a go. In the meantime, I'll mark this as answered. Much appreciated. $\endgroup$ Aug 11, 2019 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.