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}}
Thanks for your help.
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}}
Thanks for your help.
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}

titlesec after \newcommand{\centeredsection} in my code but it seemed to center my other headings and not my \newcommand centeredsection...
–
Mar 30 '14 at 05:46
\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 07:35
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:

{-3.5ex \@plus -1ex \@minus -.2ex}% {2.3ex \@plus.2ex}% does exactly? I read the following link, but still dont quite understand it.http://tex.stackexchange.com/questions/31780/where-can-i-find-help-files-or-documentation-for-commands-like-startsection-fo
–
Mar 31 '14 at 08:42
\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 08:54