1

I would like to create a number of the form a/b where a denotes the current section (easy) and b the maximal section number in the document (this I am struggling with). Similarly for subsections and subsubsections. For the example below, it should thus be 2/3.1/1.3/4. How can I find out the maximal ((sub)sub)section number in the document?

Update: After David's comment, I used \label{} to refer to the last ((sub)sub)section. However, for subsections and subsubsections, \ref{} includes the section or subsection number as well. How can this be avoided?

\documentclass{article}

\usepackage{units}

\newcommand\sectionnum{\arabic{section}}
\newcommand\subsectionnum{\arabic{subsection}}
\newcommand\subsubsectionnum{\arabic{subsubsection}}

\begin{document}
\section{Section 1}
\section{Section 2}
\subsection{Subsection 2.1}\label{lastsubsec}
\subsubsection{Subsubsection 2.1.1}
\subsubsection{Subsubsection 2.1.2}
\subsubsection{Subsubsection 2.1.3}
Now we are in Section \nicefrac{\sectionnum}{\ref{lastsec}}.\nicefrac{\subsectionnum}{\ref{lastsubsec}}.\nicefrac{\subsubsectionnum}{\ref{lastsubsubsec}}
% should give 2/3.1/1.3/4
\subsubsection{Subsubsection 2.1.4}\label{lastsubsubsec}
\section{Section 3}\label{lastsec}
\end{document}
  • basically just define each sectioning command (and end{document} to save the current values to the .auxfile, then pick them up on the next run. I'm pretty sure I've done that before in an answer here, I'll see if I can see the duplicate question (but anyone with code to hand should feel free to answer:-) – David Carlisle Mar 19 '14 at 9:50
  • Thanks a lot, David, that would be very helpful. I couldn't find a duplicate, but I might have used the 'wrong words'. – Marius Hofert Mar 19 '14 at 9:53
  • You wouldn't guess from the title but here is the other question. Note there the count is just used to change the form of numbering which is different if there is only 1, but the information gathering is the same, once you know the total number of subsections you can do whatever you want with it:-) tex.stackexchange.com/questions/148265/… – David Carlisle Mar 19 '14 at 15:07
  • ... wow, that's quite a bit above my head. For now, I added the line \AtEndDocument{\immediate\write\@auxout{\string\expandafter\gdef\noexpand\csname B@\the\c@subsection\string\endcsname{\the\c@paragraph}}} [with \makeatletter...\makeatother] to the header and obtain the additional line \expandafter\gdef \csname B@0\endcsname{0} in the .aux file... – Marius Hofert Mar 19 '14 at 16:29
  • Don't you want the maximum value of the subsection counter for each section? maybe I misread it if you only want sections it's easy you just need \thesection/\ref{lastsec} and put \label{lastsec} after the last section – David Carlisle Mar 19 '14 at 16:47
1

Thanks to David's comments and hints, and to the post here, I came up with the following solution:

\documentclass{article}

\usepackage{units}

% for referring to single subsection and subsubsection numbers
% see https://tex.stackexchange.com/questions/159299/ref-to-subsection-number-only/166676#166676
\makeatletter
\def\@firstoftwo@second#1#2{%
  \def\temp##1.##2\@nil{##2}%
   \temp#1\@nil}
\newcommand\sref[1]{\expandafter\@setref\csname r@#1\endcsname\@firstoftwo@second{#1}}
\def\@firstoftwo@third#1#2{%
  \def\temp##1.##2.##3\@nil{##3}%
   \temp#1\@nil}
\newcommand\ssref[1]{\expandafter\@setref\csname r@#1\endcsname\@firstoftwo@third{#1}}
\makeatother

\begin{document}
\section{Section 1}
\section{Section 2}
\subsection{Subsection 2.1}\label{lastsubsec}
\subsubsection{Subsubsection 2.1.1}
\subsubsection{Subsubsection 2.1.2}
\subsubsection{Subsubsection 2.1.3}
Now we are in Section \nicefrac{\arabic{section}}{\ref{lastsec}}.\nicefrac{\arabic{subsection}}{\sref{lastsubsec}}.\nicefrac{\arabic{subsubsection}}{\ssref{lastsubsubsec}}
% should give 2/3.1/1.3/4
\subsubsection{Subsubsection 2.1.4}\label{lastsubsubsec}
\section{Section 3}\label{lastsec}
\end{document}

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.