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!

\oddsidemargin =0cm \evensidemargin = 0cm \textwidth = 6.5in
\topmargin = 0in \textheight = 9in \headheight = 0in \headsep = 0in

%\renewcommand*\contentsname{Table of Contents}
\setcounter{secnumdepth}{-2}   % all section level below parts will not be numbered




{\Large \bf 
This is an example


\section{\protect \centering Section 1: \code{pip} and Virtual Environments}
\subsection{1. Introduction to \code{pip}}
This is an example
  • 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, 2019 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.
    – Jack
    Jul 8, 2019 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, 2019 at 4:36
  • Incidentally, how is \code defined?
    – Mico
    Jul 8, 2019 at 4:38
  • Yes, you are correct. I want subsubsection-level entries to be left-aligned.
    – Jack
    Jul 8, 2019 at 4:40

1 Answer 1


You could try the titlesec package.


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.


Hope that helps

  • 2
    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, 2019 at 3:40
  • This command affects all levels of headers - there are more fine-grained controls in the package.
    – veromary
    Jul 8, 2019 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!
    – Jack
    Jul 8, 2019 at 4:52
  • Move the \sectionfont line down after the \tableofcontents. It can come just before your first \section.
    – veromary
    Jul 8, 2019 at 5:43
  • This is exactly what I wanted! Thanks a lot!
    – Jack
    Jul 8, 2019 at 6:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .