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 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.

share|improve this question
1  
So did it work? Also, please post complete code. Questions should always include a Minimal Working Example. –  cfr Mar 30 at 3:30

2 Answers 2

up vote 5 down vote accepted

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

share|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/… –  Giri Mar 31 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 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}

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

share|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... –  Giri Mar 30 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 at 7:35
    
@Mico Just to use \titlespacing*{\section} {5pc}{*2}{*2}[5pc] is the remedy. –  Harish Kumar Mar 30 at 7:55
    
@Giri I have updated the answer. –  Harish Kumar Mar 30 at 8:44

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.