Page LayoutCentering section titles in article class

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Centering section titles in article class

Post by Cham »

I'm wondering if it's possible to do the following. Here's a MWE showing a very important paper to be published somewhere (content is highly classified! ;) ). The section titles are horizontally centered with their number. I would like to center the title text itself, while the section number should have an offset to the left, keeping its constant space.

Code: Select all

\RequirePackage[l2tabu,orthodox]{nag}
\documentclass[12pt,letterpaper,twoside]{article}
\usepackage{microtype}
\usepackage[total={6.5in,10in},left=1in,top=0.5in,includehead,includefoot]{geometry}
\usepackage{sectsty}
\sectionfont{\centering}
\subsectionfont{\sffamily}
\subsubsectionfont{\sffamily}
\raggedbottom
\usepackage{lipsum}

\begin{document}

\section{Some not so hilarious introduction}
\lipsum[1-2]

\section{Fancy theory of absolutely everything}
\lipsum[5-5]

\section{General crap analysis}

\subsection{Tables of useless data}
\lipsum[1-2]

\subsection{Funky graphics}
\lipsum[3-3]

\section{Painfull discussion and killer conclusion}
\lipsum[4-4]

\end{document}
Is it possible to center the title text, while keeping the section number (which would be pushed a bit to the left)?

Recommended reading 2024:

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

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

User avatar
Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

Centering section titles in article class

Post by Stefan Kottwitz »

With titlesec instead of sectsty:

Code: Select all

\usepackage{titlesec}
\titleformat{\section}
  [block]{\normalfont\bfseries\sffamily\Large}{\rlap{\thesection}}{0em}
  {\hspace*{.05\textwidth}\begin{minipage}[t]{.9\textwidth}\centering}[\end{minipage}]
This centers the section title in a minipage, and keeps the number left aligned.

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Centering section titles in article class

Post by Cham »

That's not what I meant. The section number should stay at the same location, relative to its title. I mean same constant space at the left part of the title, not completely flushed to the left side.

Is there a way to do this while keeping the sectsty package?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

Centering section titles in article class

Post by Stefan Kottwitz »

Well, extremely uncommon... I guess nobody implemented such an asymmetric feature. It's like centering plus shifting. Or centering plus adding the numbers to the left. It's asymmetric, especially when numbers are of different width, such as 1 vs. 8 vs. 10.

I would reconsider doing that or not, before hacking it together. ;)

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Centering section titles in article class

Post by Cham »

Well, I agree. But when I look globally at the full text, I find it a bit weird that the section titles aren't centered, because of their number (it's a bit aesthetically unpleasing). Maybe that's why the default article class put all the titles flushed to the left instead. Maybe adding a dot after the numbers would fix the aesthetical glitch.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Centering section titles in article class

Post by Cham »

What do you think of adding a point after the number, using the secdot package in the code above ?

Code: Select all

\usepackage{secdot}
It looks good, but I have a doubt.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

Centering section titles in article class

Post by Stefan Kottwitz »

I like the dot as a separator. "1 Introduction" looks like a bad "One Introduction" to me, while "1. Introduction" is clearly for me "First: Introduction".

I would use sans serif headings though:

\sectionfont{\sffamily\centering}

Serif bold big headings look fat to me, and remind me of stone age LaTeX. Specifically, serif section headings don't fit to sans serif subsection headings.

Also, I would not use left aligned subsection headings following centered section headings. If I would have centered headings, then no headings below them at all. And by the way, in some book styles it's considered to be bad if one heading follows another heading. Some prefer a bit intro text before the next sub heading comes.

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Centering section titles in article class

Post by Cham »

I also feel that the default quad space is too large, between the number and its section/subsection title. These secdot options are working great, but I'm not sure this is good :

Code: Select all

\usepackage{secdot}
\sectionpunct{section}{. ~}
\sectionpunct{subsection}{ ~}
What would you use as an elegant space ?

EDIT : \enskip appears to be perfect !

Code: Select all

\sectionpunct{section}{.\enskip}
\sectionpunct{subsection}{\enskip}
Post Reply