-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtitlesec.tex
2160 lines (1802 loc) · 73.1 KB
/
titlesec.tex
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%
% Copyright (C) 1998-2025 Javier Bezos http://www.texnia.com
%
% This file may be distributed and/or modified under the conditions of
% the MIT License. A version can be found at the end of this file.
%
% Repository: https://github.com/jbezos/titlesec
%
\def\fileversion{2.17}
\def\docdate{2025/01/04}
\documentclass[a4paper]{ltxguide}
\usepackage[sf,bf,compact,topmarks,calcwidth,pagestyles]{titlesec}
\usepackage{titletoc,color}
\def\gobble#1{}
\def\cs#1{\expandafter\gobble\string\\#1}
\definecolor{notes}{rgb}{.75, .3, .3}%
\makeatletter
\newenvironment{desc}
{\if@nobreak
\vskip-\lastskip
\vspace*{-2.5ex}%
\fi
\decl}
{\enddecl}
\def\@begintheorem#1#2{%
\list{}{}%
\global\advance\@listdepth\m@ne
\item[{\sffamily\bfseries\color{notes}\MakeUppercase{#1}}]}%
\newtheorem{warning}{Warning}
\newtheorem{note}{Note}
\newtheorem{example}{Example}
\makeatother
\usepackage{textcomp}
\usepackage[linktocpage]{hyperref}
\title{The \textsf{titlesec}, \textsf{titleps} and \textsf{titletoc}
Packages\footnote{The \textsf{titlesec} package is currently at
version 2.17. \copyright{} 1998--2025 Javier Bezos.}}
\author{Javier Bezos\footnote{For bug reports, comments and
suggestions go to \href{http://www.tex-tipografia.com/contact.html}%
{\texttt{http://www.tex-tipografia.com/contact.html}}. English
is not my strong point, so contact me when you find mistakes in the
manual. Other packages by the same author: \textsf{gloss} (with
Jos\'e Luis D\'{\i}az), \textsf{enumitem, accents, tensind, esindex,
dotlessi, babeltools}.}}
\date{\docdate}
\widenhead{2.1pc}{0pc}
\titlelabel{\thetitle.\quad}
\renewpagestyle{plain}[\small\sffamily\slshape]{
\footrule
\setfoot{}{\usepage}{}}
\newpagestyle{myps}[\small\sffamily\slshape]{
\headrule
\sethead{Titlesec}{\sectiontitle}{\usepage}}
\pagestyle{myps}
\newcommand{\examplesep}{%
\begin{center}%
\rule{4pt}{4pt}%
\end{center}}
\contentsmargin{0pt}
\titlecontents{section}[1.8pc]
{\addvspace{3pt}\bfseries}
{\contentslabel[\thecontentslabel.]{1.8pc}}
{}
{\quad\thecontentspage}
\titlecontents*{subsection}[1.8pc]
{\small}
{\thecontentslabel. }
{}
{, \thecontentspage}
[.---][.]
\addtolength{\topmargin}{-3pc}
\addtolength{\textwidth}{6pc}
\addtolength{\oddsidemargin}{-2pc}
\addtolength{\textheight}{7pc}
\raggedright
\parindent1em
\parskip0pt
\begin{document}
\maketitle
\tableofcontents
\begin{note}
Please, report any issues you find on
\texttt{https://github.com/jbezos/titlesec/issues}, which is better
than just complaining on an e-mail list or a web forum. Forking
and pull requests are welcome.
\end{note}
\section{Introduction}
This package is essentially a replacement---partial or total---for the
\LaTeX{} macros related with sections---namely titles, headers and
contents. The goal is to provide new features unavailable in current
\LaTeX{}; if you just want a more friendly interface than that of
standard \LaTeX{} but without changing the way \LaTeX{} works you may
consider using \textsf{fancyhdr}, by Piet van Oostrum, \textsf{sectsty},
by Rowland McDonnell, and \textsf{tocloft}, by Peter Wilson, which you
can make pretty things with.\footnote{Since the sectioning commands
are rewritten, their behaviour could be somewhat different
in some cases.}
Some of the new features provided are:
\begin{itemize}
\item Different classes and ``shapes'' of titles, with tools for very
fancy formats. You can define different formats for left and right
pages, or numbered and unnumbered titles, measure the width of the
title, add a new section level, use graphics, and many more. The
Appendix shows a good deal of examples, so jump forward right now!
\item Headers and footers defined with no |\...mark| intermediates,
and perhaps containing top, first \emph{and} bot marks at the same time.
Top marks correctly synchronized with titles, without
incompatibilities with the float mechanism. Decorative elements easily
added, including picture environments.
\item Pretty free form contents, with the possibility of grouping
entries of different levels in a paragraph or changing the format
of entries in the middle of a document.
\end{itemize}
At the time of this writing, \textsf{titlesec} works with the standard
classes and should work with many others, including the AMS ones,
although you should be aware the latter reimplement the original
internal commands. These changes will be lost here.
\begin{warning}
Compatibility with \textsf{hyperref} has been tested in typical use
cases, but it is an unsupported feature. For instance, when
using |\titleclass|, PDF bookmarks may not be properly nested, and
|\nameref| might not function as expected. Furthermore, since
\textsc{hyperref} is actively developed, currently working features
may become non-functional over time. I regret these ‘known
issues’ are unlikely to be addressed in the near future.
It is not compatible with \textsf{memoir} or \textsf{KOMA}, which
provide their own tools.
\end{warning}
As usual, load the package in the standard way with
|\usepackage|. Then, redefine the sectioning commands with the
simple, predefined settings (see section ``Quick Reference'') or with
the provided commands if you want more elaborate formats (see section
``Advanced Interface.'') In the latter case, you only need to
redefine the commands you'll use. Both methods are available at the
same time, but because |\part| is usually implemented in a
non-standard way, it remains untouched by the simple settings and
should be changed with the help of the ``Advanced Interface.''
\section{Quick Reference}
%~~~~~~~~~~~~~~~~~~~~~~
The easiest way to change the format is by means of a set of package
options and a couple of commands. If you feel happy with the
functionality provided by this set of tools, you need not go
further in this manual. Just read this section and ignore the
subsequent ones.
\subsection{Format}
There are three option groups controlling font, size and align. You
need not set all of these groups, since a default is provided for each
one; however, you must use at least an option from them if you want
this ``easy setup''.
\begin{desc}
|rm sf tt md bf up it sl sc|
\end{desc}
Select the corresponding family/series/shape. Default is |bf|.
\begin{desc}
|big medium small tiny|
\end{desc}
Set the size of titles.
Default is |big|, which gives the size of standard classes.
With |tiny|, sections (except chapters) are typed in the text
size. |medium| and |small| are intermediate layouts.
\begin{desc}
|raggedleft center raggedright|
\end{desc}
Control the alignment.
\subsection{Spacing}
\begin{desc}
|compact|
\end{desc}
This option is independent from those above and reduces the spacing
above and below the titles.
\subsection{Uppercase}
\begin{desc}
|uppercase|
\end{desc}
\fbox{2.9} Uppercases titles. Depending on the class, it might not work in
\verb|\chapter| and \verb|\part|.
\subsection{Tools}
\begin{desc}
|\titlelabel{<label-format>}|
\end{desc}
Changes the label format in sections, subsections, etc. A
|\thetitle| command is provided which is respectively |\thesection|,
|\thesubsection|, etc. The default value in standard classes is
\begin{verbatim}
\titlelabel{\thetitle\quad}
\end{verbatim}
and you may add a dot after the counter simply with
\begin{verbatim}
\titlelabel{\thetitle.\quad}
\end{verbatim}
That was done in this document.
\begin{desc}
|\titleformat*{<command>}{<format>}|
\end{desc}
This command allows to change the |<format>| of a sectioning
command, as for example:
\begin{verbatim}
\titleformat*{\section}{\itshape}
\end{verbatim}
\section{Advanced Interface}
%~~~~~~~~~~~~~~~~~~~~~~~~
Two commands are provided to change the title format. The first one
is used for the ``internal'' format, i.e., shape, font, label\dots,
the second one defines the ``external'' format, i.e., spacing before
and after, indentation, etc. This scheme is intended to easy
definitions, since in most of cases you will want to modify either
spacing or format.\footnote{Information is ``extracted'' from the
class sectioning commands, except for chapter and part. Standard
definitions with |\cs{@startsection}| are presumed---if sections have
been defined without that macro, arbitrary values for the format an
the spacing are provided, which you may change later. (Sadly, there
is no way to catch the chapter or part formats, and one similar to
that of standard classes will be used.)} That redefines existing
sectioning commands, but does not create \emph{new} ones. New
sectioning levels can be added with |\titleclass|, as described below,
and then their format can be set with the commands described here.
\subsection{Format}
A set of shapes is provided, which controls the basic distribution of
elements in the title. The available shapes are:
\begin{description}
\item[hang] is the default value, with a hanging label. (Like the
standard |\section|.)
\item[block] typesets the whole title in a block (a paragraph) without
additional formatting. Useful in centered titles\,\footnote{The label
will be slightly displaced to the left if the title is two or more
lines long and the \texttt{hang} shape is used, except with explicit
|\string\\|.} and special formatting (including graphic tools such as
|picture|, |pspicture|, etc.)
\item[display] puts the label in a separate paragraph. (Like the
standard |\chapter|.)
\item[runin] A run-in title, like the standard
|\paragraph|.\footnote{Well, not quite. The title is first boxed to
avoid some unexpected results if, for example, there is a
\texttt{\string\color} between the title and the
text. Unfortunately, due to an optimization done by \TeX{}
discretionaries may be lost. I have found no solution, except using
\textsf{luatex}, which works as one could expect. Anyway, if the
title doesn't contain hyphen or dashes, this is not usually a real
problem.}
\item[leftmargin] puts the title at the left margin. Titles at the
very end of a page will be moved to the next one and will not stick
out in the bottom margin, which means large titles can lead to
underfull pages.\footnote{However, floats following the title a couple
of lines after will interfere with the page breaking used here and
sometimes the title may stick out.} In this case you may increase the
stretchability of the page elements, use |\raggedbottom| or use the
package option |nobottomtitles| described below. Since the mechanism
used is independent from that of the margin pars, they can overlap.
A deprecated synonymous is |margin|.
\item[rightmargin] is like |leftmargin| but at the right margin.
\item[drop] wraps the text around the title, provided the
first paragraph is longer than the title (if not, they overlap).
The comments in |leftmargin| also apply here.
\item[wrap] is quite similar to drop. The only difference is
while the space reserved in drop for the title is fixed, in wrap is
automatically readjusted to the longest line. The limitations
explained below related to |calcwidth| also apply here.
\item[frame] Similar to display, but the title will be framed.
\end{description}
Note, however, some shapes do not make sense in chapters and
parts.
\begin{desc}
|\titleformat{<command>}[<shape>]{<format>}{<label>}{<sep>}{<before-code>}[<after-code>]|
\end{desc}
Here
\begin{itemize}
\item |<command>| is the sectioning command to be redefined, i.e.,
|\part|,
|\chapter|, |\section|, |\subsection|, |\subsubsection|, |\paragraph|
or |\subparagraph|.
\item The paragraph shape is set by |<shape>|, whose possible
values are those described above.
\item |<format>| is the format applied to the whole
title---label and text. This part can contain vertical material (and
horizontal with some shapes) which is typeset just after the space
above the title.
\item The label is defined in |<label>|. You may leave it empty if
there is no section label at that level, but this is not recommended
because by doing so the number is not suppressed in the table of
contents and running heads.
\item |<sep>| is the horizontal separation between label and title
body and must be a length (it must not be empty). This space is
vertical in |display| shape; in |frame| it is the distance from text
to frame. Both |<label>| and |<sep>| are ignored in starred versions
of sectioning commands. If you are using |picture| and the like, set
this parameter to 0 pt.
\item |<before-code>| is the code preceding the title body. The very last
command can take an argument, which is the title
text.\footnote{Remember font size can be changed safely between
paragraphs only, and changes in the text should be made local with a
group; otherwise the leading might be wrong---too large or too small.}
However, with the package option \texttt{explicit} the title must be
given explicitly with |#1| (see below). Penalties in this argument may
lead to unexpected results.
\item |<after-code>| is the code following the title body. The typeset
material is in vertical mode with |hang|, |block| and |display|; in
horizontal mode with |runin| and |leftmargin| (\fbox{2.7} with the
latter, at the beginning of the paragraph). Otherwise is ignored.
Penalties in this argument may lead to unexpected results.
\end{itemize}
Penalties, marks and the like must be properly synchronized with page
breaks. So, |<before-code>| and |<after-code>| are \textit{not} the
proper places for penalties. See |\sectionbreak| below.
\begin{warning}
Because of the way colors are internally ‘stacked’ you can find some
anomalous results if |\color| is used in some shapes, namely |wrap|,
|drop|, |leftmargin| and |rightmargin|. Either wrap the text with
|\textcolor| or place the code inside a group.
\end{warning}
\begin{desc}
|\chaptertitlename|
\end{desc}
It defaults to |\chaptername| except in appendices where it
is |\appendixname|. Use it instead of |\chaptername| when defining
a chapter.
\subsection{Spacing}
\begin{desc}
|\titlespacing*{<command>}{<left>}{<before-sep>}{<after-sep>}[<right-sep>]|
\end{desc}
The starred version kills the indentation of the paragraph
following the title, except in |drop|, |wrap| and |runin| where this
possibility does not make sense.
\begin{itemize}
\item |<left>| increases the left margin, except in the |...margin|,
and |drop| shapes, where this parameter sets the title width, in
|wrap|, the maximum width, and in |runin|, the indentation just before
the title. With negative value the title overhangs.\footnote{This
parameter is not equal to |<indent>| of |\cs{@startsection}|, which
doesn't work correctly. With a negative value in the latter and if
|<indent>| is larger than the label width, the first line of the title
will start in the outer margin, as expected, but the subsequent lines
will not; worse, those lines will be shortened at the right margin.}
\item |<before-sep>| is the vertical space before the title. The
standard |\chapter| inserts it with a |\vspace*|, and so does
\textsf{titlesec}; this means you have to take into account other
possible spaces inserted by \LaTeX\ at the op of the page.
\item |<after-sep>| is the separation between title and text---vertical
with |hang|, |block|, and |display|, and horizontal with |runin|,
|drop|, |wrap| and |...margin|. By making the value negative, you may
define an effective space of less than |\parskip|.\footnote{See
Goossens, Mittelbach and Samarin: \textit{The \LaTeX{} Companion},
Reading, Addison Wesley, 1993, p.~25.}
\item The |hang|, |block| and |display| shapes have the
possibility of increasing the |<right-sep>| margin with this optional
argument.
\end{itemize}
If you dislike typing the full skip values, including the |plus| and
|minus| parameters, an abbreviation |*|$n$ is provided. In the
|<before-sep>| argument this is equivalent to $n$ |ex| with some
stretchability and a minute shrinkability. In the
|<after-sep>| some stretchability (smaller) and no shrinkability.%
\footnote{They stand for $n$ times |1ex plus .3ex minus .06ex| and
|1ex plus .1ex|, respectively.} Thus, you can write
\begin{verbatim}
\titlespacing{\section}{0pt}{*4}{*1.5}
\end{verbatim}
The lengths |\beforetitleunit| and |\aftertitleunit| are used
as units in the |*| settings and you can change them if you do not like
the predefined values (or for slight changes in the makeup, for
example).
\begin{note}
|\titlespacing| does not work with either |\chapter| and |\part|
unless you change its title format as well by means of
|\titleformat|, the simple settings, or |\titleclass|.
\end{note}
\begin{note}
Arguments in |\titlespacing| must be dimensions; |\stretch| includes
a command and hence raises an error.
\end{note}
\subsection{Spacing related tools}
These commands are provided as tools for |\titleformat| and
|\titlespacing|.
\begin{desc}
|\filright \filcenter \filleft \fillast \filinner \filouter|
\end{desc}
Variants of the |\ragged...| commands, with slight differences. In
particular, the |\ragged...| commands kills the left and right spaces
set by |\titlespacing|.\footnote{Remember the package
\textsf{ragged2e} provides some additional commands for alignment,
too, like \texttt{\string\justifying}.} |\fillast| justifies the
paragraph, except the last line which is
centered.\footnote{Admittedly, a weird name, but it is short.} These
commands work in the |frame| label, too.
|\filinner| and |\filouter| are |\filleft| or |\filright| depending
on the page. Because of the asynchronous \TeX{} page
breaking, these commands can be used in |\chapter| only.
If you want a general tool to set different formats depending
on the page, see ``Extended settings'' below.
\begin{desc}
|\wordsep|
\end{desc}
The inter-word space for the current font.
\begin{desc}
|indentafter noindentafter| \quad (Package options)
\end{desc}
By-pass the settings for all of sectioning commands.%
\footnote{Formerly |indentfirst| and |nonindentfirst|, now
deprecated.}
\begin{desc}
|rigidchapters rubberchapters| \quad (Package options)
\end{desc}
With |rigidchapters| the space for chapter titles is always the same,
and |<after-sep>| in |\titlespacing| does not mean the space from the
bottom of the text title to the text body as described above, but from
the \textit{top} of the text title, i.e., |<before-sep>| $+$
|<after-sep>| now is a fixed distance from the top of the page body to
the main text. The default is |rubberchapters| where |<after-sep>| is
the separation between title and text as usual. Actually, the name is
misleading because it applies not only to the default chapter, but to
any title of top class. (More on classes below.)
\begin{desc}
|bottomtitles nobottomtitles nobottomtitles*| \quad (Package options)
\end{desc}
If |nobottomtitles| is set, titles close to the bottom margin will
be moved to the next page and the margin will be ragged. The minimal
space required in the bottom margin not to move the title is set
(approximately) by
\begin{verbatim}
\renewcommand{\bottomtitlespace}{<length>}
\end{verbatim}
whose default value is |.2\textheight|. A simple ragged bottom on the
page before is obtained with a value of 0 pt. |bottomtitles| is the
default, which simply sets |\bottomtitlespace| to a negative value.
The |nobottomtitles*| option provides more accurate computations but
titles of |margin|, |wrap| or |drop| shapes could be badly
placed. Usually, you should use the starred version.
\begin{desc}
|aftersep largestsep| \quad (Package options)
\end{desc}
By default, when there are two consecutive titles the |<after-sep>|
space from the first one is used between them. Sometimes this is not
the desired behaviour, especially when the |<before-sep>| space is much
larger than the |<after-sep>| one (otherwise the default seems
preferable). With |largestsep| the largest of them is used. Default is
|aftersep|. [\textbf{Note.} |largestsep| may not work as documented.
This is a known issue.]
\begin{desc}
|\\ \\*|\\
|pageatnewline| \quad (Package option)
\end{desc}
\fbox{2.6} In version 2.6 and later, \verb|\\| does not allow a page
break and therefore is equivalent to \verb|\\*|. Since I presume none
wants a page break inside a title, this has been made the default. If
for some strange reason you want to allow page breaks inside the
titles, use the package option \verb|pageatnewline|, which is provided
for backward compatibility.
\begin{desc}
|\nostruts|\\
|nostruts| \quad (Package option)
\end{desc}
\fbox{2.11} The styles defined by \textsf{titlesec} insert some struts
at certain places to make sure the vertical space is the same with
relation with the baseline. This is not always the desired behavior, so
the package options |nostruts| is provided. An alternative is the macro
|\nostruts| when defining a section (note this macro is defined only
within a title).
\subsection{Rules}
The package includes some tools for helping in adding rules and other
stuff below or above the title. Since the margins in titles may be modified,
these macros take into account the local settings to place rules properly.
They also take into account the space used by marginal titles.
\begin{desc}
|\titleline[<align>]{<horizontal material>}|\\
|\titlerule[<height>]|\\
|\titlerule*[<width>]{<text>}|
\end{desc}
The |\titleline| command allows inserting a line, which may
contain text and other ``horizontal'' material. It is intended
mainly for rules and leaders but in fact is also useful for other
purposes. The line has a fixed width and hence must be filled, i.e.,
|\titleline{CHAPTER}| produces an underfull box. Here the optional
|<align>| (|l|, |r| or |c|) helps, so that you simply type, say,
|\titleline[c]{CHAPTER}|.%
\footnote{The default is the \texttt{s} parameter of the
\texttt{\cs{makebox}} command.}
Using |\titleline| in places where vertical material is not expected
can lead to anomalous results. In other words, you can use it in the
|<format>| (always) and |<after-code>| (|hang|, |display| and |block|)
arguments; and in the |display| shape at the very beginning of the
|<before-code>| and |<label>| argument as well. But try it out, because
very likely it works in other places.
The |\titlerule| command, which is enclosed automatically in
|\titleline| if necessary, can be used to build rules and
fillers. The unstarred
version draws rules of height .4 pt, or |<height>| if present.
For example:
\begin{verbatim}
\titlerule[.8pt]%
\vspace{1pt}%
\titlerule
\end{verbatim}
draws two rules of different heights with a separation of
1 pt.
The starred version makes leaders with the |<text>|
repeated in boxes of its natural width. The width of the boxes
can be changed to |<width>|, but the first box remains with
its natural width so that the |<text>| is aligned to the left
and right edges of the space to be filled.
For instance, with
\begin{verbatim}
\titleformat{\section}[leftmargin]
{\titlerule*[1pc]{.}%
\vspace{1ex}%
\bfseries}
{... definition follows
\end{verbatim}
leaders spanning over both main text and title precede the section.
\begin{desc}
|calcwidth| \quad (Package option)
\end{desc}
The |wrap| shape has the capability of measuring the lines in the title
to format the paragraph. This capability may be extended to other
three shapes---namely |display|, |block| and |hang|---with this
package option. The length of the longest line is returned in
|\titlewidth|.\footnote{There are two further
parameters, |\string\titlewidthfirst| and |\string\titlewidthlast|,
which return the length of the first and last lines. There are not
specific tools for using them, but you can assign their values to
|\string\titlewidth| and then use |\string\titleline*|.}
As far as \TeX{} is concerned, any box is considered typeset material.
If the box has been enlarged with blank space, or if conversely a box
with text has been smashed, the value of |\titlewidth| may be wrong
(as far as humans is concerned). The |hang| shape, for instance, uses
internally such a kind of boxes, but in this case this behaviour is
desired when the title is flushed right; otherwise the |block| shape
produces better results. In other words, using boxes whose natural
width has been overridden may be wrong.\footnote{Which include
justified lines, whose interword spacing has been enlarged.} Further,
some commands may confuse \TeX{} and stop parsing the title. But if
you stick to text, |\\| and |\\[...]| (and it is very unlikely you
might want something else), there will be no problems.
Another
important point is the |<before-code>|, |<label>|, |<sep>|, and
|<title>| parameters (but not |<after-code>|) are evaluated twice at local
scope; if you increase a counter \emph{globally}, you are increasing
it twice. In most of cases, placing the conflicting assignment in the
|<after-code>| parameter will be ok, but alternativey you can use the
following macro.
\begin{desc}
|\iftitlemeasuring{<true>}{<false>}|
\end{desc}
\fbox{2.9} When the title is being measured (first pass), the |<true>|
branch is used, and when the title is actually typeset (second pass)
the |<false>| branch is used.
\begin{desc}
|\titleline*[<align>]{<horizontal material>}|
\end{desc}
A variant of |\titleline| to be used only with |calcwidth|.
The text will be enclosed first in a box of width |\titlewidth|; this box
will be in turn enclosed in the main box with the specified alignment.
There is no equivalent |\titlerule| and therefore you must enclose
it explicitly in a |\titleline*| if you want the |\titlewidth| to
be taken into account:
\begin{verbatim}
\titleline*[c]{\titlerule[.8pc]}
\end{verbatim}
\subsection{Page styles}
\fbox{2.8} You can assign a page style to levels of class |top| and
|page|, as well as the default chapter with the following command:%
\footnote{Named in the short-lived version 2.7 as
\texttt{\string\titlepagestyle}.}
\begin{desc}
|\assignpagestyle{<command>}{<pagestyle>}|
\end{desc}
For example, to suppress the page number in chapters write:
\begin{verbatim}
\assignpagestyle{\chapter}{empty}
\end{verbatim}
\subsection{Breaks}
\begin{desc}
|\sectionbreak \subsectionbreak \subsubsectionbreak|\\
|\paragraphbreak \subparagraphbreak \<section>break|
\end{desc}
By defining these command with |\newcommand| different page breaks
could be applied to different levels. In those undefined, a penalty
with the internal value provided by the class is used (typically
$-300$). For instance,
\begin{verbatim}
\newcommand{\sectionbreak}{\clearpage}
\end{verbatim}
makes sections begin a new page. In some layouts, the space
above the title is preserved even if the section begins a new
page; that's accomplished with:
\begin{verbatim}
\newcommand{\sectionbreak}{%
\addpenalty{-300}%
\vspace*{0pt}}
\end{verbatim}
\fbox{2.6} \verb|\<section>break| is available in the \verb|top| class,
too. Suitable values would be \verb|\cleardoublepage| (the default if
\verb|openright|) and \verb|\clearpage| (the default if
\verb|openany|). Thus, you can override \verb|openright| by defining
\verb|\chapterbreak| as \verb|\clearpage|, provided its class has been
changed to \verb|top| (in this example, parts will continue with the
\verb|openright| setting).
Note these macros apply the penalties at the right place. In other
words, penalties in |<before-code>| and |<after-code>| can lead to
unexpected (and even weird) results.
\begin{desc}
|\chaptertolists|
\end{desc}
\fbox{2.6} If defined, the usual white space written to lists (ie,
List of Figures and List of Tables) is replaced by the code in this
command. If you do not want the white space when a chapter begins,
define it to empty, i.e.,
\begin{verbatim}
\newcommand{\chaptertolists}{}
\end{verbatim}
This command is not a general tool to control
spacing in lists, and is available only in titles of top class, so
it will not work with the default chapters except if you change their
class (on the other hand, |\...tolists| can be used in any title whose
class is top).
\subsection{Other Package Options}
\begin{desc}
|explicit| \quad (Package option)
\end{desc}
\fbox{2.7} With it, the title is not implicit after |<before-code>| but
must be given explicitly with |#1| as in, for example:
\begin{verbatim}
\titleformat{\section}
{..}
{\thesection}{..}{#1.}
\end{verbatim}
(Compare it with the example in section \ref{sec:dotafter}.)
\begin{desc}
|newparttoc oldparttoc| \quad (Package options)
\end{desc}
Standard parts write the toc entry number in a non standard way.
You may change that with |newparttoc| so that \textsf{titletoc}
or a similar package can manipulate the entry. (That works only if
|\part| has been redefined.)
\begin{desc}
|clearempty| \quad (Package options)
\end{desc}
Modifies the behaviour of |\cleardoublepage| so that the |empty| page
style will be used in empty pages.
\begin{desc}
|toctitles| \quad (Package option)
\end{desc}
\fbox{2.6} Changes the behaviour of the optional argument in
sectioning titles so that it sets only the running heads and not the
TOC entries, which will be based on the full title.
\begin{desc}
|newlinetospace| \quad (Package option)
\end{desc}
\fbox{2.6} Replaces every occurrence of \verb|\\| or \verb|\\*| in
titles by a space in running heads and TOC entries. This way, you do
not have to repeat the title just to remove a formatting command. Note
with |\chapter| and |\part| you need to set the title class.
\begin{desc}
|notocpart*| \quad (Package option)
\end{desc}
\fbox{2.10.1} Long ago (by the year 2000) I decided for some reason
\verb|\part*| would behave like the AMS classes and therefore there
should be a contents entry for it. This is somewhat odd, indeed, but
the very fact is nobody has complained until now! On the other hand,
restoring the behaviour one could expect after 15 years doesn't seem a
good idea. This option restores the standard behaviour.
\subsection{Extended Settings}
%~~~~~~~~~~~~~~~~~~~~~~~~~
The first argument of both |\titleformat| and |\titlespacing| has an
extended syntax which
allows to set different formats depending on the context.\footnote{%
The \textsf{keyval} package is required for making use of it.} This
argument can be a list of key/value pairs in the form:
\begin{desc}
|<key>=<value>, <key>=<value>, <key>, <key>,...|
\end{desc}
Currently, only pages and unnumbered versions are taken care of,
besides the sectioning command name. Thus, the available keys are:
\begin{itemize}
\item |name|. Allowed values are |\chapter|, |\section|, etc.
\item |page|. Allowed values are |odd| or |even|.
\item |numberless|. A valueless key. It is not necessary unless you
want to set different numbered (without this key) and unnumbered
(with |numberless|) variants.
\end{itemize}
The basic form described above with the name of a sectioning
command, say
\begin{verbatim}
\titleformat{\section} ...
\end{verbatim}
is in fact an abbreviation for
\begin{verbatim}
\titleformat{name=\section} ...
\end{verbatim}
Let's suppose we'd like a layout with titles in the
outer margin. We might set something like
\begin{verbatim}
\titleformat{name=\section,page=even}[leftmargin]
{\filleft\scshape}{\thesection}{.5em}{}
\titleformat{name=\section,page=odd}[rightmargin]
{\filright\scshape}{\thesection}{.5em}{}
\end{verbatim}
Since the page information is written to the |aux| file, at
least two runs are necessary to get the desired result.
The ``number'' version is usually fine when generating unnumbered
variants since removing the label is the only change required in most
cases, but if you need some special formatting, there is the
|numberless| key which defines an alternative version for sections
without numbers (namely those with level below |secnumdepth|, in the
front and back matters and, of course, the starred version). For
instance
\begin{verbatim}
\titleformat{name=\section}{...% The normal definition follows
\titleformat{name=\section,numberless}{...% The unnumbered
% definition follows
\end{verbatim}
Neither |<label>| nor |<sep>| are ignored in |numberless|
variants.
These keys are available to both |\titleformat| and
|\titlespacing|. By using |page| in one (or both) of them, odd and
even pages will be formatted differently. Actually,
``even'' and ``odd'' are well established \LaTeX{} terms, but
misleading. In one side printing the ``odd'' pages refer
to ``even'' pages as well (cf.\@ |\oddsidemargin|.)
If you intend to create different odd/even \emph{and}
different numbered/unnumbered versions, it is recommended defining
the four variants.
If you remove the page specifier from a sectioning command you
must remove the |.aux| file.
\subsection{Creating new levels and changing the class}
While the shapes and the like modify the behaviour of titles related
to the surrounding text, title classes allow to change the generic
behaviour of them. With the help of classes you may insert, say, a
new |subchapter| level between |chapter| and |section|, or creating a
scheme of your own.
There are three classes: |page| is like the book |\part|, in a single
page, |top| is like |\chapter|, which begins a page and places the
title at the top, and |straight| is intended for titles in the middle
of text.\footnote{There is an further class named |part| to emulate the
article |\cs{part}|, but you should not use it at all. Use the
|straight| class instead. Remember some features rely in these classes
and \textsf{titlesec} does not change by default the definition of
\texttt{\string\part} and \texttt{\string\chapter}.}
\begin{warning}
Making a consistent scheme and defining all of
related stuff like counters, macros, format, and spacing is left to the
responsibility of the user. If there is a TOC, also the TOC format,
which might require redefining the corresponding macros to change the
hardcoded value (either by hand or with \textsf{titletoc}).
\end{warning}
\begin{desc}
|\titleclass{<name>}{<class>}|\\
|\titleclass{<name>}{<class>}[<super-level-cmd>]|
\end{desc}
If you do not use the optional argument, you just change
the |<class>| of |<name>|. For example:
\begin{verbatim}
\titleclass{\part}{straight}
\end{verbatim}
makes |part| of |straight| class.
When the second form is used, the level number is the following of
|<super-level-cmd>|. For example:
\begin{verbatim}
\titleclass{\subchapter}{straight}[\chapter]
\newcounter{subchapter}
\renewcommand{\thesubchapter}{\Alph{subchapter}}
\end{verbatim}
creates a level under chapter (some additional code is shown as well,
but you must add to it the corresponding |\titleformat| and
|\titlespacing| settings).\footnote{Regarding counters, the
\textsf{remreset} package can be useful.}
If the chapter level is 0, then the subchapter one is 1; the levels
below are increased by one (section is 2, subsection is 3, and so on).
There are two sectioning commands which perform some extra actions
depending on its name and ignoring the class:
\begin{itemize}
\item |\chapter| logs the string defined in |\chaptertitlename|
and the matter is taken into account.
\item |\part| does not encapsulate the label in the toc entry,
except if you use the |newparttoc| option.
\end{itemize}
\begin{warning}
If you set or reset the class, you may need to redefine the format,
too.
\end{warning}
\begin{desc}
|loadonly| \quad (Package option)
\end{desc}
Let us suppose you want to create your sectioning commands from scratch.
This package option ignores any previous definitions, if any,
and hence removes the possibility of using the options described
in ``Quick Reference''. Then you use the \textsf{titlesec}
tools, and define the corresponding counters and labels.
\begin{desc}
|\titleclass{<name>}[<start-level-num>]{<class>}|
\end{desc}
Here, the |<name>| title is considered the top level, with number
|<start-level-num>| (typically 0 or $-$1). It should be used only when
creating sectioning commands from scratch with the help of |loadonly|,
and there must be exactly one (no more, no less) declaration of this
kind. After it, the rest of levels are added as explained above.
\section{Additional Notes}
%~~~~~~~~~~~~~~~~~~~~~~~~
Only a |\footnote| command is allowed in titles, but it will work.
However, the footnote mark is not removed from toc entries or running
headings; for that use
\begin{verbatim}
\usepackage[stable]{footmisc}
\end{verbatim}
The rest of this section describes briefly some \LaTeX{} commands,
useful when defining sectioning titles.
\subsection{Fixed Width Labels}
The |\makebox| command allows to use fixed width label, which
makes the left margin of the actual title (not the label) to lie
in the same place. For instance (only the relevant code is
provided):
\begin{verbatim}
\titleformat{\section}
{..}
{\makebox[2em]{\thesection}}{..}{..}
\end{verbatim}
See your \LaTeX{} manual for further reference on boxing commands.
\subsection{Starred Versions}
\label{s:starred}
Using sectioning commands in the starred version is strongly
discouraged. Instead, you can use a set of markup oriented commands
which are easy to define and modify, if necessary. Thus, you can test
different layouts before choosing amongst them.
Firstly remember if you say
\begin{verbatim}
\setcounter{secnumdepth}{0}
\end{verbatim}
sections will not be numbered but they will be included in both toc
and headers.
Now, let's
suppose you want to include some sections with a special content;
for example, a section (or more) with exercises. We will use an
environment named |exercises| whose usage is:
\begin{verbatim}
\section{A section}
Text of a normal section.