[Tex/LaTex] Numbering of subsections and paragraphs

cross-referencingnumberingsectioningsections-paragraphs

I have a document style for a report which I'm having to copy to LaTeX format. I'm using pdfLaTeX, for information. In essence, the system is that paragraphs are on the same level as subsections, until the first subsection in a section, wherafter paragraphs are on the level below subsections.

It should look like this:

1.00 FIRST SECTION

1.01 This is a paragraph at subsection level in the first section.

1.02 This is also a paragraph at subsection level in the first section.

1.03 First Subsection

1.03.1 This is a paragraph at paragraph level in the first subsection in the first section.

1.03.2 This is a paragraph at paragraph level in the first subsection in the first section.

1.04 Second Subsection

1.04.1 This is a paragraph at paragraph level in the second subsection in the first section.

Although I've been playing around with LaTeX for a pretty long time, I can't quite manage this style. What I've got is the following MWE:

\documentclass{article}

%%% TITLE DEFINITIONS %%%
\usepackage{chngcntr}    % change the display of counters
\counterwithin{table}{section}
\counterwithin{paragraph}{subsection} % paragraphs counted within subsections
\usepackage[tiny,raggedright,uppercase,noindentafter,toctitles,explicit]%
           {titlesec}

\newcommand*{\newpar}{\paragraph{}}

\newcounter{paraintro}[section]
\setcounter{paraintro}{0}

\makeatletter
\@addtoreset{paragraph}{section}
\@addtoreset{subsection}{section}
\renewcommand\thesection{\arabic{section}.\two@digits{\arabic{subsection}}}
\renewcommand\thesubsection{%
    \ifnum\arabic{paraintro}>0%
    \addtocounter{subsection}{\arabic{paraintro}}%
  \fi%
  \arabic{section}.\two@digits{\arabic{subsection}}%
  \addtocounter{paraintro}{-\arabic{paraintro}}%
}
\renewcommand\theparagraph{%
  \ifnum\arabic{subsection}<1%
    \addtocounter{paraintro}{1}%
    \arabic{section}.\two@digits{\arabic{paraintro}}%
  \else
    \arabic{section}.\two@digits{\arabic{subsection}}.\arabic{paragraph}%
  \fi
}
\renewcommand{\thetable}{%
  \arabic{section}.\two@digits{\arabic{subsection}} (\alph{table})%
}
\let\oldsection\section
\newcommand{\mysection}[1]{\oldsection{\uppercase{#1}}}
\def\section{\@ifstar{\oldsection}{\mysection}}
\makeatother
\setcounter{secnumdepth}{5}

\begin{document}

\section{First Section}\label{sec:first}

\newpar This is a paragraph at subsection level in the first section.\label{intropar:first}

\newpar This is also a paragraph at subsection level in the first section.It comes after intropar \ref{intropar:first}. \label{intropar:second}

\subsection{First Subsection}\label{ssec:first}

\newpar This is a paragraph at paragraph level in subsection \ref{ssec:first} in section \ref{sec:first}.\label{par:first}

\newpar This is a paragraph at paragraph level in subsection \ref{ssec:first} in section \ref{sec:first}. It comes after paragraph \ref{par:first}.\label{par:second}.

\subsection{Second Subsection}\label{ssec:second}

\newpar This is a paragraph at paragraph level in subsection \ref{ssec:second} in section \ref{sec:first}. It comes after paragraphs \ref{par:first} and     \ref{par:second}.\label{par:third}

\end{document}

This does not give me the results I'm after; the numbers are all over the place. It would seem as though the references are messing the thing up. I suspect it's to do with the invocation of \theparagraph and its ilk when \ref is called, but I've messed around with it for so long that I've no idea how to resolve the problem.

Might anyone be able to help with this?

Best Answer

Use a conditional.

\documentclass{article}

\usepackage[tiny,raggedright,uppercase,noindentafter,toctitles,explicit]%
           {titlesec}

\usepackage{chngcntr}    % change the display of counters


\newcommand*{\newpar}{%
  \ifsubsection\else\stepcounter{subsection}\fi
  \paragraph{}}

%%% A conditional for knowing whether a \subsection
%%% command has been issued
\newif\ifsubsection
\usepackage{etoolbox}
%%% Set the conditional to true after \subsection
%%% and reset the paragraph counter
\preto{\subsection}{\global\subsectiontrue\setcounter{paragraph}{0}}
%%% Reset it to false after \section
\preto{\section}{\global\subsectionfalse}

\counterwithin*{paragraph}{section}

\makeatletter
\renewcommand\thesection{\arabic{section}.\two@digits\c@subsection}
\renewcommand\thesubsection{\thesection}

\renewcommand\theparagraph{%
  \ifsubsection
    \arabic{section}.\two@digits\c@subsection.\arabic{paragraph}%
  \else
    \arabic{section}.\two@digits\c@paragraph
  \fi
}

%%% Tables
\counterwithin*{table}{section}
\renewcommand{\thetable}{%
  \arabic{section}.\two@digits\c@subsection~(\alph{table})%
}
\makeatother

\setcounter{secnumdepth}{5}

\begin{document}

\section{First Section}\label{sec:first}

\newpar This is a paragraph at subsection level in the first section.\label{intropar:first}

\newpar This is also a paragraph at subsection level in the first section.It comes after 
intropar \ref{intropar:first}. \label{intropar:second}

\subsection{First Subsection}\label{ssec:first}

\newpar This is a paragraph at paragraph level in subsection \ref{ssec:first} in section 
\ref{sec:first}.\label{par:first}

\newpar This is a paragraph at paragraph level in subsection \ref{ssec:first} in section 
\ref{sec:first}. It comes after paragraph \ref{par:first}.\label{par:second}

\subsection{Second Subsection}\label{ssec:second}

\newpar This is a paragraph at paragraph level in subsection \ref{ssec:second} in section 
\ref{sec:first}. It comes after paragraphs \ref{par:first} and 
\ref{par:second}.\label{par:third}

\section{Second Section}

\newpar This is a paragraph at subsection level in the first section.

\newpar This is also a paragraph at subsection level in the first section.

\subsection{First Subsection}

\newpar This is a paragraph at paragraph level.

\newpar This is a paragraph at paragraph level.

\subsection{Second Subsection}

\newpar This is a paragraph at paragraph level in subsection.

\end{document}

enter image description here

Related Question