Sectsty (Modifica Sezionamento Documento, Titoli, Etc.)
Sectsty (Modifica Sezionamento Documento, Titoli, Etc.)
Sectsty (Modifica Sezionamento Documento, Titoli, Etc.)
2
Rowland McDonnell
rowland.mcdonnell@physics.org
Contents
1 Introduction 2
6 Some notes 6
7 Underlining 7
7.1 A detailed look at underlining . . . . . . . . . . . . . . . . . 8
7.1.1 Underlining with the ulem package . . . . . . . . . . 9
7.1.2 More notes on ulem and underlining . . . . . . . . . 11
1
1 Introduction
The secsty package provides a set of commands for changing the fount1
used for the various sectional headings in the standard LATEX 2ε document
classes: article, book, and report. This package also works with the
KOMA-Script classes scrartcl, scrbook, and scrreprt.
When I refer to sectional headings in this document, I mean the stuff
that is printed in the body of a document to indicate the start of a part,
chapter, section, and so on. Appendix headings are treated as chapter
headings.
I’d appreciate any bug reports, comments, or suggestions emailed to the
address above. I’m especially interested in any problems you might have
had understanding the documentation; and, of course, anything else you’d
like to tell me about this package. I’m certainly interested in any problems
you might have had in using sectsty with other packages, although I can’t
promise to be able to do anything useful about such problems.
The titlesec package offers a different approach to changing the appear-
ance of sectional headings. The fncychap package has a set of alternative
‘canned’ chapter headings. Both are available from CTAN.
Donald Arseneau won’t let me say what bits of sectsty he wrote, but
I’d like to thank him anyway: I couldn’t have managed without his help.
Thanks are also due to the various people who sent me bug reports – I’ve
not kept track so rather than mention some of you and miss others out,
I’ll just say thanks and leave it at that. You know who you are and I
appreciate your help.
\usepackage{sectsty}
You will then have some new commands available. For example:
\allsectionsfont{\sffamily}
2
your commands change this. This means the example above gives you
Computer Modern Sanserif Bold Extended by default.
You might want to change all headings so that they used medium italic.
Since the default headings use bold extended, you need to switch to the
medium weight as well as switching to italic using the standard LATEX fount
selection commands, like this:
\allsectionsfont{\mdseries\itshape}
You can change the style of the different sectional headings separately. If
you want \sections to be set in bold Adobe Times, you might use standard
LATEX fount selection commands like this:
\sectionfont{\fontfamily{ptm}\selectfont}
Similar commands exist to change the style of the rest of LATEX’s stan-
dard headings independently: \chapterfont{hcommandsi} will change
chapter headings, and so on. The full list of commands is given in sec-
tion 5 on page 5.
If you don’t understand the fount selection commands above, you might
like to look at the file fntguide.tex that comes with the standard LATEX
distribution.
\allsectionsfont{\sffamily\raggedright}
\subparagraphfont{\hspace*{5em}}
3
This adds an extra 5 em space before each subparagraph heading.
As a further example, you might want chapter headings set flush with
the right margin. This will do the job:
\chapterfont{\raggedleft}
If you want to play around with this sort of thing, it might be useful
to know what sort of positioning you get by default with the standard
classes: all sectional headings except chapter and part are fully justified
(text set flush with the left and right margins), and chapter headings are
raggedright. Part headings vary: they are raggedright in the article class,
and centred in the book and report classes.
\sectionfont{\nohang\centering}
will ensure that any multiple line section headings come out centred as
expected.
The second problem – the suppression of hanging indentation when
you use \\ in a raggedright or centred heading – remains unsolved. I’m
told that it’s caused by the definition of \@centercr. This is part of the
LATEX kernel and beyond my abilities to patch up. This problem has been
reported as a LATEX bug, so it might well be sorted out in a future LATEX
release.
Donald Arseneau’s ulem package makes its own arrangements in this
area, so sectional headings that are underlined using sectsty and ulem have
correct hanging indentation even if you do use \\ to end a line early.
4
should redefine the \sectfont command provided by the KOMA-script
classes. This is akin to using sectsty’s \allsectionsfont command.
For example, the default definition is:
\newcommand*\sectfont{\sffamily\bfseries}
you might want to change from this bold sanserif to medium italic roman.
To do this, you could add the following line to your document preamble:
\renewcommand*\sectfont{\rmfamily\mdseries\itshape}
5
\paragraphfont{hcommandsi} Changes the style of ‘paragraph’ headings
only by executing {hcommandsi} before printing each heading; you
should not use text positioning commands for this heading.
\nohang For use in multiple-line headings: this command stops lines be-
yond the first having hanging indentation, which looks pretty daft
with centred headings.
6 Some notes
The way sectsty works is by re-defining all the commands that produce the
sectional headings. The change arranges things so that the fount selection
commands you specify are executed immediately before the sectional head-
ing is printed. You can change sizes, for example, with the normal LATEX
size changing commands.
You can include almost any LATEX commands in the argument to one
of sectsty’s commands. Whether any particular command will work prop-
erly or have the effect you want is a different matter. If you come across
anything interesting you can do with this package that I’ve not mentioned
and you think someone else might be interested in, you could let me know
and I’ll put it in the documentation.
6
If you use long section names you might like to make all sectional head-
ings \raggedright:
\allsectionsfont{\raggedright}
This means that LATEX won’t try and justify sectional headings and should
manage to avoid a hyphentated sectional heading which tends to look pretty
bad.
If you use sectsty and get a complaint that you are using an old version
of LATEX, there’s probably no need to worry. Sectsty will probably work
correctly with any version of LATEX 2ε after June 1996; because the earliest
version I’ve tested sectsty with is the June 1998 release of LATEX, that’s the
version I’ve specified as being needed. Sectsty is not meant to work with
LATEX 2.09; it’s a good idea to get LATEX 2ε if at all possible.
7 Underlining
Some people have to underline sectional headings because of regulations
governing thesis submission and things like that. If you’re not forced to
use underlining, it’s probably best to avoid it: underlining is usually consid-
ered bad typographical practice (well-placed horizontal rules are a different
matter). On top of that, underlining sectional headings with sectsty is not
as trivial a job as I’d like – you’ve either got to get another package or
accept quite a lot of limitations.
To underline a sectional heading with sectsty, you need to put the
\underline command as the last command in a \hsectionifont command.
For example:
\usepackage{sectsty}
\allsectionsfont{\sffamily\underline}
\documentclass{article}
\usepackage{sectsty}
7
\usepackage[normalem]{ulem}
\allsectionsfont{\sffamily\underline}
Don’t forget that the \underline command must always be the last item in
the \hsectionifont command. This example uses the familiar \underline
command as before, but because ulem has been loaded, sectsty automati-
cally redefines \underline inside sectional headings to use ulem’s underline
code. The \underline command has its normal meaning outside sectional
headings, so you won’t get any nasty surprises.
I prefer the results you get using the ulem package, but you might prefer
the appearance of underlined sectional headings produced without ulem’s
help. If so, and you’re not using \paragraph or \subparagraph sections,
or section headings that need to be split over two lines, there’s no need to
use ulem.
Sectsty always plays some dirty tricks which involve re-defining the
\underline command inside sectional headings only. These tricks work
differently if ulem has been loaded, but it doesn’t matter whether you load
ulem before or after sectsty.
Despite these fun and games, the \underline command always has its
usual definition outside sectional headings and will work normally in the
rest of your document.
8
of each sectional heading: the re-definition used by sectsty should have no
effect on the rest of your document.
Assuming for the moment that you’re not using ulem, you can get a
straightforward single underline using the normal LATEX underline com-
mand like this:
\allsectionsfont{\underline}
This has a few problems. Most noticeably, it won’t allow sectional headings
to break at the end of the line, so long headings will extend beyond the right
hand edge of the text. This is because the standard LATEX \underline
command puts its argument into a box made in LR mode, so headings
underlined this way will be one line headings no matter what you do.
Another problem is that the underline rule used sits below the low-
est possible extent of the text it’s underlining, and so increases the space
between the underlined heading and the text below. This is especially
noticeable if you’re using \paragraph or \subparagraph headings. And
remember that the \underline command must be the last in this list, like
this:
\allsectionsfont{\sffamily\underline}
If you put \sffamily (or anything else) after \underline, it won’t work
properly.
\documentclass{article}
\usepackage{sectsty}
\usepackage[normalem]{ulem}
\allsectionsfont{\sffamily\raggedright\underline}
\begin{document}
\section{A very long sectional heading title that will
have to be split over two lines}
\end{document}
You’ll notice that the long section heading was split over two lines. Now
try this:
\documentclass{article}
9
\usepackage{sectsty}
\allsectionsfont{\sffamily\raggedright\underline}
\begin{document}
\section{A very long sectional heading title that will
have to be split over two lines}
\end{document}
You’ll notice that the underlining rule is positioned differently, and the
heading could not be split over two lines.
If you want to use in sectional headings some of the other types of
underlining allowed by the ulem package, you can use the \ulemheading
command, as in this example to give double underlining and raggedright
sectional headings:
\usepackage{sectsty,ulem}
\allsectionsfont{\raggedright\ulemheading{\uuline}}
The \ulemheading command is meant to be used only inside sectional
headings like this. It takes two arguments: the first argument is the ulem
package command to generate a particular style of underlining; the second
argument is the text to be underlined, and is provided by the command
that generates the sectional heading. All you have to do is provide the first
argument and ensure that you put the \ulemheading at the end of the
\hsectionifont command’s argument, as in this example to give you wavy
underlined sans-serif headings:
\usepackage{sectsty,ulem}
\allsectionsfont{\sffamily\ulemheading{\uwave}}
The full list of modifications allowed by the \ulemheading command is:
\ulemheading{\uline} single underline
\ulemheading{\uuline} double underline
\ulemheading{\uwave} wavy underline
\ulemheading{\sout} strike out with -
\ulemheading{\xout} cross out with /
It’s possible to define more by following the directions in the ulem package.
If you typeset a document that uses \ulemheading on a system where
the ulem package is unavailable, the document will be processed but
the output will be different. If you’ve not loaded the ulem package,
\ulemheading will produce underlining exactly the same as if you’d used
the \underline command: \ulemheading is defined to ignore the first ar-
gument and pass the heading text to the normal underlining code. This
means you can produce a document using ulem’s fancy underlining, and
process it on a LATEX system without ulem by making a single modifica-
tion: commenting out the \usepackage{ulem} command. The results will
of course be different, but the document will typeset.
10
7.1.2 More notes on ulem and underlining
The ulem package is available via anonymous ftp from the Comprehensive
TEX Archive Network (CTAN). You can find a list of CTAN sites and a Web
interface to CTAN at http://www.tug.org/ctan.html. The following ftp
urls work now (August 1998):
ftp://ftp.dante.de/tex-archive/macros/latex/contrib/other/misc/ulem.sty
ftp://ftp.tex.ac.uk/tex-archive/macros/latex/contrib/other/misc/ulem.sty
ftp://ctan.tug.org/tex-archive/macros/latex/contrib/other/misc/ulem.sty
The ulem package was written by Donald Arseneau, who contributed some
code and lots of useful advice to sectsty.
It doesn’t matter if you load ulem before or after sectsty, because sectsty
waits until the \begin{document} command before checking to see if ulem
has been loaded.
Sectsty uses a cunning trick to re-define the \underline command in-
side sectional headings only; \underline will work normally outside sec-
tional headings. The trick is cunning because the re-definition is different if
the ulem package is in use in that document: the commands that make this
trick work wait until the \begin{document} command has been executed,
so it doesn’t matter if you load ulem before or after sectsty.
The \underline and \ulemheading commands produce identical re-
sults if the ulem package is not loaded; they will both use LATEX’s normal
underlining mechanism. If you do load ulem, \underline will produce nicer
results, and \ulemheading will allow you to use any of ulem’s different un-
derlining styles.
The ulem package changes LATEX’s emphasis commands so that they
use underlining rather than italics for emphasis. If you don’t want this,
you should load ulem with the normalem option:
\usepackage[normalem]{ulem}
11
people get from the over long documentation outweighs the extra hassle it
causes.
\chapterfont{\thispagestyle{empty}}
\sectionfont{\sectionrule{3ex}{3pt}{-1ex}{1pt}}
What this example does is place two rules across the full width of the text.
One is a 3 pt rule 3 ex above the baseline of the top line of the heading,
and the other is a 1 pt rule 1 ex below the baseline of the bottom line of
the heading.
The vertical space before and after sectional headings can be affected
by these rules: the vertical space around a heading is placed before and
after everything that is printed for the sectional heading, so if a rule is
the topmost item in the sectional heading, then the vertical space will be
measured to that rule.
12
Setting the height of either rule to 0 pt will make it invisible, although
such a rule can still have an effect on spacing if you raise or lower it.
The \sectionrule command has a few problems: in particular, it can’t
place rules around centred headings, and it doesn’t work properly with run
in headings such as the standard \paragraph and \subparagraph headings.
You might like to try the titlesec package if sectsty can’t help you do what
you want.
Because \sectionrule must be the last command in a \hsectionifont
command, it can’t be used with underlined section headings: the underlin-
ing commands must also be placed last, so you can’t have both underlining
and rules around a sectional heading.
I wrote the \sectionrule command and it will be staying in sectsty.
I call it ‘unofficial’ because sectsty is meant to be very simple, and this
command takes things a long way away from my original ‘design concept’;
this is why it’s not mentioned in the bulk of the documentation above.
\sectionfont{\noindent\fbox}
This has two limits: it doesn’t work with \chapter or \part headings,
and it only works with single line headings. The reason for this last problem
is that the \fbox command typesets text in LR mode and will therefore
always produce a single line of text.
I’ve heard that other box making commands can be used to place a
frame around a sectional heading in a similar way (\colorbox apparently
works). If you use any box making command like this, you will need to
add a \noindent as with \fbox. This is because the box making com-
mand switches TEX from vertical mode to horizontal mode, which inserts
a paragraph indent unless there’s a \noindent at that point. There is a
\noindent command buried deep inside the code that produces sectional
headings, but too late on to help with this.
Another way of putting a box round a sectional heading gets round the
one-line limit of the simple approach above:
\makeatletter
\newcommand{\sectbox}[1]{%
\noindent\protect\fbox{%
\@tempdima=\hsize
\advance\@tempdima by-2\fboxsep
\advance\@tempdima by-2\fboxrule
13
\protect\parbox{\@tempdima}{%
\smallskip
% extra commands here
#1\smallskip
}}}
\makeatother
\sectionfont{\sectbox}
Again, this doesn’t work properly with \chapter or \part headings; an-
other limit is that the \sectbox command must be the last command in
the \hsectionifont command.
This technique has two significant differences to the first suggestion:
firstly, it puts a bit of extra vertical space between the text and the frame,
which improves the appearance of the heading; secondly, it places the text
of the heading inside a \parbox that stretches across the full width of the
text. This means that long headings can be broken across lines, and also
that the surrounding frame extends across the full width of the text, rather
than just enclosing the text of the heading.
While you can use fount changing commands as usual with the
\sectbox command – \sectionfont{\sffamily\sectbox} will work as
expected – the use of \parbox means that \raggedright and similar com-
mands won’t work in \hsectionifont commands. There is a way round this
problem: put the appropriate command in the definition of \sectbox at
the place marked extra commands here. For example, you could say:
\makeatletter
\newcommand{\sectbox}[1]{%
\noindent\protect\fbox{%
\@tempdima=\hsize
\advance\@tempdima by-2\fboxsep
\advance\@tempdima by-2\fboxrule
\protect\parbox{\@tempdima}{%
\smallskip
\raggedright% extra commands here
#1 \smallskip
}}}
\makeatother
\sectionfont{\sectbox}
and you’d get raggedright headings within the box. Both \raggedleft
and \centering also work, although you might like to use \nohang with
14
\centering.
\makeatletter
\def\@seccntformat#1{\protect\makebox[0pt][r]{\csname
the#1\endcsname\quad}}
\makeatother
will typeset the number of each section in the left margin, with the start of
each instance of sectional heading text aligned with the left hand edge of
the body text. It doesn’t affect chapter or part headings. This code won’t
affect the normal paragraph and subparagraph headings. If you change the
normal form of these headings by telling LATEX to print section numbers
with them, paragraph headings will work as you’d expect, but subpara-
graph headings will have the section number to the left of the normal
position although not in the left margin.
15
\makeatletter
\def\@seccntformat#1{\csname the#1\endcsname.\quad}
\makeatother
This will put a full stop after sectional numbers in sectional headers other
than part and chapter headings. It doesn’t affect cross-references or table
of contents entries.
An interesting extension to the simple original command allows you to
control the style of numbering you get with each level of sectional heading
independently:
\makeatletter
\def\@seccntformat#1{\@ifundefined{#1@cntformat}%
{\csname the#1\endcsname\quad}% default
{\csname #1@cntformat\endcsname}% individual control
}
\def\section@cntformat{\thesection.\quad}
\def\subsection@cntformat{\thesubsection.\quad}
\makeatother
this example gives you a full stop after section and subsection headings,
and has no effect on other levels.
16