# How to keep symbol bindings local to a Notebook section?

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)

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:

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

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]

• 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? – Sylvain Leroux Dec 12 '19 at 3:15
• You can group cells by making sections, subsections etc – Mike Honeychurch Dec 12 '19 at 5:03
• Ah, OK. I though that was a different concept. – Sylvain Leroux Dec 12 '19 at 11:16
• 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. – Sylvain Leroux Dec 12 '19 at 11:46

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.