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 09: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 09: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:-) http://tex.stackexchange.com/questions/148265/how-to-customize-enumeration-to-implement-brazilian-law-texts/148269#148269 – 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
  • Hi Davoid, I updated the question. The idea is indeed great, but for subsections \ref{} includes the section number, and for subsubsections, it includes the section and subsection number. How can this be avoided? – Marius Hofert Mar 19 '14 at 18:28
  • Dear David, please see the solution below I came up with. Thanks a lot for helping (if you like you can post it as solution and I will remove mine). – Marius Hofert Mar 20 '14 at 14:25
  • No it's OK you did the work (and I don't really need the points:-) – David Carlisle Mar 20 '14 at 14:29

1 Answers1

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}