Debian Codegraph @

texmf-dist/tex/plain/cweb/cwebbin/pdfwebtocfront.tex

raw
1 % pdfwebtocfront.tex
2 % Code from knuth.drv in https://github.com/oberdiek/latex-tds
3 % 2024/05/25 v3.1 by Andreas Scherer.
4 %
5 % This file is part of project https://github.com/ascherer/cwebbin
6 % and may be distributed under the MIT License or the LaTeX Project
7 % Public License.
8 %
9 % Move table-of-contents page to the front in PDF output.
10 % Works with pdftex/xetex/luatex and plain tex/dvipdf in connection
11 % with either 'pwebmac.tex' for Pascal/WEB programs or 'cwebmac.tex'
12 % for C/CWEB programs.
13 %
14 \ifx\detokenize\undefined\endinput\fi
15 \ifx\twodigits\undefined\endinput\fi
16 \def\contentsfile{\jobname.toc} % file that gets table of contents info
17 \newif\iftexmf\texmffalse % special toc treatment for TeX, Metafont, etc.
18 \newread\testread
19 \openin\testread=\contentsfile\relax
20 \ifeof\testread % First run
21 \else\closein\testread % Second run
22 % Fix page numbers in the PDF bootmarks with 'Page Labels'
23 \def\tocpages{1} % Most programs have one-page Table-of-Contents
24 \def\tocstart{2} % so we start with (fake) page 2 for the rest
25 \def\pagemode{/PageMode /UseOutlines}
26 \def\pagelabels{/PageLabels << /Nums [
27 \ifnum\contentspagenumber=0 0 << /P(Contents) >> \tocpages << /S/D/St 1 >>
28 \else 0 << /S/D/St \contentspagenumber >> \fi ] >> }
29 \def\startpdf{\ifpdflua\pdfcatalog{\pagemode\space\pagelabels}
30 \else {\special{pdf: docview << \pagemode\space\pagelabels >>}}\fi}
31 % Redefine '\con' to be invoked before the first '\N' (starred section).
32 \let\ORGcon\con
33 \def\con{%
34 % reduce size of PDF pages for more screen space
35 \pdfpagewidth=\pagewidth \advance\pdfpagewidth by 1in
36 \pdfpageheight=\fullpageheight \advance\pdfpageheight by 1in
37 \iftexmf \advance\pdfpageheight by 0.5in
38 \gtitle={Table of Contents} % overwrite default 'WEB output'
39 \mark{\noexpand\nullsec{\the\gtitle}} \fi
40 \ifpdflua \pdfhorigin=0.5in \pdfvorigin=0.5in
41 \else \global\pageshift=-0.5in
42 \global\hoffset=-0.5in \global\voffset=-0.5in \fi
43 \begingroup
44 \let\end\relax
45 \ORGcon
46 \endgroup
47 \let\con\end
48 }%
49 % Special variant for 'mf.web' and 'tex.web' (et al.).
50 % They all define a peculiar version of '\N' in their preamble.
51 % And they put their tables-of-contents on sparse 'Contents' page(s)
52 % and start the main body on page '3'.
53 \edef\x{\jobname}%
54 \edef\y{\detokenize{tex}}%
55 \ifx\x\y \texmftrue\else
56 \edef\y{\detokenize{mf}}%
57 \ifx\x\y \texmftrue\else
58 \edef\y{\detokenize{pdftex}}%
59 \ifx\x\y \texmftrue \def\tocpages{2}\else
60 \edef\y{\detokenize{xetex}}%
61 \ifx\x\y \texmftrue\fi\fi\fi\fi
62 \iftexmf
63 % These main programs start on page '3' (after any number of ToC pages)
64 \def\pagelabels{/PageLabels << /Nums [
65 0 << /P(Contents) >> \tocpages << /S/D/St 3 >> ] >> }
66 \expandafter\let\csname ORGN\expandafter\endcsname
67 \csname N\endcsname
68 \expandafter\outer\expandafter\def\csname N\endcsname{%
69 \titletrue \con \pageno=\tocstart
70 \expandafter\let\csname N\expandafter\endcsname
71 \csname ORGN\endcsname
72 \csname N\endcsname
73 }%
74 \else % Much more general approach for WEB and CWEB programs.
75 \edef\y{\detokenize{mp}}%
76 % This main program starts on page '3' (after any number of ToC pages)
77 \ifx\x\y \def\pagelabels{/PageLabels << /Nums [
78 0 << /P(Contents) >> \tocpages << /S/D/St 3 >> ] >> } \fi
79 % In fact, '\con' will be invoked _inside_ the first '\N' right _after_
80 % '\MN', but _before_ '\eject'. And '\topofcontents' gets in the twist
81 % as well.
82 \let\ORGtopofcontents\topofcontents \let\ORGrheader\rheader
83 \def\topofcontents{%
84 \def\:{\par\hangindent 2em} % Fix for 'bibtex.web'; from '\def\fin'.
85 \ORGtopofcontents
86 \let\topofcontents\relax
87 }%
88 % Squeeze '\con' between '\MN' and '\eject' in the very first '\N'.
89 \expandafter\let\csname ORGvfil\expandafter\endcsname
90 \csname vfil\endcsname
91 \expandafter\let\csname ORGeject\expandafter\endcsname
92 \csname eject\endcsname
93 \expandafter\outer\expandafter\def\csname vfil\endcsname{%
94 \let\eject\relax
95 \titletrue % prepare to output the table of contents
96 \topofcontents \con
97 \advance\pageno by -1\relax
98 \expandafter\let\csname vfil\expandafter\endcsname
99 \csname ORGvfil\endcsname
100 \expandafter\let\csname eject\expandafter\endcsname
101 \csname ORGeject\endcsname
102 \vfil\eject
103 \let\rheader\ORGrheader
104 }%
105 \fi
106 \fi