Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
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
add comment

2 Answers 2

up vote 3 down vote accepted

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

share|improve this answer
    
Thanks very helpful! –  Adam Mar 2 at 23:39
add comment

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

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.