Revised Attempt
In addition to \Unitref{<label>}
which provides the unit name along with the ref, as in "section 2" or "subsection 1.1", etc. (see original attempt), I now also introduce \Ref{<unit>}{<label>}
, where <unit>
is what is to be compared, "section", "subsection", or "subsubsection". If \ref{<label>}
is, in fact, the next increment of the unit, it will output "the next ". If \ref{<label>}
is not the next increment of the unit, it will output " \ref{<label>}
".
\documentclass{article}
\usepackage{ifthen}
\let\svsection\section
\let\svsubsection\subsection
\let\svsubsubsection\subsubsection
\def\section{\xdef\Unit{section}\svsection}
\def\subsection{\xdef\Unit{subsection}\svsubsection}
\def\subsubsection{\xdef\Unit{subsubsection}\svsubsubsection}
\let\svlabel\label
\def\label#1{\expandafter\xdef\csname Unit-#1\endcsname{\Unit}\svlabel{#1}}
\def\Unitref#1{\csname Unit-#1\endcsname~\ref{#1}}
\def\thenextsection{\the\numexpr\thesection+1\relax}
\def\thenextsubsection{\thesection.\the\numexpr\arabic{subsection}+1\relax}
\def\thenextsubsubsection{\thesection.\arabic{subsection}%
.\the\numexpr\arabic{subsubsection}+1\relax}
\def\Ref#1#2{%
\ifthenelse{\equal{\ref{#2}}{\csname thenext#1\endcsname}}%
{the next #1}{#1~\ref{#2}}
}
\begin{document}
\section{The First Section\label{s:first}}
``The First Subsection'' is \Ref{subsection}{ss:first},
whereas ``The Second Subsection'' is \Ref{subsection}{ss:second}.
\subsection{The First Subsection\label{ss:first}}
In the prior unit, \Unitref{s:first}, not to be confused with the
current unit, \Unitref{ss:first}, we have successfully captured the data
``A Subsubsection'' is \Ref{subsubsection}{sss:first},
whereas ``Second Subsubsection'' is \Ref{subsubsection}{sss:second}.
\subsubsection{A subsubsection\label{sss:first}}
``The First Section'' is \Ref{section}{s:first},
whereas ``The Second Section'' is \Ref{section}{s:second}.
\section{The Second Section\label{s:second}}
\subsection{The Section Subsection\label{ss:second}}
The prior unit, \Unitref{s:second} is not to be confused with the
way back references to either \Unitref{s:first}, \Unitref{ss:first},
or \Unitref{sss:first}. But the current unit is \Unitref{ss:second}.
We have successfully captured the data.
\subsubsection{Second subsubsection\label{sss:second}}
\end{document}

Areas that need improvement include optional capitalization of the "unit" name, if appearing at the beginning of a sentence, for example. Also, \Ref
can only compare "apples and apples". That is, if the label is that of a subsection, \Ref
can only be used to see if it is the next subsection, which is why argument #1
of \Ref
has to currently be specified explicitly.
Original (misguided) attempt
Here I just played around a little in the preamble and created \Unitref{}
to get "section 2" or "subsection 1.1", etc.
What I do is save the name "section", "subsection", etc. in a macro \Unit
whenever I enter such a unit. Then, when invoking \label
, I use the current value of \Unit
as the value of a new macro \csname Unit-<label>\endcsname
.
Finally, when invoking \Unitref{<label>}
, it calls upon \csname Unit-<label>\endcsname
and follows up with a hard space and a call to \ref{<label>}
\documentclass{article}
\let\svsection\section
\let\svsubsection\subsection
\let\svsubsubsection\subsubsection
\def\section{\xdef\Unit{section}\svsection}
\def\subsection{\xdef\Unit{subsection}\svsubsection}
\def\subsubsection{\xdef\Unit{subsubsection}\svsubsubsection}
\let\svlabel\label
\def\label#1{\expandafter\xdef\csname Unit-#1\endcsname{\Unit}\svlabel{#1}}
\def\Unitref#1{\csname Unit-#1\endcsname~\ref{#1}}
\begin{document}
\section{The First Section\label{s:first}}
\subsection{First Subsection\label{ss:first}}
In the prior unit, \Unitref{s:first}, not to be confused with the
current unit, \Unitref{ss:first}, we have successfully captured the data
\subsubsection{A subsubsection\label{sss:first}}
Blah-Blah
\section{The Next Section\label{s:second}}
\subsection{Next Subsection\label{ss:second}}
The prior unit, \Unitref{s:second} is not to be confused with the
way back references to either \Unitref{s:first}, \Unitref{ss:first},
or \Unitref{sss:first}. But the current unit is \Unitref{ss:second}.
We have successfully captured the data.
\end{document}
