Text FormattingChange section heading styles

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
marcdein
Posts: 40
Joined: Sat Dec 19, 2009 2:11 pm

Change section heading styles

Post by marcdein »

How do I change the style of section and subsection headings in my docments? E.g. I want all section headings to be roman and bold, all subsection headings to be italic and bold, etc. I understand I need to redefine the section and subsection commands, but I'm not sure how to do this.

Thanks,

Marco

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Change section heading styles

Post by localghost »

Use sectsty or titlesec for standard classes. Other classes may have built-in features.


Best regards and welcome to the board
Thorsten
karlisrepsons
Posts: 50
Joined: Sat May 23, 2009 10:13 pm

Change section heading styles

Post by karlisrepsons »

In hope to ease my own over-questioning "guilt", I post a very relevant part of one work mine. Its written for XeTeX:

Code: Select all

\usepackage{titlesec}

% how many levels of text structure should get numbered?
%     NOTE: this has to be >= 3 for subsubsection to have whatever kind of content before its name in report or book documentclass!
\setcounter{secnumdepth}{3}

% how should text structure elements be formatted textually?
%     chapter
\renewcommand{\thechapter}{\Roman{chapter}}

%     section
\renewcommand{\thesection}{\arabic{section}.}

%     subsection
\renewcommand{\thesubsection}{\arabic{section}.\arabic{subsection}.}

%     subsubsection
\renewcommand{\thesubsubsection}{\arabic{section}.\arabic{subsection}.\arabic{subsubsection}.}


% fonts setup here
%     chapter
\newcommand{\mychapterfont}
{
   \addfontfeature{Colour=000060}
   \Huge
}

%     section
\newcommand{\mysectionfont}
{
   \addfontfeature{Colour=000060}
   \Large
}

%     subsection
\newcommand{\mysubsectionfont}
{
   \addfontfeature{Colour=000040}
   \large
}

%     subsubsection
\newcommand{\mysubsubsectionfont}
{
   \addfontfeature{Colour=000030}
   \normalsize
}


% how should text structure elements look?
%     chapter
\titleformat{\chapter}
{
   \mychapterfont
}
{\thechapter}{0.7em}{}

%     for appendix I like alternative formatting
%     appendix
\newcommand{\appendixTOC}
{
\titleformat{\chapter}
{
   \mychapterfont
}
{}{0em}{}
}

%     section
\titleformat{\section}
{
   \mysectionfont
}
{\thesection}{0.7em}{}

%     subsection
\titleformat{\subsection}
{
   \mysubsectionfont
}
{\thesubsection}{.5em}{}

%     subsubsection
\titleformat{\subsubsection}
{
   \mysubsubsectionfont
}
{$\diamond$}{.4em}{}
Also this should be somewhere in preamble before the fonts use:

Code: Select all

\usepackage{fontspec}   % provides font selecting commands
So what you need to do is: take what you need, change / add your own font features (like adding \itshape or something for bold) and that is it. You might need to read fontspec description [1].

[1] http://mirror.ctan.org/macros/xetex/lat ... ntspec.pdf
marcdein
Posts: 40
Joined: Sat Dec 19, 2009 2:11 pm

Re: Change section heading styles

Post by marcdein »

Many thanks for your replies. Unfortunately the "titlesec" package doesn't seem to be compatible with the amsart document class, which is what I am using. When I try to add this package it messes up the numbers of the formulas (e.g. (2.1) appears as (2..1)) and other things. Is there some way of modifying the amsart class directly? The code dealing with section headings contains stuff like

\def\section{\@startsection{section}{1}%
\z@{.7\linespacing\@plus\linespacing}{.5\linespacing}%
{\normalfont\scshape\centering}}
\def\subsection{\@startsection{subsection}{2}%
\z@{.5\linespacing\@plus.7\linespacing}{-.5em}%
{\normalfont\bfseries}}
\def\subsubsection{\@startsection{subsubsection}{3}%
\z@{.5\linespacing\@plus.7\linespacing}{-.5em}%
{\normalfont\itshape}}

but I'm not sure what it means. How can I find out more on this?
karlisrepsons
Posts: 50
Joined: Sat May 23, 2009 10:13 pm

Re: Change section heading styles

Post by karlisrepsons »

Just in case: maybe you copied those \renewcommand{\thesection} and similar things without thinking? Otherwise maybe others have something to say... localghost already wrote, that (as it can be concluded) your amsart might have some switches for what you want.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Change section heading styles

Post by localghost »

marcdein wrote:[…] Unfortunately the "titlesec" package doesn't seem to be compatible with the amsart document class, which is what I am using. […]
From this you can see that giving all necessary information at the beginning like the used document class is very important. Both of the packages I suggested earlier don't work with amsart. But you already picked the relevant code from the class file. Now we have to modify these declarations. Add the following to your preamble.

Code: Select all

\makeatletter
\def\section{\@startsection{section}{1}%
\z@{.7\linespacing\@plus\linespacing}{.5\linespacing}%
{\normalfont\bfseries\centering}}
\def\subsection{\@startsection{subsection}{2}%
\z@{.5\linespacing\@plus.7\linespacing}{-.5em}%
{\normalfont\bfseries\itshape}}
\def\subsubsection{\@startsection{subsubsection}{3}%
\z@{.5\linespacing\@plus.7\linespacing}{-.5em}%
{\normalfont\bfseries}}
\makeatother
I would suggest to use one of the standard classes and customize the headings with titlesec subsequently.
Post Reply