5.3 Datos de página dinámicos - números de página y marcas
LaTEX’s output routine, which produces the typeset pages, works asynchronously. That is, LaTEX assembles and prepares enough material to be sure that a page can be filled and then builds that page, usually leaving some residual material behind to be used on the next page(s). Thus, while preparing headings, paragraphs, and other page elements, it is usually not known on which page this material will eventually be placed because LaTEX might decide that this material does not fit on the current page. (We have already discussed this problem in the section about page-wise footnote numbering.) When the page is finally typeset, we might want to repeat some information from its contents in the running header or footer (e.g., the current section head) to give the reader extra guidance. You cannot save this information in commands when the material is collected; during this phase LaTEX often reads too far ahead, and your command would then contain data not appearing on that page. LaTEX solves this problem by providing a mark mechanism through which you can identify data as being of interest for the assembled page. In the output routine all marks from the page are collected, and the first and the last mark are made available. The detailed mechanism is explained in this section together with some useful extension packages.
5.3.1 Números de página de LaTeX
The page number is controlled through a counter named page. This counter is automatically stepped by LaTEX whenever it has finished a page— that is, after it has been used. Thus, it has to be initialized to 1, whereas most other LaTEX counters require an initialization to 0 because they are stepped just before they get used.
The command to access the typographical representation of the page number is , following standard LaTEX convention. There is, however, another subtle difference compared to other LaTEX counters: the command is not defined by the LaTEX kernel but instead comes into existence only after the first execution of a declaration, which typically happens in the document class file.
The best (though perhaps not the most convenient) way to get at the page number for the current page in the middle of the text is via a combination of the commands and , which should be put directly one following the other so that no page break can interfere.
5.3.2 lastpage - Una forma de referenciarlo
For a long time standard LaTEX had no way to refer to the number of pages in a document; that is, you could not write “this document consists of 6 pages” or generate “page 5 of 10” without manually counting the pages yourself. To resolve this limitation, Jeffrey Goldberg developed the package lastpage now maintained by Martin Münch, which works by automatically generating a label with the name LastPage on the last page so that you can refer to its page number via . Example 5- 4-5 on page 400 demonstrates its use.
The string produced by that call to is the content of as it would appear on the last page. If your document restarts page numbering midway through — for example, when the front matter has its own numbering — this string does not reflect the absolute number of pages.
The package works by generating the label within the hook, making sure that any pending floats are placed first. However, because this hook might also be used by other packages to place textual material at the end of the document, there is a chance that the label may be placed too early. In that case you can try to load lastpage after the package that generates this extra material.
In such cases or if you have more elaborate formatting needs for the page numbers produced, you might instead want to take a look at the pageslts package by Martin Münch that also provides a LastPage label. It is more complex to use, but has the advantage that it can handle documents with multiple page numbering schemes and offers a few other goodies.
Alternatively, assuming you have a current LaTEX distribution, you can simply use LaTEX’s hook mechanism. The declaration
5.3.3 chappg - Números de página por capítulos
For some publications it is required to restart numbering with every chapter and to display the page number together with the chapter number on each page. This can already be done with the commands at our disposal by simply putting
5.3.4 Comandos de marcas heredados de LaTeX
The TEX primitive , which you may encounter inside package code dealing with page layout or output routines, is ultimately responsible for associating some text (its argument) with a position on a page (i.e., the position where the is executed). When producing the final page, TEX makes the first mark on the assembled page available in , the last in , and the from the previous page as . If there are no marks on that page, then and also inherit the value of the previous . Thus, if each heading command internally issues a with the heading text as its argument, then one can display the first or last heading text on a page in the running header or footer by using these commands.
However, it is not possible to use these commands directly in LaTEX, because LaTEX uses a higher-level protocol to control marks, so please do not try this. We mention them here only to explain the underlying general mechanism.1 LaTEX effectively structures the content of the argument so that the direct use of this command most likely results in strange error messages.
As a replacement for the command, standard LaTEX always offered the following two commands to generate marks:
and
.
5.3.5 El nuevo mecanismo de marcas de LaTeX
As we have seen so far, LaTEX’s mark mechanism was built with a certain layout in mind and is, therefore, only partially usable for other applications. As a result a number of attempts have been made to extend or replace it with code that supports more complex marking mechanisms.
Part of the limitation is inherent in TEX itself, which provides only one class of marks and thus makes different independent marks difficult (though not impossible) to implement. This issue is resolved in eTEX, which provides independent mark classes. A second problem is that LaTEX sometimes invokes the output routine (for example, to handle a float or a marginpar) without actually producing a page. This changes the internal state of the mark mechanism, and except for and , the LaTEX kernel does not account for that.
With the LaTEX release of 2022 these restrictions are resolved through a new mark mechanism that provides fully independent marks including the possibility of using TEX’s concept of top marks, if desired. It does not alter the existing mechanism so that , etc., can still be used.