0

As on page 21 of the book The TeXbook, if we need to change the page number to add 1, we need to use \global\advance\count0 by 1, as \advance\count0 by 1 couldn't change \count0 in other groups. However,

hello
\advance\count0 by 1
\bye % end the document

enter image description here

can still change page number to be +1, which is 2 now. So, how should I understand the \global here, as whether there is \global, the page number is always 2. In what condition, without \global, we can't change the page number?

This question is about another one, on page 21 of that book, the exercise 5.6 says:

If you think you understand local and global definitions, here’s a little test to make sure: Suppose \c stands for ‘\count1=’, \g stands for ‘\global\count1=’, and \s stands for ‘\showthe\count1’. What values will be shown?

{\c1\s\g2{\s\c3\s\g4\s\c5\s}\s\c6\s}\s

The result is {1 {2 3 4 5}4 6} 4. Why?


\def\c{\count1=}
\def\g{\global\count1=}
\def\s{\showthe\count1}
{\c1\s\g2{\s\c3\s\g4\s\c5\s}\s\c6\s}\s


\bye% end the document

Why isn't this code right?

1
  • 2
    You are at the top level ...
    – Joseph Wright
    Commented Nov 8, 2023 at 7:29

2 Answers 2

4

In your first part of this question, you're changing the value of \count0 at the top level (you're not inside of any group -- or if you prefer to think of it that way, you're at group level 0), so in effect this has the same result as if you'd done \global, since there is no wider scope, hence your page number is indeed changed.

In the second part of the question, I hope the following helps understandig it:

{% start of group level 1
    \c1 % local value 1
    \s  % shows 1
    \g2 % local and global value 2
    {% start of group level 2
        \s  % shows 2
        \c3 % local value 3
        \s  % shows 3
        \g4 % local and global value 4
        \s  % shows 4
        \c5 % local value 5
        \s  % shows 5
    }% end of group level 2, restores to last active value valid above level 2 -> global 4
    \s  % shows 4
    \c6 % local value 6
    \s  % shows 6
}% end of group level 1, restores to last active value valid above level 1 -> global 4
\s % shows 4

With global meaning it changes the value at this group-level and every higher group-level, so global in group level 2 changes the value in that group, and group level 1, and group level 0, hence globally.

6
  • May you help check my updated queation? I want to test it. Thanks.
    – Y. zeng
    Commented Nov 8, 2023 at 7:49
  • 1
    @Y.zeng please refrain from editing your question invalidating answers given to you. Instead ask a new question if you got a new question. In this case however, there is no new question, your code does what it should be doing, it just doesn't print the result in the document, \showthe shows the current value of the counter on the terminal and in the log, not in the resulting DVI/PDF/whatever output format, and there the correct values are displayed (1, 2, 3, 4, 5, 4, 6, 4).
    – Skillmon
    Commented Nov 8, 2023 at 8:40
  • Change \showthe to \the, and the result is 2432432. If not change, the log file is empty and the content in terminal is > 1. <to be read again> \global \g ->\global \count 1= l.4 {\c1\s\g 2{\s\c3\s\g4\s\c5\s}\s\c6\s}\s ?
    – Y. zeng
    Commented Nov 8, 2023 at 8:44
  • @Y.zeng if you change \showthe to \the the thing becomes expandable, and \c6\s becomes \count1=6\the\count1 with \count1 having the value 4 it becomes \count1=64 (for instance). Instead do \def\s{\relax\the\count1 } and you should be fine.
    – Skillmon
    Commented Nov 8, 2023 at 8:59
  • Thanks. Why \the will make it expandable and why need \relax here and what is the function of \relax?
    – Y. zeng
    Commented Nov 8, 2023 at 9:02
4

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.

2
  • +1 Great answer! Is the old TeX Book still relevant with all the new activity of the LaTeX team? Commented Nov 8, 2023 at 8:31
  • 2
    @Dr.ManuelKuehner Of course it is!
    – egreg
    Commented Nov 8, 2023 at 9:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .