Number paragraphs according to containing entity

Is there a way to have paragraphs at different depths in the sectioning hierarchy?

For example, in my dissertation background chapter, I don't need five levels (chapter/section/subsection/subsubsection/paragraph). In the body of the mathematical presentation, I need at least that many.

I want some of the same treatment for paragraphs at any sectioning level:

• Same heading styling (italic, not bold, for amsbook class)

But I want the numbering to adjust to the containing entity. Currently if I don't use subsections and subsubsections, I get zeros in the paragraph number, or worse (it tries to remember the number of the last subsubsection I did have).

\chapter{This is chapter 1}

\section{This is section 1}
Content

\subsection{This is subsection 1.1}
Content

\subsubsection{This is subsubsection 1.1.1}
Content

\paragraph{This is paragraph 1.1.1.1}
Content

\section{This is section 2}
Content

\paragraph{I would like this paragraph to be 2.1}
Content

\section{This is section 3}
Content

\subsection{This is subsection 3.1}
Content

\paragraph{I would like this to be paragraph 3.1.1}
Content


Is this kind of numbering feasible? Can it be done somewhat automatically, or would I need to redefine the paragraph style each time I put paragraphs in a different level?

• asnything in latex is changeable but I suppose the intended markup for that would be to use \subsection{I would like this paragraph to be 2.1} as it is a 2nd level heading, and then style subsections in the front matter differently if that is what you need. \paragraph is unfortunately named it is nothing to do with paragraphs of text, it's just an arbitrary name for a 4th level heading Nov 13, 2014 at 16:42
• @DavidCarlisle: I'm not strictly opposed to using \subsection for paragraphs at second-level, but how can I get the paragraph-like behavior named in my two bullets? Nov 13, 2014 at 16:45
• i've taken the liberty of adding an [amsbook] tag, since from your description, that seems to be what you're using. and i'll think about how this might be done. do you need it only in your introduction, or also in other parts of the dissertation as well? Nov 13, 2014 at 16:51
• @barbarabeeton: Mostly in the background and conclusion chapter. (Sorry if the word "introduction" implied this is front matter. It's not.) The sections describing methods and results are quite deeply nested. Nov 13, 2014 at 17:07

I'm not sure this is good style. Better, I'm sure this is not good style. ;-)

\documentclass{amsbook}

\usepackage{etoolbox,chngcntr}

\makeatletter
% at \@startsection, set also the level we're at
\pretocmd{\@startsection}{\setcounter{voigtlevel}{#2}}{}{}
\makeatother

\newcounter{voigtlevel} % keeps track of the current level

% all counters should be reset when an ancestor is stepped
\counterwithin*{paragraph}{chapter}
\counterwithin*{paragraph}{section}
\counterwithin*{paragraph}{subsection}
\counterwithin*{subsubsection}{chapter}
\counterwithin*{subsubsection}{section}
\counterwithin*{subsection}{chapter}

% redefine \theparagraph
\renewcommand{\theparagraph}{%
\ifcase\value{voigtlevel}\or
\thesection.\arabic{paragraph}\or
\thesection\zarabic{subsection}.\arabic{paragraph}\else
\thesection\zarabic{subsection}\zarabic{subsubsection}.\arabic{paragraph}\fi
}
% print no number (and no period) if the counter is 0
\newcommand{\zarabic}[1]{\ifnum\value{#1}=0 \else.\arabic{#1}\fi}

% sections don't have the chapter number
\renewcommand{\thesection}{\arabic{section}}

% paragraphs are numbered
\setcounter{secnumdepth}{4}

\begin{document}
\mainmatter
\chapter{This is chapter 1}

\section{This is section 1}
Content

\subsection{This is subsection 1.1}
Content

\subsubsection{This is subsubsection 1.1.1}
Content

\paragraph{This is paragraph 1.1.1.1}
Content

\section{This is section 2}
Content

\paragraph{I would like this paragraph to be 2.1}
Content

\section{This is section 3}
Content

\subsection{This is subsection 3.1}
Content

\paragraph{I would like this to be paragraph 3.1.1}
Content
\end{document}


• I'm pretty sure that repeating the number after the heading is not good style, akso. Oh wait, it's only showing up because it is part of the section heading in my sample code. Looks great! Is there a specific concern about this numbering style? Nov 13, 2014 at 17:57
• @BenVoigt I didn't change your text in any way, keeping the number in the title to show correctness. The problem is: what the reference to 3.1.1 refers to if you have a subsubsection with the same number? Nov 13, 2014 at 18:00
• That's my job to avoid, by not mixing child sections of different levels within the same immediate parent. But that already would create havoc; it's not something that your solution does any worse than the default. Nov 13, 2014 at 18:08

I cleaned up egreg's solution a bit, to extend it to cover subparagraphs and also take care of an annoyance in amsbook regarding incomplete support for the tocdepth counter.

And I got rid of the voigtlevel counter since it was completely redundant with zarabic.

\usepackage{chngcntr}

% place subsubsection, paragraph, and subparagraph at distinct values of tocdepth
% amsbook default is for all to share level 3
\makeatletter
\def\l@paragraph{\@tocline{4}{0pt}{1pc}{9pc}{}}
\def\l@subparagraph{\@tocline{5}{0pt}{1pc}{11pc}{}}
\makeatother

% all counters should be reset when an ancestor is stepped
\counterwithin*{subparagraph}{chapter}
\counterwithin*{subparagraph}{section}
\counterwithin*{subparagraph}{subsection}
\counterwithin*{subparagraph}{subsubsection}
\counterwithin*{paragraph}{chapter}
\counterwithin*{paragraph}{section}
\counterwithin*{paragraph}{subsection}
\counterwithin*{subsubsection}{chapter}
\counterwithin*{subsubsection}{section}
\counterwithin*{subsection}{chapter}

% print no number (and no period) if the counter is 0
\newcommand{\zarabic}[1]{\ifnum\value{#1}=0 \else.\arabic{#1}\fi}

% redefine \theparagraph
\renewcommand{\theparagraph}{%
\thesection\zarabic{subsection}\zarabic{subsubsection}.\arabic{paragraph}%
}
\renewcommand{\thesubparagraph}{%
\thesection\zarabic{subsection}\zarabic{subsubsection}\zarabic{paragraph}.\arabic{subparagraph}%
}