6

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.

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

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:

enter image description here

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:

enter image description here

| improve this answer | |
  • 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/… – user48045 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
6

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}

enter image description here

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} 

enter image description here

| improve this answer | |
  • Thank you Harish. I inserted what you had for titlesec after \newcommand{\centeredsection} in my code but it seemed to center my other headings and not my \newcommand centeredsection... – user48045 Mar 30 '14 at 5:46
  • 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. – user11232 Mar 30 '14 at 7:55
  • @Giri I have updated the answer. – user11232 Mar 30 '14 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy