summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/titlesec/titletoc.sty
blob: 2bd15c3cfe7fe9312b984bc101e8f209f4577b96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
% +---------------------------------------------+
% | Documentation is in the titlesec.tex file.  |
% +---------------------------------------------+
%
% Copyright (c) 1998-2007 by Javier Bezos.
% All Rights Reserved.
%
% This file is part of the titlesec distribution release 2.8
% -----------------------------------------------------------
%
% It may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2003/12/01 or later.
% 
% This work has the LPPL maintenance status "maintained".
% 
% The Current Maintainer of this work is Javier Bezos.

% History of v1.1
% ~~~~~~~~~~~~~~~
% 99/04/22: A bug fix. Some missing global assignments in \ttl@tocentry
%           were missing, and page numbers messed up with 
%           \titlecontents*
% 
% History of v1.2
% ~~~~~~~~~~~~~~~~
% 99/10/20: Added \contentsafter. \ttl@tocentry modified to accept
%           the new command for grouping in a paragraph entries of
%           different levels.
% 99/10/27: Instead of defining the level numbers explicitly, that's
%           done with \ttl@setlevels and \ttl@levels.
% 99/10/27: Added the basic code for multiple indexes.
% 99/10/29: Added the toctop test to the \ttl@savel@... commands.
% 99/12/28: Simplified \ttl@savel@... with \ttl@lselect.
% 00/01/12: \contentsafter rejected. An aditional optional parameter
%           is used instead. (Too) many thing had to be rewritten.
% 00/01/27: Removed \ttl@setlevels. Any change of levels are
%           best done with titlesec.
% 00/02/14: Moved misplaced \vskip in ttl@tocentry.
% 00/02/16: A few changes in the "block" separators.
% 00/02/16: Fixed bug in \ttl@margin. For the right margin the
%           correction was used instead of the margin.
%
% History of v1.2.1
% ~~~~~~~~~~~~~~~~~
% 00/04/25: Dirty trick to make sure that \ttl@finish is the very
%           last thing in lists of floats (lof, lot, etc.).
% 00/05/14: Missing \global before \ttl@labelfalse added.
% 
% History of 1.2.2
% ~~~~~~~~~~~~~~~~
% 00/06/05: The optional argument no longer mandatory, but
%           still reporting an error if the length is missing.
%           Minor internal changes, and a patch mechanism added.
%
% History of 1.3
% ~~~~~~~~~~~~~~
% 01/01/12: Minor changes and some missing \globals added for
%           partial tocs to work inside a group.
% 01/01/21: Added \dottedcontents.
% 
% History of 1.4
% ~~~~~~~~~~~~~~
% 02/03/27: titletoc.new merged into this file
% 02/03/27: \ttl@finish written by the last \newpage. 
% 02/03/27: \ttl@lsection broke paragraphs always. Now
%           only if the sublevel is typeset.
% 02/03/27: \ttl@finish -> \contentsfinish
%
% History of 1.5
% ~~~~~~~~~~~~~~
% 03/06/14: \contentspush didn't work because of the change of
% (.new)    \leftskip  was done inside a group before the
%           corresponding \par 
% 03/06/14: Added an useful error if a partial TOC is not
% (.new)    defined.
% 05/01/17: New options rubber/rigidseps, to remove if desired
%           the small vertical "plus" between entries.
% 05/01/20: Begin work on partial lists.
% 05/01/21: \ttl@outblock and \ttl@outnoblock just to avoid a
%           huge definition and to ease patching.
% 05/01/22: Work on partial lists finished (but to be resumed).

% History of 1.6
% ~~~~~~~~~~~~~~
% 07/05/04: Added the patch 2005/05/03 which fixed a wrong
%           result with \included (F. Mittelbach)
% 07/07/14: Removed the patch mechanism.

\ProvidesPackage{titletoc}[2007/08/12 v1.6 TOC entries]

% The following tags are used:
% ttl@  : the generic one, shared with titlesec
% ttlb@ : starting punctuation for block format
% ttlm@ : and middle
% ttle@ : and ending
% ttll@ : section level
% ttlx@ : file extension
% 
% Fistly, comes the
% common stuff with titlesec
% ~~~~~~~~~~~~~~~~~~~~~~~~~~

\newif\ifttl@label
\newif\ifttl@fromblock
\newdimen\ttl@leftsep

\providecommand\titleline{%
  \@ifstar{\ttl@line@i{\hb@xt@\titlewidth}}%
          {\ttl@line@i{}}}

\def\ttl@line@i#1{%
  \@ifnextchar[{\ttl@line{#1}}{\ttl@line{#1}[s]}}

\def\ttl@line#1[#2]#3{%
  \vskip\topskip
  \hrule \@height \z@
  \nobreak
  \vskip-\topskip
  \begingroup
    \parindent\z@
    \everypar{}%
    \leftskip\z@
    \rightskip\z@   %  #1 is either \hb@xt@\titlewidth or empty:
    \@makebox[\hsize][#2]{\ttl@makeline{#1{#3}}}%
    \par
  \endgroup
  \hrule height \z@
  \nobreak}

% Fillers:

\providecommand\titlerule{\@ifstar{\ttl@row}{\ttl@rule}}

\let\ttl@leaders\xleaders

\def\ttl@row{\@ifnextchar[{\ttl@row@i}{\ttl@row@i[\wd\z@]}}
\def\ttl@row@i[#1]#2{%
  \ifvmode\expandafter\titleline\fi
  {\sbox\z@{#2}%
   \hspace{-#1}%
   \hskip\wd\z@
   \ttl@leaders\hb@xt@#1{\hss\box\z@}%
   \hfill\kern\z@}}

\def\ttl@rule{\@ifnextchar[{\ttl@rule@i}{\ttl@rule@i[.4pt]}}
\def\ttl@rule@i[#1]{%
  \ifvmode\expandafter\titleline\fi
  {\leaders\hrule height #1\hfill\kern\z@}}

\providecommand\filright{%
  \gdef\ttl@filleft##1{\hskip##1}%
  \gdef\ttl@filright##1{\hfill}%
  \let\\\@centercr
  \advance\rightskip\z@ \@plus 1fil\relax}
\providecommand\filleft{%
  \gdef\ttl@filleft##1{\hfill}%
  \gdef\ttl@filright##1{\hskip##1}%
  \let\\\@centercr
  \advance\leftskip\z@ \@plus 1fil
  \parfillskip\z@}
\providecommand\filcenter{\filleft\filright
  \gdef\ttl@filleft##1{\hfill}}
\providecommand\fillast{%
  \gdef\ttl@filleft##1{\hfill}%
  \gdef\ttl@filright##1{\hfill}%
  \let\\\@centercr
  \filleft\advance\rightskip\z@ \@plus -1fil
  \parfillskip\z@ \@plus 2fil\relax}
  
% Now, the specific titletoc part

% User interface
% ~~~~~~~~~~~~~~
% Tools:

\DeclareOption{dotinlabels}{\def\ttl@idot{.}}
\DeclareOption{nodotinlabels}{\let\ttl@idot\@empty}

\DeclareOption{rigidseps}{%
  \def\ttl@contentsstretch{\vskip\z@}}
\DeclareOption{rubberseps}{%
  \def\ttl@contentsstretch{\vskip\z@\@plus.1\p@}}

\DeclareOption{leftlabels}{%
  \renewcommand\numberline[1]{\hb@xt@\@tempdima{#1\ttl@idot\hfil}}%
  \newcommand\contentslabel[2][\thecontentslabel\ttl@idot]{%
%   \let\ttl@a\thecontentslabel   %%%%% For the star variant
%   \def\thecontentslabel{#2}%    %%% To be fully implemented... when?
%   \setbox\z@\hbox{#1}%
%   \dimen@\wd\z@
%   \let\thecontentslabel\ttl@a
%   \hspace*{-\dimen@}\hb@xt@\dimen@{#1\hfil}
%  \show\ttl@b \show\ttl@a
    \hspace*{-#2}\hb@xt@#2{#1\hfil}}}

\DeclareOption{rightlabels}{%
  \renewcommand\numberline[1]{\hb@xt@\@tempdima{\hss#1\ttl@idot\enspace}}%
  \let\contentslabel\relax
  \newcommand\contentslabel[2][\thecontentslabel\ttl@idot\enspace]{%
    \hspace*{-#2}\hb@xt@#2{\hfil#1}}}

\newcommand\contentspage[1][\thecontentspage]{%
  \hb@xt@\@pnumwidth{\hfil#1}%
  \hspace*{-\@pnumwidth}}

\newcommand\contentspush[1]{%
  \sbox\z@{#1}%
  \xdef\ttl@b{\advance\leftskip\the\wd\z@}%
  \aftergroup\ttl@b
  \leavevmode\llap{\box\z@}}

% General commands. A level register. Explicit numbers
% because they are used in csnames. Ignored if already defined.

\ifx\ttll@section\@undefined

  \@ifundefined{chapter}
    {\def\ttll@part{0}}
    {\def\ttll@part{-1}%
     \def\ttll@chapter{0}}
  \def\ttll@section{1}
  \def\ttll@subsection{2}
  \def\ttll@subsubsection{3}
  \def\ttll@paragraph{4}
  \def\ttll@subparagraph{5}

\fi

% We make sure that a series of * entries are finished and
% that a \titlecontents in the middle of a document is
% written to the right file. We need ship out floats before;
% that's very tricky.

\newcommand\contentsuse[2]{%
  \expandafter\def\csname ttlx@#1\endcsname{#2}%
  \expandafter\def\csname ttll@#1\endcsname{-1000}%
  \expandafter\def\expandafter\ttl@finishall\expandafter{%
    \ttl@finishall
    \@writefile{#2}{\contentsfinish}}}

\def\ttl@finishall{\@writefile{toc}{\contentsfinish}}

\AtEndDocument{%
  \ttl@startlists
  \let\ttl@newpage\newpage
  \def\newpage{%
    \let\newpage\ttl@newpage
    \newpage
    \if@filesw
      \immediate\write\@mainaux{\string\ttl@finishall}%
    \fi}}

\contentsuse{figure}{lof}
\contentsuse{table}{lot}

\def\contentsfinish{%
  \ifttl@fromblock
    \xdef\ttl@b{-10000}% ships out any saved punctuation
    \ttl@preend
    \@@par
    \endgroup
    \global\ttl@fromblockfalse
  \fi}

% The two basic commands. First \contentsmargin:

\newcommand\contentsmargin[1][\z@]{%
  \def\ttl@corr{#1}\def\@pnumwidth}

% The following is the value of \contentsmargin when
% used inside \ttl@tocenty (ie, \titlecontents).

\newcommand\ttl@margin[2][\z@]{%
  \def\ttl@corr{#1}%
  \advance\rightskip-\@pnumwidth\relax
  \advance\rightskip#2\relax
  \def\@pnumwidth{#2}}

% \titlecontents deals with concepts, not commands; hence no
% escape char

\newcommand\titlecontents{%
  \@ifstar{\ttl@contents{\z@}}%
          {\ttl@contents{\@ne}}}

\def\ttl@contents#1#2{%
  \@ifnextchar[{\ttl@contents@i{#1}{#2}}%
               {\ttl@contents@i{#1}{#2}[\@nil]}}

\def\ttl@contents@i#1#2[#3]#4#5#6#7{%
  \expandafter\def\csname l@#2\endcsname
    {\ttl@tocentry{#1}{#2}{#3}{#4}{{#5}{#6}}{#7}}%
  \@ifnextchar[{\ttl@contents@ii{#1}{#2}}%
               {\ttl@contents@ii{#1}{#2}[]}}

\def\ttl@contents@ii#1#2[#3]{%
  \ifcase#1\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {\@ifnextchar[{\ttl@contents@iii{#1}{#2}{#3}}%
                {\ttl@contents@iii{#1}{#2}{#3}[]}}%
  {\ttl@contents@iv{#1}{#2}{#3}[][]}}

\def\ttl@contents@iii#1#2#3[#4]{%
  \@ifnextchar[{\@tempswatrue\ttl@contents@iv{#1}{#2}{#3}[#4]}%
               {\@tempswafalse\ttl@contents@iv{#1}{#2}{#3}[#4][]}}

\def\ttl@contents@iv#1#2#3[#4][#5]{%
  \ifcase#1\relax
    \if@tempswa
      \ttl@contents@v{#2}{#3}{#4}{#5}%
    \else
      \ttl@contents@v{#2}{}{#3}{#4}%
    \fi
  \else
    \ttl@contents@v{#2}{}{}{#3}%
  \fi}

\def\ttl@contents@v#1#2#3#4{%
  \expandafter\def\csname ttlb@#1\endcsname{#2}%
  \expandafter\def\csname ttlm@#1\endcsname{#3}%
  \expandafter\def\csname ttle@#1\endcsname{#4}}

\begingroup
\catcode`\-=12\catcode`\>=12
\gdef\ttl@strip#1->#2\@@#3{\def#3{#2}}
\endgroup

\AtBeginDocument{%
  \def\ttl@change@i#1#2#3#4#5#6#7{%
    \expandafter\def\csname l@#2\endcsname
      {\ttl@tocentry{#1}{#2}{#3}{#4}{{#5}{#6}}{#7}}}%
  \let\ttl@change@v\ttl@contents@v
  \def\ttl@contents@i#1#2[#3]#4#5#6#7{%
    \def\ttl@a{\ttl@change@i{#1}{#2}{#3}{#4}{#5}{#6}{#7}}%
    \@ifnextchar[{\ttl@contents@ii{#1}{#2}}%
                 {\ttl@contents@ii{#1}{#2}[]}}%
  \def\ttl@contents@v#1#2#3#4{%
    \def\ttl@b{\ttl@change@v{#1}{#2}{#3}{#4}}%
    \expandafter\ttl@strip\meaning\ttl@a\@@\ttl@a
    \expandafter\ttl@strip\meaning\ttl@b\@@\ttl@b
    \edef\ttl@c{%
      \expandafter\ifx\csname ttlx@#2\endcsname\relax
        toc%
      \else
        \csname ttlx@#2\endcsname
      \fi}%
    \addtocontents{\ttl@c}{\ttl@a\relax}%
    \addtocontents{\ttl@c}{\ttl@b\relax}}}

% Printing the toc entry
% ~~~~~~~~~~~~~~~~~~~~~~

\def\ttl@lasttoc{-1000} % An inital dummy assignment

% 1 ifblock, 2 sect name, 3 left, 4 before,
% 5 {with}{without}, 6 filler/page, 7  title 8 pageno

\let\ttl@preend\@empty

\def\ttl@outpunct{%
  \ifnum\ttl@c>\ttl@b\relax
    \ttl@preend
  \fi}

\def\ttl@outblock#1#2#3{%
  \ifcase#1\relax
    \ifnum\ttl@b>\ttl@a\relax
      \begingroup
      \protected@edef\ttl@preend{%
        \@nameuse{ttle@#2}%
        \endgroup
        \protect\@namedef{ttl@c}{\ttl@a}%
        \protect\ttl@outpunct}%
      #3%
      \@nameuse{ttlb@#2}%
    \else\ifnum\ttl@b<\ttl@a\relax
      \ttl@preend
      \@nameuse{ttlm@#2}%
    \else
      \@nameuse{ttlm@#2}%
    \fi\fi
  \else
    \ttl@preend
    \@@par
    \endgroup
    \@firstoftwo
  \fi}

\def\ttl@outnoblock#1#2#3{%
  \begingroup
  \ifnum\ttl@b>\ttl@a
    \nobreak
  \else\ifnum\ttl@b<\ttl@a
    \addpenalty{\@secpenalty}%
  \else
    \addpenalty{\z@}% 
  \fi\fi
  \ttl@contentsstretch
  \nobreak
  \ifcase#1\relax\else\interlinepenalty\@M\fi
  \parindent\z@
  \ifx\@nil#2%
    \PackageError{titletoc}{Unimplemented}%
	    {The optional argument is currently mandatory}%
  \else
    \setlength\leftskip{#2}%
  \fi
  \setlength\rightskip{\@pnumwidth}%
  \let\contentsmargin\ttl@margin
  \def\ttl@makeline##1{##1}%
  #3%
  \addtolength{\parfillskip}{-\ttl@corr}%
  \addtolength{\rightskip}{\ttl@corr}%
  \let\ttl@leaders\leaders}

\def\ttl@tocentry#1#2#3#4#5#6#7#8{%
  \xdef\ttl@b{\csname ttll@#2\endcsname}%
  \ifnum\ttl@b>\c@tocdepth\else
    \ifnum\ttl@b<\ttl@toctop\else
      \edef\ttl@a{\ttl@lasttoc}%
      \gdef\thecontentspage{#8}%
      \global\let\thecontentslabel\@empty
      \global\ttl@labelfalse
      \sbox\z@{%  Unused box. It just catch the numberline
        \def\numberline##1{\global\ttl@labeltrue\gdef\thecontentslabel{##1}}%
        #7}%    \ttl@b = current     \ttl@a = previous
      \ifttl@fromblock
        \ttl@outblock{#1}{#2}{#4}%
      \else
        \ttl@outnoblock{#1}{#3}{#4}%
      \fi
      \def\numberline##1{\ignorespaces}%
      \ifttl@label
        {\leavevmode\strut\@firstoftwo#5{#7}\strut\kern\z@}%
      \else
        {\leavevmode\strut\@secondoftwo#5{#7}\strut\kern\z@}%
      \fi
      {#6}%
      \ifcase#1\relax
        \ifttl@fromblock\else
          \protected@edef\ttl@preend{\@nameuse{ttle@#2}}%
        \fi
        \global\ttl@fromblocktrue
      \else
        \@@par
        \nobreak
        \csname ttle@#2\endcsname
        \endgroup
        \global\ttl@fromblockfalse
      \fi
      \xdef\ttl@lasttoc{\csname ttll@#2\endcsname}%
    \fi
  \fi
  \ignorespaces}

\newcommand\dottedcontents{}
\def\dottedcontents#1[#2]#3#4#5{%
  \titlecontents{#1}[#2]{#3}%
    {\contentslabel{#4}}%
    {\hspace*{-#4}}%
    {\titlerule*[#5]{.}\contentspage}}

\ExecuteOptions{leftlabels,nodotinlabels,rubberseps}

\ProcessOptions

% Multiple tocs and lists
% ~~~~~~~~~~~~~~~~~~~~~~~~
% After some attemps to adapt titletoc to minitoc, I've decided
% to implement my own solution, because entries as written by
% minitoc are non standard and unmanageable. The new commands
% provides a good deal of flexibility, too.

\let\ttl@startlists\@empty
\let\ttl@writefile\@writefile

\def\ttl@partialtoc{ptc}
\def\ttl@partiallof{plf}
\def\ttl@partiallot{plt}

\def\ttl@writepartial#1#2{%
  \ttl@topartial{toc}{#1}{#2}%
  \ttl@topartial{lof}{#1}{#2}%
  \ttl@topartial{lot}{#1}{#2}%
  \ttl@writefile{#1}{#2}}

\def\ttl@topartial#1#2#3{%
  \def\ttl@a{#1}%
  \def\ttl@b{#2}%
  \ifx\ttl@a\ttl@b
     \ttl@writefile{\csname ttl@partial#2\endcsname}{#3}%
  \fi}

\def\ttl@xstartlist#1{%
  \@ifundefined{ttl@startlist#1}{%
    \global\@namedef{ttl@startlist#1}{%
      \let\@writefile\ttl@writepartial
      \if@filesw
        \expandafter\newwrite\csname tf@#1\endcsname
        \immediate\openout\csname tf@#1\endcsname\jobname.#1\relax
      \fi
      \@nobreakfalse}%
    \expandafter\gdef\expandafter\ttl@startlists\expandafter{%
      \ttl@startlists
      \@nameuse{ttl@startlist#1}}}{}}

\newcommand\startcontents[1][default]{\startlist[#1]{toc}}

\newcommand\startlist[2][default]{%
  \expandafter\ttl@xstartlist\csname ttl@partial#2\endcsname
  \@ifundefined{c@ttl@#2@#1}%
    {\newcounter{ttl@#2@#1}}%
    {\stoplist[#1]{#2}}%  
  \stepcounter{ttl@#2@#1}%
  \resumelist[#1]{#2}}

\newcommand\stopcontents[1][default]{\stoplist[#1]{toc}}

\newcommand\stoplist[2][default]{%
  \protected@write\@auxout{}{%
    \string\ttl@writefile{\csname ttl@partial#2\endcsname}{%
       \string\ttl@stoptoc{#1@\arabic{ttl@#2@#1}}}}}

\newcommand\resumecontents[1][default]{\resumelist[#1]{toc}}

\newcommand\resumelist[2][default]{%
  \protected@write\@auxout{}{%
    \string\ttl@writefile{\csname ttl@partial#2\endcsname}{%
       \string\ttl@starttoc{#1@\arabic{ttl@#2@#1}}}}}

\def\ttl@starttoc#1{%
  \ifx\@writefile\@gobbletwo\else % Is this test necessary?
    \def\ttl@a{#1}%
    \ifx\ttl@a\ttl@ptoc
      \let\contentsline\ttl@contentsline
    \fi
  \fi
  \ignorespaces}

\def\ttl@stoptoc#1{%
  \ifx\@writefile\@gobbletwo\else % Is this test necessary?
    \def\ttl@a{#1}%
    \ifx\ttl@a\ttl@ptoc
      \let\contentsline\ttl@gobblecontents
    \fi
  \fi
  \ignorespaces}

\newcommand\printcontents[4][default]{%
  \ttl@printlist[#1]{toc}{#2}{#3}{#4}}

\newcommand\printlist[4][default]{%
  \ttl@printlist[#1]{#2}{#3}{-1001}{#4}}

\newcommand\ttl@printlist[5][default]{%
  \begingroup
    \@ifundefined{c@ttl@#2@#1}%
      {\PackageError{titletoc}{No partial #2 named #1}%
          {You must start before a partial toc/list\MessageBreak
          with \string/startcontents/\string\startlist.}}{}%
    \edef\ttl@ptoc{#1@\arabic{ttl@#2@#1}}%
    \def\ttl@toctop{#4}%
    #5%
    \let\ttl@xcontentsline\contentsline
    \let\contentsline\ttl@gobblecontents
    \def\ttl@contentsline##1{%
      \@ifundefined{ttll@#3##1}%
        {\expandafter\let\csname ttll@#3##1\expandafter\endcsname
           \csname ttll@##1\endcsname}{}%
      \@ifundefined{l@#3##1}%
        {\ttl@xcontentsline{##1}}%
        {\ttl@xcontentsline{#3##1}}}%
    \makeatletter
    \@input{\jobname.\csname ttl@partial#2\endcsname}%
    \makeatother
    \@nobreakfalse
  \endgroup}

\AtBeginDocument{%
  \ifx\ttl@gobblecontents\@undefined
    \def\ttl@gobblecontents#1#2#3{\ignorespaces}%
  \fi}

\def\ttl@toctop{-1000}

% Now the we add \contentsfinish to the current definitions
% and a "selector" for partial tocs

\def\ttl@lselect#1{%
  \ifnum\csname ttll@#1\endcsname>\c@tocdepth\else
    \contentsfinish
  \fi
  \ifnum\csname ttll@#1\endcsname<\ttl@toctop\relax
    \expandafter\@gobbletwo
  \else
    \expandafter\expandafter\csname ttl@savel@#1\endcsname
  \fi}

\let\ttl@savel@part\l@part
\def\l@part{\ttl@lselect{part}}

\let\ttl@savel@chapter\l@chapter
\def\l@chapter{\ttl@lselect{chapter}}

\let\ttl@savel@section\l@section
\def\l@section{\ttl@lselect{section}}

\let\ttl@savel@subsection\l@subsection
\def\l@subsection{\ttl@lselect{subsection}}

\let\ttl@savel@subsubsection\l@subsubsection
\def\l@subsubsection{\ttl@lselect{subsubsection}}

\let\ttl@savel@paragraph\l@paragraph
\def\l@paragraph{\ttl@lselect{paragraph}}

\let\ttl@savel@subparagraph\l@subparagraph
\def\l@subparagraph{\ttl@lselect{subparagraph}}

\let\ttl@savel@figure\l@figure
\def\l@figure{\ttl@lselect{figure}}

\let\ttl@savel@table\l@table
\def\l@table{\ttl@lselect{table}}

\@tempskipa\@pnumwidth
\edef\@pnumwidth{\the\@tempskipa}
\advance\@tempskipa-\@tocrmarg
\edef\ttl@corr{-\the\@tempskipa}

\endinput