0
$\begingroup$

Is there a way to split a Mathematica worksheet, so that I can define parameters that are valid only for a section of the worksheet, like "local parameters" of a Matlab or C++ function?

  1. I need a solution that does not require clearing the parameter each time and assigning them values again please.

  2. I don't want to worry which part of the code I write above or below another.

For a minimal example:

In this section I assign value for C and Q, which should be valid in this section only:

 C=1; 
 f[x_]:=Sin[x*C] 
 Q=NIntegrate[f[x],{x,0,1}] 

In this section I want the parameters to remain free, but not to have to clear them, or define a whole different set of parameter names. The problem with the this trivial solution is that I have too many parameters so it is simply very annoying to do so. -->:

 f[x_]:=Sin[x*C] 
 Q=Integrate[f[x],{x,0,1}]  

Thanks!

Update1: Maybe my question is more clear as follows:

Is there a way to split the Mathematica worksheet, such that different section of it will work on different kernels?

Update2: I am looking for something that works like

 Block[{C = 1}, Q = Exp[C]], 

but for more then one calculation, namely for a whole section of the worksheet.

$\endgroup$
0
$\begingroup$

One possibility is to add sections for grouping, and then change the CellContext option of input cells to CellGroup. Then all of the input cells in a section will use the context of the cell group, which will be different for each section. As an example, here is some code to create a notebook with multiple sub-sub-sections, with an input cell in each section, and with a style sheet that gives input cells the option CellContext->CellGroup:

CreateDocument[
    {
    TextCell["Subsection 1", "Subsubsection"], 
    ExpressionCell[Defer[x=1], "Input"],
    TextCell["Subsection 2", "Subsubsection"],
    ExpressionCell[Defer[x], "Input"]
    },
    StyleDefinitions->Notebook[
        {
        Cell[StyleData[StyleDefinitions->"Default.nb"]],
        Cell[StyleData["Input"],CellContext->CellGroup]
        },
        StyleDefinitions->"PrivateStylesheetFormatting.nb"
    ]
];

And here is an animation showing the evaluation of each input cell in the created notebook:

enter image description here

$\endgroup$
  • $\begingroup$ Good answer, thanks! $\endgroup$ – user1611107 Mar 20 '18 at 11:12

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.