To number the subsection, I use


It works well for the normal numbered sections. For example, if we use


it shows

1. A
1.1. try
1.2. try2

However, if one section is not numbered, the subsections within it looks weird. For example, if we write


we get

.1. try
.2. try

How can I write a macros to make an automatic judgement: if the section is numbered, this number is added to subsection numbering; if the section is not numbered, we directly number subsection by itself? As an example, I want to get

1. A
1.1. A1
1.2. A2

1. B1
2. B2
  • 1
    Don't do this! The readers will be lost with such navigation through your text.
    – Zarko
    Nov 7, 2015 at 19:47
  • 1
    I hope it is obvious why this would be a weird thing to do, but look at your redefinition of \subsection. Then use: \renewcommand*\thesubsection{\arabic{subsection}} at the point where you want the redefinition to take place. You will probably also have to reset the subsection counter: \setcounter{subsection}{0}. (But, really, don't do this.).
    – jon
    Nov 7, 2015 at 20:08
  • Thank you all. Yes it is a bad idea. Following jon's, maybe it is good to \renewcommand*\thesubsection{*.\arabic{subsection}} and \setcounter{subsection}{0} at the point of nonumber-sections to make it clear.
    – X Leo
    Nov 7, 2015 at 21:01
  • If you acknowledge this is a bad idea, why are you still trying to do it?
    – cfr
    Nov 8, 2015 at 1:13
  • 1
    This is document class and/or package dependent behavior. The default for book, report or article is to number subsections the way you do, but \section*{A} should not affect the section counter at all. Subsections should act like they are still in the last section (or section 0). Nov 8, 2015 at 15:25

1 Answer 1


This may make sense within the document body, but the ToC formatting will require some consideration.

Below I've updated the way \section works. It conditions on * and reformats the \thesubsection counter accordingly - either removing the prepended \thesection. or inserting it.

enter image description here



\RenewDocumentCommand{\section}{s o m}{%
    {\oldsection*{#3}% \section*
     \renewcommand{\thesubsection}{\arabic{subsection}}}% Update subsection numbering
    {\IfNoValueTF{#2}% \section
     {\oldsection{#3}}% \section{..}
     {\oldsection[#2]{#3}}% \section[.]{..}
     \renewcommand{\thesubsection}{\thesection.\arabic{subsection}}% Update subsection numbering
  \setcounter{subsection}{-1}\stepcounter{subsection}% Reset all sub-counters beyond \subsection

% Add period after sectional numbers in document body and ToC
\renewcommand{\@seccntformat}[1]{\csname the#1\endcsname.\quad}



\subsection{First subsection}
\subsection{Second subsection}

\subsection{Third subsection}
\subsection{Fourth subsection}

\subsection{Fifth subsection}
\subsection{Sixth subsection}

\subsection{Seventh subsection}
\subsection{Eighth subsection}


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.