2

I am trying to center sections and subsections in an article with \begin{center} \end{center} and although it works for one when I try to center the second section or subsection it doesn't work. What am I doing wrong?

3
  • Could you please add a minimal working example (MWE)?
    – egreg
    Commented Mar 2, 2014 at 23:24
  • Please always post a complete small document that shows the problem. It isn't clear if you want to centre the headings or the text of the sections (you should never use any markup in the document to affect heading formatting, which should be set at the start of the document) Commented Mar 2, 2014 at 23:24
  • Sorry I meant that I want to center the headings!
    – Adam
    Commented Mar 2, 2014 at 23:26

2 Answers 2

3

The simplest method for centering section titles is with sectsty:

\documentclass{article}

\usepackage{sectsty}
\allsectionsfont{\centering}

\begin{document}
\section{A section}
Some tex here.
\subsection{A sub section}
Some tex here.
\section*{An un numbered section}
Some tex here.
\subsection*{An un numbered sub section}
Some tex here.
\end{document}

enter image description here

0
4

I assume that you are trying to center the headings of the sections and subsections. For this I will use titlesec package.

\documentclass{article}
\usepackage{titlesec}
\titleformat{\section}{\filcenter\bfseries} {\thesection.}{0.5em}{}
\titleformat{\subsection}{\filcenter\bfseries} {\thesection.\thesubsection.}{0.5em}{}
\titleformat{name=\section,numberless}{\filcenter\bfseries} {}{0.5em}{}
\titleformat{name=\subsectionmny=numberless}{\filcenter\bfseries} {}{0.5em}{}
%% spacings
\titlespacing*{\section} {0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\titlespacing*{\subsection} {0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\begin{document}
\section{A section}
Some tex here.
\subsection{A sub section}
Some tex here.
\section*{An un numbered section}
Some tex here.
\subsection*{An un numbered sub section}
Some tex here.
\end{document}

enter image description here

2
  • Thank you. Very helpful. Can't this be done by simply using the begin{center} environment?
    – Adam
    Commented Mar 2, 2014 at 23:28
  • 1
    @Adam Don't do it :). You will mess up your other things. Use titlesec.
    – user11232
    Commented Mar 2, 2014 at 23:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .