# XeLaTeX problem with section centering

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?

-
Could you please add a minimal working example (MWE)? –  egreg Mar 2 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) –  David Carlisle Mar 2 at 23:24
Sorry I meant that I want to center the headings! –  Adam Mar 2 at 23:26

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}


-

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}


-
Thank you. Very helpful. Can't this be done by simply using the begin{center} environment? –  Adam Mar 2 at 23:28
@Adam Don't do it :). You will mess up your other things. Use titlesec. –  Harish Kumar Mar 2 at 23:30
Ok I won't thanks! –  Adam Mar 2 at 23:31