如何找出文件中最大的((子)子)節號?

如何找出文件中最大的((子)子)節號?

我想創建一些表格a/b,其中a表示當前部分(簡單)和b文件中的最大部分編號(我正在努力解決這個問題)。對於小節和小節也是如此。對於下面的範例,它應該是2/3.1/1.3/4.如何找出文件中的最大((子)子)節號?

更新:在大衛的評論之後,我曾經\label{}參考最後的((子)子)部分。但是,對於小節和小節,\ref{}也包括節或小節編號。如何避免這種情況?

\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}

答案1

感謝大衛的評論和提示,以及這篇文章這裡,我想出了以下解決方案:

\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}

相關內容