I was trying to center my section headings by using the command \centering, but I found that once the section headings are centered, the section names in the table of contents are also centered. Is there a way to keep the section names in the table of contents left-aligned while all the section headings are centered? (I only want the section headings to be centered and the headings of all other section levels to be left-aligned.) Below is an example code. Thanks a lot!

\documentclass{article}
\oddsidemargin =0cm \evensidemargin = 0cm \textwidth = 6.5in
\topmargin = 0in \textheight = 9in \headheight = 0in \headsep = 0in
\renewcommand{\floatpagefraction}{0.0}
%\usepackage{indentfirst}
\usepackage{fancyhdr}
\fancyhf{}

\setcounter{secnumdepth}{-2}   % all section level below parts will not be numbered

\usepackage{sectsty}
\sectionfont{\fontsize{13}{15}\selectfont}
\subsectionfont{\fontsize{10}{15}\selectfont}

\newcommand{\sectionalign}{\centering}

\begin{document}

\pagestyle{fancy}
\afterpage{\cfoot{\thepage}}
\hfill
\begin{center}
{\Large \bf
This is an example
\vspace{6pt}}
\end{center}

\tableofcontents
\newpage

\section{\protect \centering Section 1: \code{pip} and Virtual Environments}
\subsection{1. Introduction to \code{pip}}
This is an example
\end{document}

• You wrote, "I found that once the headings are centered..." -- please tell us what you've done so far, including which document class you employ. Please also clarify whether only section-level headers should be centered horizontally in the document, or if subsection- and subsubsection-level headers should be centered as well. – Mico Jul 8 at 3:38
• Hi, thank you for your feedback! I've updated my question statement and paste an example code. Hope it is more clear right now. – Wei-Tse WEHS7661 Jul 8 at 4:29
• Thanks for these additional pieces of information. If I understand your code correctly, you want just section-level headers to be centered, whereas subsubsection-level entries should be left-aligned (or, possibly, fully justified. Is this impression correct? – Mico Jul 8 at 4:36
• Incidentally, how is \code defined? – Mico Jul 8 at 4:38
• Yes, you are correct. I want subsubsection-level entries to be left-aligned. – Wei-Tse WEHS7661 Jul 8 at 4:40

You could try the titlesec package.

\usepackage[center]{titlesec}


I don't have the reputation to comment on the original post, but looks like to use sectsty for this, you can just put \centering after \selectfont in your \sectionfont and subsectionfont commands. You don't need to redefine \sectionalign at all.

\sectionfont{\fontsize{13}{15}\selectfont\centering}
\subsectionfont{\fontsize{10}{15}\selectfont\centering}
%\newcommand{\sectionalign}{\centering}


Hope that helps

• Does this instruction apply just to section-level headers or, more broadly, to all levels of sectioning headers (including chapter, subsection, and subsubsection?) – Mico Jul 8 at 3:40
• This command affects all levels of headers - there are more fine-grained controls in the package. – veromary Jul 8 at 4:34
• Thank you for your answer! Now that works! The only thing is that after I used \sectionfont{\fontsize{13}{15}\selectfont\centering}, the heading of the table of contents (Contents) is centered, but I want it to be left-aligned. Could you help me with this? Thanks! – Wei-Tse WEHS7661 Jul 8 at 4:52
• Move the \sectionfont line down after the \tableofcontents. It can come just before your first \section. – veromary Jul 8 at 5:43
• This is exactly what I wanted! Thanks a lot! – Wei-Tse WEHS7661 Jul 8 at 6:18