1

This question and the comments and answers prompted this question. How do I effectively use Cells and CellGroups in mathematica? I've always only programmed systematically inside the input cells, entering the next line in a new cell after evaluating the previous. Looking around at the different options available, this seemed inefficient.

How do I use these more effectively? I tried organizing my code into sections using command-5 and into subsections, etc. But then when I try to get back to input cell with command-9, it doesn't evaluate it. I'm sure I'm doing something wrong, so help is appreciated.

Community
  • 1
  • 1
  • I use cell groups to organize my code however I see fit. Are you asking for advice on organizing? Or how groups themselves work? Or how they relate to local context? This is not clear to me. – Mr.Wizard May 05 '11 at 15:08
  • I've provided some more context in my comment to your answer. I think addressing 1) how they work and 2) how they relate to context will help. I guess organizing code using it is subjective. –  May 05 '11 at 15:13
  • I've got to leave, but I expect that someone will give a good answer soon; otherwise, I'll post tomorrow. – Mr.Wizard May 05 '11 at 15:20
  • What do you mean with "when I try to get back to input cell with command-9"? – Sjoerd C. de Vries May 05 '11 at 16:26
  • I presume you know that `Option-click` (Mac) or `Alt-click` (PC) on a cell bracket **selects all cells of the same type**. Option click on a text cell selects all text cell brackets, for example. – 681234 May 05 '11 at 19:39
  • 1
    One common problem is to press Cmd+9 (or Cmd+n for any style) while already editing a cell of a different style (i.e., when the text cursor is a normal, vertical flashing cursor). This changes style mid-cell but that's not what you want, and it won't let you evaluate Input that you type. Instead you want to press Down, or click between cells, to get a *horizontal* cursor. Then you when you press Cmd+9 (or Cmd+n) you create a new cell. – Andrew Moylan May 06 '11 at 01:11
  • Please see my updated answer, and tell me of anything that remains unanswered. – Mr.Wizard May 06 '11 at 07:13

2 Answers2

0

Cell groups are just that, a way to group cells together. These groups can be easily selected, executed as one, and opened/closed (collapsed). By default these groups are indicated by a series of blue brackets to the right of the cells:

enter image description here

As seen above, output cells are automatically grouped with the input cell that created them. Also, the Section cell automatically groups the Text, Input, and Output cells below it. Finally the Title cell groups all cells below it, including the Section cell.

The different classes of cells that are available, such as Title, Section, etc., are determined by the active Stylesheet. The way they group is controlled by the option CellGroupingRules. This is a more advanced area, and details are probably better addressed in a different question.

Cell Grouping can be done either automatically, the default, or manually, or to a limited extent by a combination thereof. This is all handled by the menu commands in Cell > Grouping > ... or the keyboard shortcuts listed there.

If using Automatic Grouping, then styling a cell in a style that groups (as controlled by CellGroupingRules) will automatically group "lesser" cells and cell groups beneath it.


To style a cell, select the entire cell, not just the cell contents, by either:

  • clicking on the blue cell bracket (or the area where it would be if it is hidden)

  • clicking within the cell and using Alt+. until the entire cell is selected

  • clicking and dragging from above the cell, where the cursor is horizontal, to below the cell

Then, use menu Format > Style > ..., or the corresponding key combinations.

The same methods can be used to select cell groups, rather than individual cells.


To create a new cell of a particular style:

  1. click outside of existing cells where the cursor is horizontal

  2. this should create a horizontal rule as shown in earlier illustration

  3. specify a style, again with Format > Style > ... or keyboard shortcuts

  4. start typing (or paste) the contents of the new cell

To create a new Input cell, the default style, simply do step (1) and then type or paste the input.

Community
  • 1
  • 1
Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
  • correct I can do that, but what does a cell group mean? is the input cell now different from the previous cell? if I enter in an input cell and then click under it and start a new cell, does that have a new context as per your question? –  May 05 '11 at 15:03
  • @d'o-o'b I did not realize your question was about $Context as well. If you set the menu option that I showed in my question, then each group will have its own context, but I never use this myself, as I never want *every* group to have its own context. – Mr.Wizard May 05 '11 at 15:05
  • @Mr. Wizard: It's not really about contexts... I'm just interested in knowing how to use different cell groups. Your question was interesting because often I've had to write different variables for each cell just so as to not have a conflict with each other. it'll be greatly helpful if i can get them to be different in each cellgroup. So the answer to your question will help me with that. This was more to get an understanding of what constitutes a group. Is it the bunch of cells covered by the longest blue bracket to the right? –  May 05 '11 at 15:12
  • @d'o-o'b "Is it the bunch of cells covered by the longest blue bracket to the right?" Yes. :-) – Mr.Wizard May 05 '11 at 15:18
  • @d'o-o'b Thanks for the accept. Do you have any more questions about this? – Mr.Wizard May 07 '11 at 06:39
  • @Mr. Wizard: I think I know enough for my needs now. I'll post a comment here, if I have more questions. Thanks for the help :) –  May 07 '11 at 06:41
0

It depends on your stylesheet. Input is not the nineth numbered style in every stylesheet. Also, you have to select the cell bracket to change a cell's style.

I use sections particularly to be able to use folding, i.e. the double clicking on the cell group bracket to hide the lower ranking cells. That makes for a clean document. It doesn't do anything with the Input cells (except for the context option).

Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94