2

To number the subsection, I use

\renewcommand*\thesubsection{\thesection.\arabic{subsection}}

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

\section{A}  
\subsection{try}
\subsection{try2}

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

\section*{A}
\subsection{try}
\subsection{try2}

we get

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

B
1. B1
2. B2
6
  • 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

2

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

\documentclass{article}

\usepackage{xparse}

\let\oldsection\section
\RenewDocumentCommand{\section}{s o m}{%
  \IfBooleanTF{#1}
    {\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
\makeatletter
\renewcommand{\@seccntformat}[1]{\csname the#1\endcsname.\quad}
\renewcommand{\numberline}[1]{\hb@xt@\@tempdima{#1.\hfil}}
\makeatother

\begin{document}

\tableofcontents

\section{A}
\subsection{First subsection}
\subsection{Second subsection}

\section*{B}
\subsection{Third subsection}
\subsection{Fourth subsection}

\section*{C}
\subsection{Fifth subsection}
\subsection{Sixth subsection}

\section{D}
\subsection{Seventh subsection}
\subsection{Eighth subsection}

\end{document}

You must log in to answer this question.

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