Text FormattingHow to center section titles in article class

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

How to center section titles in article class

Post by Cham »

I need to change the section titles position in the article class. By default, the titles are located at left. I would prefer to have them centered. The subsection titles should stay at left (default position)

Here's a MWE to work with :

Code: Select all

\documentclass[12pt,oneside]{article}
\usepackage[letterpaper,margin=1in]{geometry}
\usepackage{blindtext}

\begin{document}
\section{First title}
\blindtext
\section{Second title}
\blindtext
\subsection{A sub-section}
\blindtext
\end{document}
I know this should be pretty basic, but I'm still having difficulties with this kind of settup.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How to center section titles in article class

Post by Johannes_B »

For such small changes, package sectsty can be used.

Code: Select all

\documentclass[12pt,oneside]{article}
\usepackage[letterpaper,margin=1in]{geometry}
\usepackage{blindtext}
\usepackage{sectsty}
\sectionfont{\centering}

\begin{document}
\section{First title}
\blindtext
\section{Second title}
\blindtext
\subsection{A sub-section}
\blindtext
\end{document}
A KOMA-script class does not need any additional packages. It has lots of stuff built in.

Code: Select all

\documentclass[12pt,oneside]{scrartcl}
\usepackage[letterpaper,margin=1in]{geometry}
\usepackage{blindtext}
\RedeclareSectionCommand[font=\centering]{section}
%\addtokomafont{disposition}{\rmfamily}

\begin{document}
\section{First title}
\blindtext
\section{Second title}
\blindtext
\subsection{A sub-section}
\blindtext
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

How to center section titles in article class

Post by Cham »

Thanks Johannes. The first solution gives a nicer output.

In both solutions, the space between the section number and its title seems to be too large. How can we reduce it ?

EDIT : I've found a command to add a dot, and allow to change the \quad space to another size. But then I'm not sure anymore what would be best looking. The dot also change the subsections number, but it's ugly in that case :

Code: Select all

\makeatletter \def\@seccntformat#1{\csname the#1\endcsname.\quad} \makeatother
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How to center section titles in article class

Post by Johannes_B »

If you don't want the period, just leave it out.

KOMA-script does not use big spaces.

Code: Select all

\documentclass[12pt,oneside]{scrartcl}
\usepackage[letterpaper,margin=1in]{geometry}
\usepackage{blindtext}
\RedeclareSectionCommand[font=\centering\Large]{section}
\addtokomafont{disposition}{\rmfamily}
 
\begin{document}
\section{First title}
\blindtext
\section{Second title}
\blindtext
\subsection{A sub-section}
\blindtext
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
vaman
Posts: 17
Joined: Wed Oct 09, 2019 5:51 am

How to center section titles in article class

Post by vaman »

Code: Select all

\documentclass[12pt,oneside]{article}
\usepackage[letterpaper,margin=1in]{geometry}
\usepackage{blindtext}
\def \bc{\begin{center}}
\def{\ec{\end{center}}
\begin{document}
	\bc
	\section{First title}
	\ec
	\blindtext
	\bc
	\section{Second title}
	\ec
	\blindtext
	\bc
	\subsection{A sub-section}
	\ec
	\blindtext
\end{document}
%I did this and compiled. It works well with me.
Last edited by Stefan Kottwitz on Tue Sep 07, 2021 8:55 am, edited 8 times in total.
Reason: code tag fixed
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

How to center section titles in article class

Post by Ijon Tichy »

@vaman: As I already explained: Please mark the code in your postings as code!

BTW: The shown code is the worst idea, because it changes the vertical spacing of the headings and it is not semantic but physical markup, that has to be done for every single \section, \subsection etc. command. All the other suggestions made above, i.e., the two examples by @Johannes, are better. So why do you show a new, not better suggestion to an old question, that has been answered well already? That's nonsense. Please answer new questions or old questions, that has not been answered already.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
Post Reply