0
$\begingroup$

I'm using the Notebook on Wolfram-Cloud as a ... well ... notebook to keep notes about the topic I'm studying and to answer exercises. I usually keep one notebook file per chapter, with course notes on the top, and exercises solution on the bottom (see the screen capture below)

Notebok screen capture

This sometimes cause me troubles when a symbol is bound to some value in one exercise, then the same symbol name is re-used in a different exercise. I could Clear all symbols used at the start of each exercise section. But it seems quite cumbersome.

What are the best practices/solutions to ensure definitions and symbol bindings to stay local to a given section?

More formally, I was looking for a way to keep bindings lexically scoped to sections (and its subsections)


I tried Module but it does not allow the module content to be broken in different notebook parts: enter image description here

$\endgroup$
  • 1
    $\begingroup$ Use a cell context that is specific to each cell group? $\endgroup$ – Mike Honeychurch Dec 11 '19 at 23:20
  • $\begingroup$ Thanks @Mike. Well, I never heard about "cell context" nor "cell groups". You gave me new search terms to explore the Wolfram Langage documentation! $\endgroup$ – Sylvain Leroux Dec 11 '19 at 23:23
  • $\begingroup$ @Mike indeed, it looks promising. But at first sight, the doc is sparse, to say the least: reference.wolfram.com/language/ref/CellContext.html $\endgroup$ – Sylvain Leroux Dec 11 '19 at 23:26
3
$\begingroup$

You could try implementing one of these options -- whatever works best for you:

SetOptions[EvaluationNotebook[], CellContext -> CellGroup]

SetOptions[EvaluationNotebook[], CellContext -> Cell]

other possibilities, less likely based on your requirements:

SetOptions[EvaluationNotebook[], CellContext -> Notebook]

SetOptions[EvaluationNotebook[], CellContext -> Style]
$\endgroup$
  • $\begingroup$ Thanks, @Mike. I saw something like that while searching for a solution. But I didn't manage to group cells, nor use stylesheets from Wolfram Cloud. All the docs seem to refer to the Desktop Notebook. Or did I missed something? $\endgroup$ – Sylvain Leroux Dec 12 '19 at 3:15
  • $\begingroup$ You can group cells by making sections, subsections etc $\endgroup$ – Mike Honeychurch Dec 12 '19 at 5:03
  • $\begingroup$ Ah, OK. I though that was a different concept. $\endgroup$ – Sylvain Leroux Dec 12 '19 at 11:16
  • $\begingroup$ Actually @Mike, I was unable to make that work. Probably by my lack of understanding where I'm supposed to change the CellContext. Since this is somewhat at the margin of my initial question, I asked a new question for that specific issue. $\endgroup$ – Sylvain Leroux Dec 12 '19 at 11:46
2
$\begingroup$

I wasn't able to find how to use CellContext and cell groups in the Wolfram Cloud Notebook. As a workaround, I create a new context for each exercise using Begin and End:

Ex 3.4

 Begin["Ex3x4`"]

<<< mix of text cells and computation here >>>

 While[$Context != "Global`", End[]]

I wrap the End[] call in a While loop to ensure I restore the Global context, since, in Notebook interactive usage, the cell containing Begin may end-up being evaluated more than one.

enter image description here

$\endgroup$

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.