Explanation
Some of questions have come up about how CounterBox["Figure"]
and CounterBox["Figure", tag]
work in the comments under the OP. This box seem to be an approach that can solve the problem at hand (see also @Carl Woll's answer), so instead of a potentially long exchange of comments, which have to be short, I thought an answer that can be edited would be more appropriate. I also have a correction to make to my comments.
First, I don't know where there is much documentation on CounterBox[]
thanks to @BenIzd, there is this webpage that introduces CounterBox[]
in V3.0.2; it seems to be an HTML version of this Wolfram Library tech note. This answer is based on what I inferred by examining cell expressions of the following:
A cell after doing the menu command Insert > AutomaticNumber... of tutorial that numbered things.
Styles like "ItemNumbered"
and "DisplayFormulaNumbered"
in stylesheets like Default.nb (find it with FileNameJoin[{$InstallationDirectory, "SystemFiles", "FrontEnd", "StyleSheets", "Default.nb"}]
).
Tutorials that numbered things.
CounterBox[counter]
— for numbering cells
The one-argument version is typeset by the Front End to show the number of cells with style counter
from the beginning of a "unit" (my term, see below) up to and including the current cell, provided the style is counted. (This is the correction I needed to make; see Counter versus style below.) It is generally used to label a cell with a number. If you want to have as style that is sometimes numbered and sometimes not, it is better to have two styles like "DisplayFormula"
and "DisplayFormulaNumbered"
, with the numbered style inheriting from the unnumbered one.
CounterBox[counter, tag]
— for referring to numbered cells
The two-argument version is typeset by the Front End to show the value of CounterBox[counter]
in the (first) cell tagged tag
. The menu command Insert > Automatic Numbering... presents a dialog for inserting a CounterBox[]
. You have to select the style, which it and what documentation there is calls a "Counter". The style/counter will be automatically initialized with the style of the cell the cursor is in. And if you also select a tag from the list of tagged cells, it will insert the corresponding two-argument form. If you type a tag that does not exist in the notebook, the CounterBox[]
will be displayed as "XXX".
Options (including "unit")
One can explore the symbols returned by Names["Counter*"]
. All but one relate to CounterBox
. Some are Cell
options and some are CounterBox
options. Some have usage messages, some have documentation pages, and some have neither. Two are worth mentioning, meaning only that I have something meaningful and seemingly accurate to say about them. See the next section for something about CounterIncrements
. The other option CounterAssignments
controls what I called the "unit". This option causes the cell to reset the counters to the values indicated. Here is an example of the option value from the default stylesheet for the styles "Title"
and "Section"
:
CurrentValue[{StyleDefinitions, "Title", CounterAssignments}]
(*
{{"Section", 0}, {"Equation", 0}, {"Figure", 0},
{"Subtitle", 0}, {"Subsubtitle", 0}, {"Item", 0},
{"Subitem", 0}, {"Subsubitem", 0}, {"ItemNumbered", 0},
{"SubitemNumbered", 0}, {"SubsubitemNumbered", 0}}
*)
CurrentValue[{StyleDefinitions, "Section", CounterAssignments}]
(*
{{"Subsection", 0}, {"Subsubsection", 0}, {"Item", 0},
{"Subitem", 0}, {"Subsubitem", 0}, {"ItemNumbered", 0},
{"SubitemNumbered", 0}, {"SubsubitemNumbered", 0}}
*)
One can see that, assuming there is just one cell of style "Title"
at the beginning of the notebook, that "Equation"
and "Figure"
are numbered sequentially through the whole notebook. One can also see that the "Section"
counter is reset by "Title"
but not the "Subsection"
counter. So there is an assumption about the document structure build into the stylesheet.
"Counter" versus style
When I initially made my comments, it had been long time since I had really looked at CounterBox[]
. I had in mind that the term "Counter" used in Mathematica is interchangeable with "style." That is not strictly true. The CounterBox[counter]
counts the cells whose CounterIncrements
option value is counter
. This is usually set in a stylesheet. In the default stylesheet, most but not all styles have CounterIncrements
set to the name of the style. A typical example of not are the styles that have a numbered variant:
CurrentValue[{StyleDefinitions, "DisplayFormula",
CounterIncrements}]
CurrentValue[{StyleDefinitions, "DisplayFormulaNumbered",
CounterIncrements}]
(*
{}
"DisplayFormulaNumbered"
*)
I believe the Cell
option CounterIncrements -> {counter,...}
takes a list of counters, where each counter
is a string. I would guess that multiple cell styles can increment the same counter and that a single cell can increment multiple counters.
Example application to OP's task
The following is modified from a comment by the OP:
makeFig3[expr_, tag_String] := (
CellPrint@Cell[BoxData[
ToBoxes[expr]], "Figure",
CounterIncrements -> {"Figure"}, (* N.B. *)
CellTags -> "Fig" <> tag]; (* N.B. *)
CellPrint@Cell[TextData[
{StyleBox["Fig. ", FontWeight -> "Bold"],
StyleBox[CounterBox["Figure"], FontWeight -> "Bold"],
StyleBox[".", FontWeight -> "Bold"]}],
"FigureCaption"]); (* Note no CellTag needed *)
Let's make two figures:
makeFig3[Plot[Sin[x], {x, 0, 2 Pi}], "PlotSin"]
makeFig3[Plot[Cos[x], {x, 0, 2 Pi}], "PlotCos"]
Now let's discuss them in a "Text"
cell, which was created by hand. A question about the use of cell tags was raised in a comment. The cell tags created by makeFig3[]
are used here for reference. This text cell needs no tag, at least not for the purpose of referring to the figures. Likewise the caption cell created by makeFig3[]
needs no tag.
CellPrint@Cell[TextData[
{"Compare ",
StyleBox["Fig. ", FontWeight -> "Bold"],
StyleBox[CounterBox["Figure", "FigPlotSin"], (* Note tag *)
FontWeight -> "Bold"], StyleBox[".", FontWeight -> "Bold"],
" with ",
StyleBox["Fig. ", FontWeight -> "Bold"],
StyleBox[CounterBox["Figure", "FigPlotCos"], (* Note tag *)
FontWeight -> "Bold"], StyleBox[".", FontWeight -> "Bold"],
" "}],
"Text", CellTags -> "foo"]
Now let's look at the outputs (I don't have styles "Figure"
or "FigureCaption"
, so they display with no cell margins in whatever default styling there is for undefined styles; the "Text"
cell gets indented, not centered):
"FigureCaption"
style, it is just a cell without anyBox
, likeCellPrint[Cell["Fig 1", {"FigureCaption"}]]
which solves your problem, is there any reason you chose your code? $\endgroup$$FigureIndex=0
solve the problem? (like... TextCell["Fig. " <> ToString[$FigureIndex += 1] ...
) or it should be in a specific order other than execution? $\endgroup$$FigureIndex
if I want to change something in a figure and evaluate it once more it will increase its number. Moreover, this approach will make it larger than the maximum value of$FigureIndex=0
in the moment I make the reevaluation of the figure. I would better like to make the numeration on the vases of the cell tags. $\endgroup$CounterBox["Figure"]
orCounterBox["Figure", celltag]
. Something likeCellPrint@Cell[TextData[{StyleBox["Fig. ", FontWeight -> "Bold"], StyleBox[CounterBox["Figure"], FontWeight -> "Bold"], StyleBox[".", FontWeight -> "Bold"], " ", "A caption."}], "FigureCaption"]
for the caption. You can refer to the counter in the cell through its tag:CellPrint@Cell[TextData[{"In figure ", CounterBox["Figure", "fig:myfirst"], " we see..."}], "Text"]
. See alsoCounterIncrements
etc. I used it in stylesheets and editing, not in programming. I think it might work, though. $\endgroup$