10
$\begingroup$

( This has probably been asked before, but I found no Q/A's ) ( Update: Changed Title 28/6 )

Mathematica has the Style 'DisplayFormulaNumbered', I want to find / create something like 'TextNumbered' ( think of the Theorem environment in LaTeX ).

The reason for asking is that I want to write a Notebook containing several Theorems with Proofs, and I want to number the Theorem cells.

$\endgroup$
  • 2
    $\begingroup$ Check CurrentValue[{StyleDefinitions, "DisplayFormulaNumbered"}] and everything related to the Counter keyword. $\endgroup$ – Kuba Jun 27 at 10:33
  • $\begingroup$ OK, thanks. There might be something in Counter. $\endgroup$ – nilo de roock Jun 27 at 12:00
  • $\begingroup$ I found no such thing as a Counter keyword ( I am on $Version = 12. ). Are you suggesting to somehow copy the "DisplayFormulaNumbered" definition to a "TextNumbered" definition? - If so, how do I do that, and what should I change? $\endgroup$ – nilo de roock Jun 27 at 12:45
  • $\begingroup$ I meant things like CounterBox CounterIncrement etc. $\endgroup$ – Kuba Jun 27 at 12:59
  • $\begingroup$ Are you suggesting to somehow copy the "DisplayFormulaNumbered" definition to a "TextNumbered" definition? $\endgroup$ – nilo de roock Jun 28 at 9:22
7
$\begingroup$

I've done this sort of thing with the following three styles, one for an unnumbered theorem, one for a numbered theorem, and one for the continuation of a theorem (after, say, a displayed formula, for which I used a different style). The continuation style ensures the text aligns on the left. (The "Text" style has a left cell margin of 66 and the paragraph indenting adds 24 to that.) If all theorems (which for me included corollaries and so forth) are to be numbered, then the first two styles may be merged.

Cell[StyleData["Theorem", StyleDefinitions -> StyleData["Text"]],
 ParagraphIndent->-24,
 MenuSortingValue->10000,
 FontSlant->"Italic"]

Cell[StyleData["TheoremNumbered", StyleDefinitions -> StyleData["Theorem"]],
 CounterIncrements->"TheoremNumbered",
 MenuSortingValue->1000]

Cell[StyleData["TheoremContinued", StyleDefinitions -> StyleData["Theorem"]],
 CellMargins->{{90, 10}, {4, 8}},
 ParagraphIndent->0,
 MenuSortingValue->10000]

You have to type "Theorem" in the cell (and make it bold, if desired) but not the number. For the number, use the menu Insert > Automatic Numbering... and select "This counter object." To refer back to a numbered theorem, you'll need to give each theorem a cell tag. Then click on the desired insertion point and from the automatic numbering menu dialog, select the appropriate tag. Note that the number that appears in the "DisplayFormulaNumbered" style is one of the CellFrameLabels. That does not seem an appropriate place for a theorem number, so I settled for manual insertion.

Here is an example with the cell expressions following them. (I did not have to edit the cell expressions, but the different uses of CounterBox[] might be instructive.)

Mathematica graphics

Cell[TextData[{
 StyleBox["Theorem ",
  FontWeight->"Bold"],
 StyleBox[
 CounterBox["TheoremNumbered"],
  FontWeight->"Bold"],
 StyleBox[".",
  FontWeight->"Bold"],
 "  Any equation of the form...can be transformed in to an equation of the form (a parenthetical remark \
to show what happens when there is a line break)..."
}], "TheoremNumbered",
 CellTags->"ref:thm:normalform"]

Cell[BoxData[
 RowBox[{"\t\t", 
  FormBox[
   RowBox[{"x", " ", "=", " ", "4"}],
   TraditionalForm]}]], "DisplayFormulaNumbered"]

Cell["and perhaps other forms as well.", "TheoremContinued"]

Cell[TextData[{
 StyleBox["Theorem ",
  FontWeight->"Bold"],
 StyleBox[
 CounterBox["TheoremNumbered"],
  FontWeight->"Bold"],
 StyleBox[".",
  FontWeight->"Bold"],
 "  Any equation of the form...exist."
}], "TheoremNumbered",
 CellTags->"ref:thm:secondform"]

Cell[TextData[{
 "If we compare Theorem ",

 CounterBox["TheoremNumbered", "ref:thm:normalform"],
 " with Theorem ",

 CounterBox["TheoremNumbered", "ref:thm:secondform"],
 "...."
}], "Text"]
$\endgroup$
  • 2
    $\begingroup$ Worth mentioning that you can also set some CellGroupingRules on maybe the basic "Theorem" style and then set a DefaultNewCellStyle so that, e.g., every new cell will be a "TheoremContinued" if you are inside a "Theorem". You can also use the StyleKeyMapping option to map "Theorem" to "TheoremNumbered" if you press Tab or something. $\endgroup$ – b3m2a1 Jun 30 at 1:33
  • $\begingroup$ @b3m2a1 Thanks, that's good idea. $\endgroup$ – Michael E2 Jun 30 at 1:35
  • $\begingroup$ You could also put the "Theorem" and CounterBox in the CellDingbat or CellFrameLabels. Also possible to have it auto fill that by clever and targeted usage of CellDynamicExpression as a FE thing. $\endgroup$ – b3m2a1 Jun 30 at 17:59
  • $\begingroup$ @b3m2a1 Yes, I considered posting a "Homework" style that puts the CounterBox in the dingbat, but I didn't like that for theorems. However, it's a matter of taste and worth pointing out. I mentioned CellFrameLabels above; they seem to be centered, which seems an unusual place for numbering text. Above center might be the better of the four sides. I have a fear of too many dynamic expressions in stylesheets, but maybe such a fear is no longer well founded. Thanks again, though, you're helping me improve my own stylesheets. :) $\endgroup$ – Michael E2 Jun 30 at 18:28
  • $\begingroup$ Awesome answer, ( I knew there was a way in MMa ). Thank you very much. $\endgroup$ – nilo de roock Jul 1 at 6:48

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.