# \newcommand for centered \section

I am trying to use the titlesec package to achieve this but I'm not certain as to what the syntax should be...

\usepackage{titlesec}

\newcommand{\centeredsection}{\titleformat{\section}{\centering}}


-
So did it work? Also, please post complete code. Questions should always include a Minimal Working Example. –  cfr Mar 30 '14 at 3:30

If you want all section titles centered, it suffices to use the starred form of \titleformat in this way:

\titleformat*{\section}{\centering\normalfont\Large\bfseries}


MWE:

\documentclass{article}
\usepackage{titlesec}

\titleformat*{\section}{\centering\normalfont\Large\bfseries}

\begin{document}

\section{test}
Some text

\end{document}


Output:

Instead, if you are trying to create a new sectioning command which have a centered title, this can not be done in this way (supposing you are using the article class):

\makeatletter
\newcommand\centeredsection{\@startsection {section}{1}{\z@}%
{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}%
{\centering\normalfont\Large\bfseries}}
\makeatother


MWE:

\documentclass{article}

\makeatletter
\newcommand\centeredsection{\@startsection {section}{1}{\z@}%
{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}%
{\centering\normalfont\Large\bfseries}}
\makeatother

\begin{document}

\centeredsection{Centered section}
Some text

\section{Normal section}
Some text

\end{document}


Output:

-
Thanks Karl. This works great. May I ask what {-3.5ex \@plus -1ex \@minus -.2ex}% {2.3ex \@plus.2ex}% does exactly? I read the following link, but still dont quite understand it.tex.stackexchange.com/questions/31780/… –  Giri Mar 31 '14 at 8:42
@Giri You're welcome. I've simply taken the definition of \section and added \centering. {-3.5ex \@plus -1ex \@minus -.2ex} is the spacing before the section title (-3.5ex is the real spacing while \@plus -1ex \@minus -.2ex is the glue). The latter one is the spacing after the section title (with similar meanings). –  karlkoeller Mar 31 '14 at 8:54

This is how it is done with titlesec

\documentclass{article}
\usepackage{titlesec,showframe}

\titleformat{\section}[block]
{\filcenter\Large
\normalfont\bfseries}
{\thesection}{0.5em}{}
\titlespacing*{\section}
{5pc}{*2}{*2}[5pc]

\begin{document}

\section{test}
Some text

\end{document}


With sectsty:

\documentclass{article}
\usepackage{sectsty,showframe}

\sectionfont{\centering}

\begin{document}

\section{test}
Some text

\end{document}


If you want to define a new section command that is centered, this is not the way. But following is an example of doing it.

\documentclass{article}
\usepackage{showframe}
\usepackage{etoolbox}
\usepackage{letltxmacro}
%%make \section centered
\patchcmd{\section}{\normalfont}{\centering\normalfont}{}{}
%% assign \section to \centeredsection
\LetLtxMacro{\centeredsection}{\section}
%% revert \centering from regular section
\patchcmd{\section}{\centering\normalfont}{\normalfont}{}{}

\begin{document}
\centeredsection{Centered section}
Some text

\section{Normal section}
Some text

\end{document}


-
Your solution causes the first paragraph after the \section command to be indented, whereas "normal LaTeX" behavior is not to insert \parindent at the start of the first paragraph following a sectioning command. Is there a way to remedy this situation? –  Mico Mar 30 '14 at 7:35
@Mico Just to use \titlespacing*{\section} {5pc}{*2}{*2}[5pc] is the remedy. –  Harish Kumar Mar 30 '14 at 7:55
@Giri I have updated the answer. –  Harish Kumar Mar 30 '14 at 8:44