Typically, the value of \count0
(aliased to \pageno
in plain TeX) is modified inside the output routine, whose commands are executed inside a group. Therefore the plain output routine has \advancepageno
which in turn is
\def\advancepageno{\ifnum\pageno<\z@ \global\advance\pageno\m@ne
\else\global\advance\pageno\@ne \fi} % increase |pageno|
as the convention of plain TeX is that negative values of \count0
will result in Roman numerals to be printed as page numbers. Indeed, the page number is printed by \folio
:
\def\folio{\ifnum\pageno<\z@ \romannumeral-\pageno \else\number\pageno \fi}
Complete quotation:
Output routines are always protected by enclosing them
in groups, so that they do not inadvertently mess up the rest of TeX; but
the change to \count0
would disappear if it were kept local to the
output group. The command
\global\advance\count0 by 1
solves the problem; it increases \count0
and makes this value stick around
at the end of the output routine. In general, \global
makes the immediately
following definition pertain to all existing groups, not just to the
innermost one.
This is a specific feature of \count0
and you shouldn't “export” it to other counters. Of course, if you change \count0
at the top level, you don't need \global
.
When you do \showthe\count1
, you're presented the current value of the counter:
{% open a group
\count1=1
\showthe\count1 % << 1
\global\count1=2
{
\showthe\count1 % << 2
\count1=3
\showthe\count1 % << 3
\global\count1=4
\showthe\count1 % << 4
\count1=5
\showthe\count1 % << 5
}
\showthe\count1 % << 4
\count1=6
\showthe\count1 % << 6
}
\showthe\count1 % << 4
You may want to better see what's going on by running pdftex
that has \tracingassigns
on
\tracingrestores=1
\tracingassigns=1
{% group level 1
\count1=1
\immediate\write20{\the\count1} % << 1
\global\count1=2
{% group level 2
\immediate\write20{\the\count1} % << 2
\count1=3
\immediate\write20{\the\count1} % << 3
\global\count1=4
\immediate\write20{\the\count1} % << 4
\count1=5
\immediate\write20{\the\count1} % << 5
}% group level 1
\immediate\write20{\the\count1} % << 4
\count1=6
\immediate\write20{\the\count1} % << 6
}% group level 0
\immediate\write20{\the\count1} % << 4
\end
The log file will be
This is pdfTeX, Version 3.141592653-2.6-1.40.25 (TeX Live 2023) (preloaded format=pdftex 2023.9.21) 8 NOV 2023 09:17
entering extended mode
restricted \write18 enabled.
%&-line parsing enabled.
**counters
(./counters.tex{into \tracingassigns=1}
{changing \count1=0}
{into \count1=1}
1
{globally changing \count1=1}
{into \count1=2}
2
{changing \count1=2}
{into \count1=3}
3
{globally changing \count1=3}
{into \count1=4}
4
{changing \count1=4}
{into \count1=5}
5
{restoring \count1=4}
{retaining \count1=4}
4
{changing \count1=4}
{into \count1=6}
6
{restoring \count1=4}
{retaining \count1=4}
4
)
No pages of output.
PDF statistics:
0 PDF objects out of 1000 (max. 8388607)
0 named destinations out of 1000 (max. 500000)
1 words of extra memory for PDF output out of 10000 (max. 10000000)
and the console will print
1
2
3
4
5
4
6
4
as predicted in the comments to the commands.