Debian Codegraph @

texmf-dist/source/latex/totpages/totpages.dtx

raw
1 % \iffalse ^^A -*- mode: latex; -*-
2 \NeedsTeXFormat{LaTeX2e}[1995/12/01]
3 %<*driver>
4 \ProvidesFile{totpages.drv}
5 [2005/09/19 v2.00 Driver for Totpages Package (muewi)]
6 \documentclass{ltxdoc}
7 \usepackage{url}
8 \usepackage{totpages}
9 \usepackage[toc]{multitoc}
10 \GetFileInfo{totpages.sty}
11 \EnableCrossrefs
12 \CodelineIndex % Index code by line number
13 \RecordChanges % Changelog to .gls
14 %\OnlyDescription % comment out for implementation details
15 \setcounter{IndexColumns}{2}
16 %\OldMakeIndex % use if your MakeIndex is pre-v2.9
17 \begin{document}
18 \setcounter{page}{1234}
19 \DocInput{totpages.dtx}
20 \end{document}
21 %</driver>
22 %
23 % Copyright (c) [1999-2005] by Wilhelm M\"uller. All rights reserved.
24 %
25 % This program may be redistributed and/or modified under the terms
26 % of the LaTeX Project Public License, either version 1.1 of this
27 % license, or (at your option) any later version.
28 % The latest version of this license is in
29 % CTAN:macros/latex/base/lppl.txt.
30 %
31 % For error reports in case of UNCHANGED versions see totpages.ins
32 %
33 % \fi
34 %
35 % \CheckSum{286}
36 %
37 %% \CharacterTable
38 %% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
39 %% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
40 %% Digits \0\1\2\3\4\5\6\7\8\9
41 %% Exclamation \! Double quote \" Hash (number) \#
42 %% Dollar \$ Percent \% Ampersand \&
43 %% Acute accent \' Left paren \( Right paren \)
44 %% Asterisk \* Plus \+ Comma \,
45 %% Minus \- Point \. Solidus \/
46 %% Colon \: Semicolon \; Less than \<
47 %% Equals \= Greater than \> Question mark \?
48 %% Commercial at \@ Left bracket \[ Backslash \\
49 %% Right bracket \] Circumflex \^ Underscore \_
50 %% Grave accent \` Left brace \{ Vertical bar \|
51 %% Right brace \} Tilde \~}
52 %%
53 % \iffalse meta-comment
54 %% ===================================================================
55 %% @LaTeX-style-file{
56 %% author = {Wilhelm M\"uller},
57 %% version = "2.00",
58 %% date = "19 September 2005",
59 %% filename = "totpages.sty",
60 %% address = {Wilhelm M\"uller
61 %% Herbststra\ss{}e 66
62 %% D-28215 Bremen
63 %% telephone = "+49-421-1692525",
64 %% email = "[email protected]",
65 %% codetable = "ISO/ASCII",
66 %% keywords = "LaTeX, pages",
67 %% supported = "yes",
68 %% docstring = "LaTeX package which sets count1 to the total
69 %% number of pages in the document.
70 %% Uses the everyshi and keyval packages."
71 %%
72 %% }
73 %% ===================================================================
74 % \fi
75 %
76 % \newcommand*{\Count}[1]{\texttt{\bslash{}count#1}}
77 % \newcommand*{\package}[1]{\textsf{#1}}
78 % \newcommand*{\ftype}[1]{\textsl{#1}}
79 % \newcommand*{\NEWfeature}[1]{^^A stolen from ltxguide.cls
80 % \hskip 1sp \marginpar{\small\sffamily\raggedright
81 % New feature\\#1}}
82 %
83 % ^^A^^A We need to document a label -- we do this by using the macros for
84 % ^^A^^A environments and changing the macros for the index entries.
85 % \makeatletter
86 % \renewcommand*{\SpecialEnvIndex}[1]{^^A
87 % \@bsphack\index{\string#1^^A
88 % \actualchar\string\verb\quotechar*\verbatimchar
89 % \string#1\verbatimchar
90 % \encapchar usage}\@esphack}
91 % \renewcommand*{\SpecialMainEnvIndex}[1]{^^A
92 % \@bsphack\special@index{\string#1\actualchar
93 % \string\verb\quotechar*\verbatimchar
94 % \string#1\verbatimchar\encapchar main}%
95 % \@esphack}
96 % \makeatother
97 %
98 % \changes{v1.00}{1999/07/14}{Initial Version}
99 % \changes{v1.10}{2003/02/05}{Fixed hook into \package{hyperref}}
100 % \changes{v2.00}{2005/09/19}{
101 % Incorporated changes by Ido Tal to compute
102 % number of sheets needed;
103 % changed option processing to use keyval;
104 % corrected a problem when we wanted to write to \ftype{aux}
105 % although \texttt{\char`\\nofiles} was requested;
106 % moved \texttt{\char`\\StopEventually} to correct place
107 % }
108 %
109 % \DoNotIndex{\@empty,\@firstofone,\addtocounter,\arabic}
110 % \DoNotIndex{\begingroup,\else,\endgroup}
111 % \DoNotIndex{\fi,\ifx,\immediate,\let,\MessageBreak}
112 % \DoNotIndex{\ref,\relax,\string,\lowercase,\textlatin}
113 % \DoNotIndex{\the,\thepage,\undefined,\do,\divide,\value}
114 % \DoNotIndex{\ifHy@pageanchor,\if@filesw,\ifHy@hypertexnames}
115 % \DoNotIndex{\ifHy@plainpages,\Hy@temp,\Hy@pagecounter}
116 % \DoNotIndex{\end,\documentclass,\ldots,\pageref,\renewcommand}
117 % \DoNotIndex{\roman,\setcounter,\usepackage,\begin,\@number}
118 % \DoNotIndex{\@currname,\@currext,\noexpand,\gdef,\edef,\def}
119 %
120 % ^^A -----------------------------
121 %
122 % \title{\unskip
123 % The \package{totpages} package^^A
124 % \thanks{^^A
125 % The version number of this file is \fileversion,
126 % last revised \filedate.}
127 % }
128 % \author{Wilhelm M\"uller\\[0.5ex]
129 % \normalsize Herbststra\ss{}e 66\\
130 % \normalsize D-28215 Bremen\\
131 % \normalsize [email protected]
132 % }
133 % \date{\filedate}
134 % \maketitle
135 %
136 % ^^A -----------------------------
137 %
138 %
139 % \begin{abstract}
140 % \noindent This package counts the total number of pages shipped
141 % out to the \ftype{dvi} file, which
142 % allows sentences like
143 % ``This document contains \ref{TotPages} pages,
144 % the last one being no.~\pageref{TotPages}.'' to be produced
145 % automatically.
146 % The counter itself may be shipped out to the \ftype{dvi} file, too,
147 % allowing page access by absolute page numbers, or it may be just
148 % a regular \LaTeX\ counter.
149 %
150 % It cooperates with the package \package{hyperref}, producing
151 % a hyperlink to the last page---though this is not of much use as hypertext
152 % readers usually allow direct access to the last page anyway.
153 %
154 % Optionally, the number of sheets of paper needed to print the document
155 % can be computed.
156 % \end{abstract}
157 %
158 % \pagestyle{headings}
159 %
160 % ^^A -----------------------------
161 %
162 % \tableofcontents
163 %
164 % ^^A -----------------------------
165 %
166 % \section{Introduction}
167 % ^^A
168 % Printing only part of a \ftype{dvi} file can be difficult or
169 % impossible if pages in different
170 % parts of the document have the same number---e.\,g., in the
171 % front matter and the first text pages (iii vs. 3).
172 %
173 % \TeX{} provides an easy solution to this problem: whenever a page
174 % has been completed by the output routine and is being shipped out to
175 % the \ftype{dvi} file,
176 % it displays the values of \Count{0} to \Count{9},
177 % with trailing zeroes suppressed,
178 % (e.\,g., \texttt{[1002.0.3]}) and writes them to the \ftype{log} and
179 % \ftype{dvi} files, cf.\ \cite[p.~119]{KnuthTeXa}.
180 % So,
181 % if you have a \ftype{dvi} driver which allows page selection by other counters
182 % than \Count{0}, you will be able to refer to absolute page numbers
183 % when you make use of this package.
184 %
185 % \section{Using \package{totpages}}
186 % ^^A
187 % \DescribeEnv{TotPages}
188 % Keeping a count of the absolute page number makes it
189 % possible to provide the \emph{total} number of pages of the
190 % document (as opposed to the ``number'' on the last page provided
191 % by the \package{lastpage} package, cf.\ \cite{package:lastpage}).
192 %
193 % References to the counter itself won't make much sense since it is updated
194 % asynchronously whenever an actual shipout occurs.
195 % But by referencing the label |TotPages| (with |\ref{TotPages}|)
196 % you get the total number of pages the document had at the end
197 % of the previous run
198 % of \LaTeX. If, for some reason, you want to know what page number \LaTeX\
199 % actually (would have) printed, you may use |\pageref{TotPages}|.^^A
200 % \footnote{Since all references to this label are obviously forward
201 % references, you will have to run \LaTeX\ at least twice to get
202 % correct results.}
203 %
204 % \DescribeMacro{\theTotPages}
205 % What is actually printed by |\ref{TotPages}| is the result of the
206 % macro |\theTotPages|, which normally is simply |\arabic{TotPages}|, but
207 % may be redefined to anything (sensible)
208 % somewhere before |\end{document}|.
209 %
210 % If you want to use this package, you should load \package{totpages}
211 % as the \emph{last} package in your document because it
212 % executes some code at |\end{document}| and has to be sure that its
213 % code is the last code to be executed there.\footnote{See
214 % \cite{package:lastpage} for a discussion of problems.}
215 %
216 % You \emph{must not} use \package{count1to} or \package{lastpage}
217 % together with this package. Package \package{count1to} provides additional
218 % functionality, but will not cooperate with \package{hyperref}, whereas
219 % any use of \package{lastpage} is covered by \package{totpages}.^^A
220 % \footnote{\package{lastpage} requires less resources, though.}
221 % If you try to use one (or both) of these packages together with
222 % \package{totpages}, you will receive an error message at
223 % |\begin{document}|. I don't think that something really bad will happen
224 % if you ignore it (unless you use \package{count1to} together with
225 % \package{hyperref}), but I won't guarantee anything, either.
226 %
227 %
228 %
229 % ^^A -----------------------------
230 %
231 % \section{Options}
232 % \subsection{Writing absolute page numbers to the \ftype{dvi} file}
233 % \DescribeEnv{dvi}
234 % \DescribeEnv{nodvi}
235 % With option |dvi| (the default), \Count{1} is used for the absolute
236 % page counter. This value is put into the \ftype{dvi} file and may be
237 % used by the \ftype{dvi} driver.
238 %
239 % Option |nodvi| uses a regular \LaTeX\ counter which will not
240 % show up elsewhere.
241 %
242 % No matter what kind of counter is used, its name will always be |TotPages|,
243 % e.\,g., if you want to change its format to roman. (But who would want
244 % to do that?).
245 %
246 % ^^A\pagebreak[1]
247 % \subsection{Computing the number of sheets of paper}
248 % \DescribeEnv{pagespersheet}
249 % \DescribeEnv{nopagespersheet}
250 % The option |pagespersheet=|$n$ allows to compute the number of
251 % \NEWfeature{Provide number of sheets needed for printing}
252 % physical sheets of paper needed when the document will be
253 % printed.\footnote{Thanks
254 % to Ido Tal for suggesting this
255 % feature, providing an initial realisation, testing it, and
256 % proof-reading the documentation.}
257 % To compute this value, you have to specify the positive integer,
258 % $n$, telling how many pages will be
259 % printed on one physical sheet of paper. For example, when you
260 % use a duplex printer and let your printer driver scale and
261 % rearrange document pages to print two document pages
262 % on one physical page, you should specify
263 % |pagespersheet=4|. Using |nopagespersheet| (or |pagespersheet=no|)
264 % turns this computation off; this is the default setting.
265 %
266 % \DescribeMacro{\TotPerSheet}
267 % When |pagespersheet| is set to valid number,
268 % its value is available in the macro |\TotPerSheet| (in exactly the
269 % same form as you wrote it),
270 % otherwise this macro is undefined.
271 %
272 % \DescribeMacro{\TotSheets}
273 % The number of sheets, as computed at the end of the previous
274 % \LaTeX\ run, will be contained (as decimal
275 % digits) in the macro |\TotSheets|.\footnote{The value of
276 % |pagespersheet| must, of course, coincide with the options you use
277 % when you print the document.}
278 % When |pagespersheet| is
279 % used for the first time, the value of this macro will be set to |0000|
280 % (four zeroes), which is still a number but different from the
281 % representation of an actual zero. When you say |nopagespersheet|
282 % (or never use |pagespersheet| at all), |\TotSheets| will be undefined.
283 % When, at the end of the document, the value of |\TotSheets| differs
284 % from the one at |\begin{document}| (the initial |0000|, or as read
285 % from \ftype{aux}),
286 % a warning will be issued similar to the one you get when labels differ.
287 %
288 % ^^A -----------------------------
289 %
290 % \section{Required packages}
291 % ^^A
292 % This package needs the \package{everyshi}
293 % \cite{package:everyshi} and \package{keyval} \cite{package:keyval} packages.
294 %
295 %
296 % ^^A -----------------------------
297 %
298 % \section{Acknowledgements}
299 % ^^A
300 % This package uses ideas inspired by
301 % Martin Schr\"oder's \package{count1to}
302 % package \cite{package:count1to}, Jeff Goldberg's \package{lastpage}
303 % package \cite{package:lastpage}, and the \package{lastpage} and
304 % \package{keyval} support
305 % contained in Sebastian Rahtz's \package{hyperref}
306 % package \cite{package:hyperref}, and implements options suggested
307 % by and based on ideas of Ido Tal \cite{mail:Tal}.
308 %
309 %
310 % ^^A -----------------------------
311 %
312 % \begin{thebibliography}{HHHHHHHH\,1}
313 % \raggedright
314 % \bibitem[Carlisle]{package:keyval}
315 % David Carlisle.
316 % \newblock The \package{keyval} package.
317 % \newblock \url{CTAN: tex-archive/macros/latex/required/graphics/keyval.dtx}.
318 % \newblock \LaTeXe{} package.
319 % \bibitem[Goldberg]{package:lastpage}
320 % Jeff Goldberg.
321 % \newblock The \package{lastpage}-package.
322 % \newblock \url{CTAN: tex-archive/macros/latex/contrib/lastpage/}.
323 % \newblock \LaTeXe{} package.
324 % \bibitem[Knuth]{KnuthTeXa}
325 % Donald~E.\ Knuth.
326 % \newblock \emph{The {\TeX}Book}, volume~A of \emph{Computers
327 % and Typesetting}.
328 % \newblock Addison-Wes\-ley, 1986.
329 % \newblock Tenth printing, revised, June 1990.
330 % \bibitem[Rahtz]{package:hyperref}
331 % Sebastian Rahtz.
332 % \newblock Hypertext marks in \LaTeX.
333 % \newblock \url{CTAN: tex-archive/macros/latex/contrib/hyperref/}.
334 % \newblock \LaTeXe{} package.
335 % \bibitem[Schr\"oder\,1]{package:count1to}
336 % Martin Schr\"oder.
337 % \newblock The \package{count1to} package.
338 % \newblock \url{CTAN: tex-archive/macros/latex/contrib/ms/count1to.dtx}.
339 % \newblock \LaTeXe{} package.
340 % \bibitem[Schr\"oder\,2]{package:everyshi}
341 % Martin Schr\"oder.
342 % \newblock The \package{everyshi} package.
343 % \newblock \url{CTAN: tex-archive/macros/latex/contrib/ms/everyshi.dtx}.
344 % \newblock \LaTeXe{} package.
345 % \bibitem[Tal]{mail:Tal}
346 % Ido Tal.
347 % \newblock Private communication.
348 % \end{thebibliography}
349 %
350 %
351 %
352 % ^^A -----------------------------
353 %
354 % \StopEventually{}^^A
355 %
356 % ^^A -----------------------------
357 %
358 %
359 % \section{The implementation}
360 % ^^A
361 % \setlength{\parindent}{0pt}
362 % We need the \package{everyshi} and \package{keyval} packages.
363 % \begin{macrocode}
364 %<*package>
365 \ProvidesPackage{totpages}
366 [2005/09/19 v2.00 Totpages Package (muewi)]
367 \NeedsTeXFormat{LaTeX2e}[1995/12/01]
368 \RequirePackage{everyshi}[1994/12/09]
369 \RequirePackage{keyval}[1998/06/05]
370 % \end{macrocode}
371 %
372 % \begin{macro}{\ifTotPagesToDvi}
373 % The user can decide if the absolute page number should
374 % go to the \ftype{dvi} file or not, the default being yes:
375 % \begin{macrocode}
376 \newif\ifTotPagesToDvi\TotPagesToDvitrue
377 % \end{macrocode}
378 % \end{macro}
379 %
380 % \begin{macro}{\ifPagesPerSheet}
381 % \begin{macro}{\TotPerSheet}
382 % The user may ask to compute the number of sheets needed for
383 % printing.
384 % To do this, we need some variables. (The number
385 % of pages per sheet is saved in a macro instead of a counter
386 % since counters are a scarce resource in some \TeX\ implementations.)
387 %
388 % \begin{macrocode}
389 \newif\ifPagesPerSheet\PagesPerSheetfalse
390 \newcommand{\TotPerSheet}{}
391 % \end{macrocode}
392 % \end{macro}
393 % \end{macro}
394 %
395 % We want to parse package options with keyval. This code has been
396 % stolen (more or less) from \package{hyperref}:
397 %
398 % \SpecialMainEnvIndex{dvi}
399 % \SpecialMainEnvIndex{nodvi}
400 % First, we define the keys. In case of the boolean keys, |dvi| and
401 % |nodvi|, we ignore any value, just taking into account their presence:
402 %
403 % \begin{macrocode}
404 \define@key{Tot}{dvi}[x]{\TotPagesToDvitrue}
405 \define@key{Tot}{nodvi}[x]{\TotPagesToDvifalse}
406 % \end{macrocode}
407 %
408 % \SpecialMainEnvIndex{pagespersheet}
409 % \SpecialMainEnvIndex{nopagespersheet}
410 % For the numeric key, a value of |no| may be used
411 % to disable the feature.
412 %
413 % \begin{macrocode}
414 \define@key{Tot}{nopagespersheet}[x]{\PagesPerSheetfalse}
415 \define@key{Tot}{pagespersheet}{%
416 \lowercase{\def\Tot@temp{#1}}%
417 \ifx\Tot@temp\@empty
418 \else
419 \def\Tot@no{no}
420 \ifx\Tot@temp\Tot@no
421 \else
422 \edef\TotPerSheet{#1}
423 \PagesPerSheettrue
424 \fi
425 \fi
426 }
427 % \end{macrocode}
428 %
429 % \begin{macro}{\Tot@ptionsWithKV}
430 % Now comes the actual option processing. We won't use the code from \package{hyperref}
431 % literally since it will produce a warning when something like |pagespersheet=2|
432 % is specified as a global option (although it's processed correctly!). Nevertheless,
433 % this code has been derived from the code in \package{hyperref}, which, in turn,
434 % contains the following attribution: ``This section was written by David Carlisle.''
435 % \begin{macrocode}
436 \def\Tot@ptionsWithKV#1{%
437 \let\@tempc\relax
438 \let\Tot@tempa\@empty
439 % \end{macrocode}
440 % \begin{macro}{\Tot@tempopt}
441 % To cope with global options of the form |key=value|, we have to look for
442 % a definition of |key|, but remove |key=value| from the list of unprocessed
443 % options. \TeX's argument parser will help us here:
444 % \begin{macrocode}
445 \def\Tot@tempopt##1=##2=##3\Tot@tempopt{%
446 % \end{macrocode}
447 % |\Tot@tempopt| should be called with an option string followed by |==\Tot@tempopt|.
448 % |##1| will always contain the keyword, |##2| and |##3| will vary:
449 % \begin{center}\small
450 % \DeleteShortVerb{\|}
451 % \begin{tabular}{l|l|l|l}
452 % option string & \verb|##1| & \verb|##2| & \verb|##3| \\\hline
453 % \verb|key=value| & \verb|key| & \verb|value| & \verb|=| \\
454 % \verb|key=| & \verb|key| & (empty) & \verb|=| \\
455 % \verb|key| & \verb|key| & (empty) & (empty)
456 % \end{tabular}\\
457 % \MakeShortVerb{\|}
458 % \end{center}
459 % Now we can check the existence of the key\ldots
460 % \begin{macrocode}
461 \@ifundefined{KV@#1@##1}%
462 {}%
463 {%
464 % \end{macrocode}
465 % \ldots{}and, if found, add the complete string to the list
466 % of options to be parsed
467 % here and remove it from the list of unused options:
468 % \begin{macrocode}
469 \edef\Tot@tempa{\Tot@tempa,\CurrentOption,}%
470 \@expandtwoargs\@removeelement\CurrentOption
471 \@unusedoptionlist\@unusedoptionlist
472 }%
473 }%
474 % \end{macrocode}
475 % \end{macro}
476 % Add any global options that are known to KV to the start of the list
477 % being built in |\Tot@tempa| and mark them used by removing them from
478 % the unused option list.
479 % \begin{macrocode}
480 \@for\CurrentOption:=\@classoptionslist\do{%
481 \expandafter\Tot@tempopt\CurrentOption==\Tot@tempopt
482 }%
483 % \end{macrocode}
484 %
485 % Now stick the package options at the end of the list and wrap in a call
486 % to |\setkeys|.
487 % \begin{macrocode}
488 \edef\Tot@tempa{%
489 \noexpand\setkeys{#1}{%
490 \Tot@tempa\@ptionlist{\@currname.\@currext}%
491 }%
492 }%
493 % \end{macrocode}
494 %
495 % Do it. And drop unused macros
496 % \begin{macrocode}
497 \Tot@tempa
498 \let\Tot@no\relax
499 \let\Tot@temp\relax
500 \let\Tot@tempa\relax
501 \let\Tot@tempopt\relax
502 \let\Tot@ptionsWithKV\undefined
503 }
504 % \end{macrocode}
505 % \end{macro}
506 % \begin{macrocode}
507 \Tot@ptionsWithKV{Tot}%
508 % \end{macrocode}
509 %
510 % Unknown package options will be flagged as an error by |\setkeys|, so we
511 % will turn off \LaTeX's message. (Don't ask me why this has to be
512 % wrapped in |\AtEndOfPackage|. If it isn't, the warning will still appear.)
513 % \begin{macrocode}
514 \AtEndOfPackage{\let\@unprocessedoptions\relax}
515 % \end{macrocode}
516 %
517 % Finally, we check if the value given for |pagespersheet| makes sense.
518 % This code is not really perfect since using really strange things for
519 % the option value will cause havoc.
520 %
521 % \begin{macrocode}
522 \ifPagesPerSheet
523 \ifnum 1 > \TotPerSheet
524 \PackageError{totpages}{%
525 The number of pages per sheet must be positive.}{%
526 You cannot print less than one TeX page per sheet of paper.\MessageBreak
527 The option pagespersheet has been disabled.}
528 \PagesPerSheetfalse
529 \fi
530 \fi
531 % \end{macrocode}
532 %
533 % \begin{macro}{\theTotPages}
534 % \SpecialMainEnvIndex{TotPages}
535 % If the counter is to go to the \ftype{dvi} file, we have to use a
536 % fixed count register: \Count{1}. (For this to work, we have to copy
537 % a few internals from \ftype{latex.ltx}.) Otherwise, we'll just use a
538 % conventional \LaTeX\ counter.
539 % \begin{macrocode}
540 \ifTotPagesToDvi
541 \countdef\c@TotPages=1 \c@TotPages=0
542 \let\cl@TotPages\@empty
543 \newcommand\theTotPages{\arabic{TotPages}}
544 \else
545 \newcounter{TotPages}
546 \fi
547 % \end{macrocode}
548 % \end{macro}
549 % To save a bit of space,
550 % some control sequences for the options are removed. Some more will be removed
551 % at |\begin{document}|.
552 % \begin{macrocode}
553 \let\ifTotPagesToDvi\undefined
554 \let\TotPagesToDvitrue\undefined
555 \let\TotPagesToDvifalse\undefined
556 % \end{macrocode}
557 % When shipout occurs, we step the counter.
558 % \begin{macrocode}
559 \EveryShipout{\stepcounter{TotPages}}
560 % \end{macrocode}
561 % Nothing outside this package should mess with that counter, of
562 % course.---At the beginning of the document, we should be able to
563 % find out
564 % what packages are used and warn about some we aren't sure we can
565 % cooperate with:
566 %
567 % \begin{macrocode}
568 \AtBeginDocument{%
569 \@ifpackageloaded{count1to}{%
570 \PackageError{totpages}{%
571 Can't use both, count1to and totpages}{%
572 You should read the documentation of these packages and\MessageBreak
573 decide which one is more appropriate for your intentions.}
574 }{}%
575 \@ifpackageloaded{lastpage}{%
576 \PackageError{totpages}{%
577 Can't use both, lastpage and totpages}{%
578 You should read the documentation of these packages and\MessageBreak
579 decide which one is more appropriate for your intentions.}
580 }{}%
581 % \end{macrocode}
582 % \begin{macro}{\TotPages@putlabel}
583 % We define a command which will be executed at the end of the document,
584 % but if anybody did it already, we simply hope they did it right.
585 % \begin{macrocode}
586 \ifx\undefined\TotPages@putlabel
587 % \end{macrocode}
588 % If the package \package{hyperref} is used,
589 % we think we know what to do to produce a correct hyperlink label.
590 % (This code was obtained from the \package{lastpage} support of
591 % \package{hyperref} itself.)
592 % \begin{macrocode}
593 \@ifpackageloaded{hyperref}{%
594 % \end{macrocode}
595 % There are two different ways to produce a warning with \package{hyperref}:
596 % \begin{macrocode}
597 \ifx\undefined\Hy@WarningNoLine
598 \let\Hy@WarningNoLine\hyper@warn
599 \fi
600 \ifHy@pageanchor
601 \else
602 \Hy@WarningNoLine{%
603 The \string\ref{TotPages} link will not provide hyperlinks\MessageBreak
604 with disabled option `pageanchor'%
605 }%
606 \fi
607 % \end{macrocode}
608 % When |\nofiles| was requested, we can't do much since no \ftype{aux} is
609 % to be written, so we won't define anything.
610 % \begin{macrocode}
611 \if@filesw
612 \def\TotPages@putlabel{%
613 \addtocounter{page}{-1}%
614 \begingroup
615 \let\@number\@firstofone
616 \ifHy@pageanchor
617 \ifHy@hypertexnames
618 \ifHy@plainpages
619 \def\Hy@temp{\arabic{page}}%
620 \else
621 \let\textlatin\@firstofone
622 \edef\Hy@temp{\thepage}%
623 \fi
624 \else
625 \def\Hy@temp{\the\Hy@pagecounter}%
626 \fi
627 \fi
628 \immediate\write\@mainaux{%
629 \string\newlabel
630 {TotPages}{{\theTotPages}{\thepage}{}{%
631 \ifHy@pageanchor page.\Hy@temp\fi}{}}%
632 }%
633 \endgroup
634 \addtocounter{page}{1}%
635 }%
636 \fi
637 % \end{macrocode}
638 % Otherwise, we just put a label here, but |\label| may refer
639 % to some random counter just stepped some time ago, so we have to
640 % produce it ourselves.
641 % \begin{macrocode}
642 }{%
643 \if@filesw
644 \def\TotPages@putlabel{%
645 \addtocounter{page}{-1}%
646 \immediate\write\@mainaux{%
647 \string\newlabel{TotPages}{{\theTotPages}{\thepage}}%
648 }%
649 \addtocounter{page}{1}%
650 }%
651 \fi
652 }%
653 \fi
654 % \end{macrocode}
655 % \end{macro}
656 %
657 % \begin{macro}{\TotSheets}
658 % \begin{macro}{\TotPages@putSheets}
659 % When |pagespersheet| is disabled, any use of |\TotSheets|
660 % will be flagged as an error. Otherwise, the initial value will
661 % be |0000|. When the value computed during the previous
662 % run has been read from the \ftype{aux} file, we just assume it is right
663 % and check at the end of the document if it is still the same.
664 % \SpecialEnvIndex{TotPages}
665 % \begin{macrocode}
666 \ifPagesPerSheet
667 \ifx\undefined\TotSheets
668 \newcommand{\TotSheets}{0000}
669 \fi
670 \def\TotPages@putSheets{%
671 \addtocounter{TotPages}{\TotPerSheet}
672 \addtocounter{TotPages}{-1}
673 \divide \value{TotPages} by \TotPerSheet
674 \edef\Tot@temp{\arabic{TotPages}}
675 \ifx\Tot@temp\TotSheets \else
676 \PackageWarning{totpages}{%
677 The number of sheets may have changed (\TotSheets => \Tot@temp).\MessageBreak
678 You should run your document through the formatter again.}
679 \fi
680 \if@filesw
681 \immediate\write\@mainaux{%
682 \string\gdef\string\TotSheets{\Tot@temp}}%
683 \fi
684 }%
685 \else
686 \let\TotSheets\undefined
687 \fi
688 % \end{macrocode}
689 % \end{macro}
690 % \end{macro}
691 %
692 % Before we write the value to the \ftype{aux} file we call a \cs{clearpage}
693 % to force all pending floats to be output.
694 % To do this we have to be sure to be the \emph{last} macro called by
695 % \cs{AtEndDocument}---but this is nearly impossible to assure,
696 % so we try to do our very best and set our
697 % entry for \cs{AtEndDocument} during |\begin{document}| processing.
698 %
699 % When |\nofiles| is specified, though, we won't have to do anything
700 % at the end of the document.
701 % \begin{macrocode}
702 \if@filesw
703 \AtEndDocument{%
704 % \end{macrocode}
705 % The label \texttt{TotPages} is put onto the last page
706 % of the document and holds the total number of pages of the
707 % document, i.\,e., the value of |TotPages|.
708 % \begin{macrocode}
709 \clearpage
710 \TotPages@putlabel
711 }%
712 \fi
713 % \end{macrocode}
714 %
715 % Finally, we may have to compute the number of sheets. This
716 % is done even when no files are written to allow checking for changes.
717 % \begin{macrocode}
718 \ifPagesPerSheet
719 \AtEndDocument{\TotPages@putSheets}
720 \fi
721 \let\ifPagesPerSheet\undefined
722 \let\PagesPerSheettrue\undefined
723 \let\PagesPerSheetfalse\undefined
724 }
725 %</package>
726 % \end{macrocode}
727 %
728 % \section{An example file}
729 % And, at last, there
730 % is a small example file using features of package \package{totpages}.
731 %
732 % \begin{macrocode}
733 %<*example>
734 \documentclass[pagespersheet=2]{article}
735 \usepackage[dvips]{hyperref}
736 %%%%% Replace preceding line by the following one for pdflatex:
737 %%\usepackage[pdftex]{hyperref}
738 \usepackage[dvi]{totpages}
739 \renewcommand{\theTotPages}{\roman{TotPages}}
740 \begin{document}
741 \setcounter{page}{12345}
742 This is the number of pages (in Roman numerals): ``\ref{TotPages}''.
743 On the last page, the number ``\pageref{TotPages}'' is printed.
744
745 \clearpage
746
747 a second page\ldots
748
749 \clearpage
750
751 and a last one\ldots\ldots
752
753
754 When printed on a duplex printer in duplex mode, \TotSheets{} sheets of
755 paper will be needed.
756 \end{document}
757 %</example>
758 % \end{macrocode}
759 %
760 % ^^A -----------------------------
761 %
762 % \Finale
763 %
764 % ^^A Run ``makeindex -s gind.ist totpages.idx'' and
765 % ^^A ``makeindex -s gglo.ist -o totpages.gls totpages.glo''
766 % ^^A to format indexes!
767 % \PrintChanges
768 % \PrintIndex
769 % ^^A Make sure that the index is not printed twice
770 % ^^A (ltxdoc.cfg might have a second \PrintIndex command)
771 % \let\PrintChanges\relax
772 % \let\PrintIndex\relax