[Tex/LaTex] Centering sections without centering subsections

formattingsectioning

This question is about how to center all the sections (including chapters) without centering subsections.

I have browsed all the related questions asked here but I found no one to which my question is duplicate. I have tried a proposed solution that centers all the headlines. But I do not know how to partially center the headlines.

Need your help, thanks so much, with my codes:

\documentclass[a4paper]{article}
\usepackage{mathtools}
\usepackage{colonequals}
\usepackage{mathrsfs}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{amsmath}`enter code here`
\usepackage{natbib}
\usepackage{bm}
\usepackage{mathdots}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{rotating, threeparttable, booktabs}
\usepackage{color}
\usepackage{dcolumn}
\usepackage{type1cm}
%\usepackage[margin=1.2in]{geometry}
%\usepackage{times}
\captionsetup{font=sf,font=small}
\setlength{\bibsep}{2pt}
\captionsetup{font=sf,font=small,labelfont=bf,textfont=bf}
\theoremstyle{plain}
\newtheorem{thm}{Theorem}[section]
\newtheorem{lem}[thm]{Lemma}
\newtheorem{cor}[thm]{Corollary}
\newtheorem{pro}[thm]{Proposition}
\newtheorem{assump}{Assumption}
\newtheorem{axiom}{Axiom}
\newtheorem{conj}{Conjecture}
\theoremstyle{definition}
\newtheorem{defi}[thm]{Definition}
\newtheorem{exam}[thm]{Example}
\newtheorem{exer}{Exercise}
\newtheorem*{rem}{Remark}
\newtheorem*{notation}{Notation}
%\renewcommand{\proofname}{\textbf{\textup{Proof}}}
\renewcommand*{\qedsymbol}{$\blacksquare$}
%\renewcommand{\baselinestretch}{2}
\renewcommand{\textfraction}{0.15}
\renewcommand{\topfraction}{0.85}
\renewcommand{\bottomfraction}{0.65}
\renewcommand{\floatpagefraction}{0.60}
%\renewcommand\familydefault{\sfdefault}


\newcommand\laref{\fontsize{12}{20pt}\selectfont \smallskip\noindent%
\hangindent=19pt}
\newcommand\xitem{\par\noindent\hangindent=8mm}



% To increase line space in footnote.
%
\let\oldfootnote\footnote
\renewcommand\footnote[1]{\oldfootnote{%
  \renewcommand\baselinestretch{1.5}%
  \large\scriptsize\ignorespaces#1}}
\addtolength{\footnotesep}{1pt}


%centering all titles
\usepackage{sectsty}
\sectionfont{\centering}

Best Answer

One way with sectsty:

\documentclass{article}
\usepackage{sectsty}
\sectionfont{\centering}
\begin{document}
  \section{Some section}
  \subsection{A subsection}
\end{document}

enter image description here

sectsty also offers \chapterfont (but in article class, there ain't no chapter), \subsectionfont etc along with \allsectionsfont which will apply to all sections.

With titlesec:

\documentclass{article}
\usepackage{titlesec}
\titleformat*{\section}{\Large\bfseries\centering}
\begin{document}
  \section{Some section}
  \subsection{A subsection}
\end{document}
Related Question