In the following, we will disscuss something about titlesec package.
Typos
Page 2:
1 2 3 4 5 6 7
if sections have been defined without that macro, arbitrary values for the format (an) the spacing are provided.
--> if sections have been defined without that macro, arbitrary values for the format (and) the spacing are provided,
Exceptions
Two sec commands are special:
\chapter
\part
These 2 commands need more attention when you need to change their style.
Shapes
The test code snippet as below:
1 2 3 4 5 6 7 8
\titleformat{\section}[⟨shape⟩]{\sffamily}{\thesection}{0pt}{}[] \section{SECOND} The prior examples {redefined existing} sectional unit title. commands. This defines a new one, illustrating the needed counter and macros to display that counter. commands This defines a new one, illustrating the needed counter and macros to display that counter illustrating the needed defines a new one, illustrating the needed counter and macros to display that counter.
Different section (title) shape example:
display:
block:
runin(插入):
leftmargin: Puts the title at the left margin. Since the mechanism used is independent from that of the margin pars, they can overlap. A deprecated synonymous is margin. (useful in twoside layout)
rightmargin: Similar as the above (useful in twoside layout)
drop: the space reserved in drop for the title is fixed
wrap: the space automatically readjusted to the longest line
frame: framed + display
hang: is the default value, with a hanging label. (Like the standard \section.)
Format
syntax
Syntax of \titleformat as below, we call SECNAME by text, 1 by label in section title: 1.SECNAME
1 2 3 4 5 6 7 8 9 10
\titleformat{⟨command⟩} [⟨shape⟩] {⟨format⟩} % apply to both label and text {⟨label⟩} % \thesection, \thesubsection, etc. {⟨sep⟩} % horizontal space between label and text % (1. vertical in 'display' shape; % (2. in 'frame' it is the distance from text to frame; {⟨before-code⟩} % code before the text [⟨after-code⟩] % code after the text:
For code before and after, sec title is typeset in (c.f. ⟨sep⟩):
vertical mode for hang, block and display;
horizontal mode for runin and leftmargin;
Some Note about these arguments:
⟨label⟩ and ⟨sep⟩ are ignored in starred versions of sectioning commands.
⟨before-code⟩ and ⟨after-code⟩ are not the proper places for penalties, see command \sectionbreak.
All of the arguments is within a sec group, something like:
The horizontal mode and vertical mode test is similar to that in ⟨after-code⟩, see example:
1 2 3 4 5 6 7 8
\titleformat{\section}[display]{\sffamily}{\thesection}{0pt}{X\vskip3em X}[] \section{SECOND} The prior examples {redefined existing} sectional unit title. commands. This defines a new one, illustrating the needed counter and macros to display that counter. commands This defines a new one, illustrating the needed counter and macros to display that counter illustrating the needed defines a new one, illustrating the needed counter and macros to display that counter.
output:
You can use a command that absorb 1 argument to handle the text in section body, see example:
1 2 3 4 5 6 7 8 9 10
\def\testcmd#1{\textbf{(#1)}} \titleformat{\section}[hang]{\sffamily}{\thesection}{0pt}{\testcmd}[] \section{SECOND} The prior examples {redefined existing} sectional unit title. commands. This defines a new one, illustrating the needed counter and macros to display that counter. commands This defines a new one, illustrating the needed counter and macros to display that counter illustrating the needed defines a new one, illustrating the needed counter and macros to display that counter.
output:
If you load the explicit package option, the you should pass #1 to your own function, c.f. the \testcmd command above.
after-cdoe
An example about ⟨after-code⟩ in each mode:
1 2 3 4 5 6 7 8 9
% horizontal mode \titleformat{\section}[runin]{\sffamily}{\thesection}{0pt}{}[X\hskip4em X] \section{SECOND} The prior examples {redefined existing} sectional unit title. commands. This defines a new one, illustrating the needed counter and macros to display that counter. commands This defines a new one, illustrating the needed counter and macros to display that counter illustrating the needed defines a new one, illustrating the needed counter and macros to display that counter.
output:
Then test this code in vertical mode:
1 2 3 4 5 6 7 8 9
% vertical mode \titleformat{\section}[hang]{\sffamily}{\thesection}{0pt}{}[X\vskip4em X] \section{SECOND} The prior examples {redefined existing} sectional unit title. commands. This defines a new one, illustrating the needed counter and macros to display that counter. commands This defines a new one, illustrating the needed counter and macros to display that counter illustrating the needed defines a new one, illustrating the needed counter and macros to display that counter.
output:
Spacing
Syntax
There are two ways to set these spacing, the fisrt is use command:
1 2 3 4 5 6 7 8 9 10
\titlespacing*{⟨command⟩} {⟨left⟩} % left margin, except in the: % (1. title width in '..margin', and 'drop' shapes % (2. maximum width in 'wrap' % (3. indentation before the whole title {⟨before-sep⟩} % vertical space before the title {⟨after-sep⟩} % separation between title and text: % (1. vertical with 'hang', 'block', 'display'; % (2. horizontal with 'runin', 'drop', 'wrap' and '...margin'; [⟨right-sep⟩]
examples
See example:
1 2 3 4 5 6 7 8 9 10
XXX-YYY \titleformat{\section}[hang]{\bfseries}{\thesection}{0pt}{}[] \titlespacing*{\section}{0em}{2em}{2em} \section{SECOND} The prior examples {redefined existing} sectional unit title. commands. This defines a new one, illustrating the needed counter and macros to display that counter. commands This defines a new one, illustrating the needed counter and macros to display that counter illustrating the needed defines a new one, illustrating the needed counter and macros to display that counter.
output:
Example of runin shape:
1 2 3 4 5 6 7 8 9 10 11
XXX-YYY \titleformat{\section}[runin]{\bfseries}{\thesection}{0pt}{}[] \titlespacing*{\section}{1em}{2em}{2em} \section{SECOND} The prior examples {redefined existing} sectional unit title. commands. This defines a new one, illustrating the needed counter and macros to display that counter. commands This defines a new one, illustrating the needed counter and macros to display that counter illustrating the needed defines a new one, illustrating the needed counter and macros to display that counter.
output:
There is an syntax abbreviation *n in {⟨before-sep⟩} and {⟨after-sep⟩}, which stands for n times of:
1 2
1ex plus .3ex minus .06ex 1ex plus .1ex
respectively.
Tools
Rule
titlesec also provides command to add rule above of below sec title:
The \titlerule command, which is enclosed automatically in \titleline if necessary. Example of \titleline and \titlerule :
1 2 3 4 5 6 7 8 9 10 11 12
\titleformat{\section}[display] {\color{red}\bfseries\titlerule} % always {\thesection}{0pt} {\color{blue}\titleline[c]{AAA}} [\color{orange}\titlerule] % for 'hang', 'display', 'block'; \section{SECOND} The prior examples {redefined existing} sectional unit title. commands. This defines a new one, illustrating the needed counter and macros to display that counter. commands This defines a new one, illustrating the needed counter and macros to display that counter illustrating the needed defines a new one, illustrating the needed counter and macros to display that counter.
output:
Example of \titlerule*:
1 2 3 4 5 6 7 8 9 10 11 12
\titleformat{\section}[hang] {\bfseries\titlerule*[1pc]{.}} {\thesection}{0pt} {\titlerule*[2pc]{x}} \section{SECOND} The prior examples {redefined existing} sectional unit title. commands. This defines a new one, illustrating the needed counter and macros to display that counter. commands This defines a new one, illustrating the needed counter and macros to display that counter illustrating the needed defines a new one, illustrating the needed counter and macros to display that counter.
output:
Extend Syntax
The first argument of both \titleformat and \titlespacing has an extended syntax which allows to set different formats depending on the context. This argument can be a list of key/value pairs in the form:
name: \chapter, \section, etc.
page: Only even, odd.
numberless: set different style for numbered and unnumbered (or those with level below secnumdepth) sec commands.
The “number” version is usually fine when generating unnumbered variants since removing the label is the only change required in most cases.
An example from the doc:
Let’s suppose we’d like a layout with titles in the outer margin. We might set something like: