# ref to subsection number only

is there a way to reference the subsection part only? Example:

\documentclass{article}
\begin{document}

\section{This is my section}
For further information see subsection \ref{subsecthree} within this section.
\subsection{One}
\subsection{Two}
\subsection{Three}\label{subsecthree}

\end{document}

Usually gives (without the stars):

1 This is my section
For further information see subsection **1.3** within this section.

But I want:

1 This is my section
For further information see subsection **3** within this section.

Is that possible?

 To make it more clear: I am looking for a command that works like \ref, but prints (the arabic number of) the value of the subsection counter at the position of the \label.

=======================
== Solution in LaTeX ==

There is another solution below. This is a different solution I came up with
(I cannot post an answer to my own question, so I put it here):

\documentclass{article}

\newcommand{\labelsubseccounter}[1]{
\renewcommand\thesubsection{\arabic{subsection}}
\refstepcounter{subsection}
\label{#1}
\renewcommand\thesubsection{\thesection.\arabic{subsection}}
}

\begin{document}

\section{This is my section}
For further information see subsection \ref{subsecthree} within this section,
which is called \ref{onepointthree}, not \ref{onepointfour}.
\subsection{One}
\subsection{Two}
\subsection{Three}\label{onepointthree}\labelsubseccounter{subsecthree}
\subsection{Four}\label{onepointfour}

\end{document}

(I would like to insert the compiled code, but don't know how, sorry)

• Welcome to TeX.SX! Please add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. Feb 8 '14 at 13:00
• Thanks for the superquick reply. I added the three lines. Everything is working as expected though. I'm just looking for a way to make \ref{} not printing the section number in front of the subsection number.
– Emil
Feb 8 '14 at 13:06
• Do you use hyperref or any other related package which influences references? Feb 8 '14 at 13:14
• Sorry, my question seems not to be clear enough. Everything is working as it is supposed to by default. The \ref-command prints the subsection number "1.3". But I want a command that prints the subsection part only, thus "3", not "1.3". If there is a package to do that, I'll use it.
– Emil
Feb 8 '14 at 13:26

Related to your MWE you can use the following:

\documentclass{article}
\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}%
}

\makeatother
\begin{document}
\section{This is my section}
For further information see
\begin{tabular}{ll}
\verb+\sref{subsecone}+   & \sref{subsecone} \\
\verb+\sref{subsectwo}+   & \sref{subsectwo} \\
\verb+\sref{subsecthree}+ & \sref{subsecthree} \\
\end{tabular}  within this section.
\subsection{One}\label{subsecone}
\subsection{Two}\label{subsectwo}
\subsection{Three}\label{subsecthree}
\end{document}

Some explanation.

The standard reference are saved in the aux-file in the following form:

\newlabel{subsectwo}{{1.2}{1}}

You can see that \newlabel is a command with two mandatory argument. The first argument is the label name and the second argument has two other groups inside. The first group is the numbering scheme and the second group shows the page.

So you want to extract the first group of the second mandatory argument and then you want to extract the second part of the number.

With the command \@firstoftwo@second you take the first group of the second argument of \newlabel and due to it's defintion you extract your requested number.

• Nice. Could you please briefly explain what is the idea behind \@firstoftwo@second? Feb 8 '14 at 13:47
• Thanks for your answer. I'll try to understand the TeX code. I found another solution in LaTeX, which is not pretty, but lets me reference both the subsection counter and the section.subsection as usual. I'll append it to my question as I cannot answer my own question.
– Emil
Feb 8 '14 at 13:56
• This does not work with the package "hyperref". Is there some easy modification that will make it a better neighbour? Jan 16 '16 at 18:40
• latex.ltx does \expandafter#2#1\null, so you should probably use some \@firstofthree@second to avoid the \null becoming part of your captured #2.
– bers
Aug 7 '19 at 23:02
• @PauloNey - If you still need a solution to the present cross-referencing issue that works correctly with both \autoref and \cref, please see this answer.
– Mico
Aug 8 '19 at 4:47

This is not an answer, I just would like to add a version for subsubsections. Maybe Marco Daniel (or someone else) can verify whether this is correct (I'm not an expert here):

\documentclass{article}

% for subsections
\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}}
\makeatother

% for subsubsections
\makeatletter
\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}
\section{Section 3}\label{sec}
\subsection{Subsection}\label{subsec}
\subsubsection{Subsubsection}
\subsubsection{Subsubsection}\label{subsubsec}
Now we are in Subsubsection \ssref{subsubsec} of Subsection \sref{subsec} of
Section \ref{sec}
\end{document}
• This does not work for me. It prints not only the desired number but also the section title and then again the full section number. May 26 '14 at 19:59
• OK, this is because the hyperref package stores more information. It has five parameters of which only the first must be passed on: \def\@firstoftwo@third#1#2{% becomes \def\@firstoftwo@third#1#2#3#4#5{% May 26 '14 at 20:47
• @Thomas could you provide a whole example that works with hyperref? I tried your mod above, but no luck. Jan 16 '16 at 18:47
• I can't seem to reconstruct where and how I used this, sorry. Jan 18 '16 at 8:38
• @bers - I think you should qualify the claim "I got this to work with hyperref". See my comment below your answer.
– Mico
Aug 8 '19 at 5:00

This is @MarcoDaniel's solution generalized for use with article or IEEEtran, with or without hyperref. Besides being more general, it is more robust (works with \pdfstrcmp) and returns expected results (discards \hbox{} from the end of the result, for example). Also includes variants for \subsubsections.

@Mico is 100% correct that this solution does not modify in any way the behavior of \ref, \autoref, \cref etc. Also, with hyperref, the result of \sub(sub)ref is not a link. So what have I been using this for?

Imagine a document where the order of sub(sub)sections in one section (e.g., Results) should follow the same order of sub(sub)sections in another section (e.g., Materials & Methods, Experiments). To ensure this is always the case, I use a structure like this:

\section{Methods}
\subsection{...}
\label{MethodsA}
\section{Results}
\subsection{...}
\label{ResultA}
\CheckIfSubsectionsAreIdentical{MethodsA}{ResultsA}

For this to work, I obviously needed a technique to extract the naked sub(sub)section number from a reference, without any \hboxes, \nulls, or anything other included. This is what \sub(sub)ref is for. With hyperref, it's a replacement for \ref*, the version that does not give you hyperlinks.

And here is the code:

\documentclass{article}
% \documentclass{IEEEtran}
% \usepackage{hyperref}

\makeatletter
\def\lastoftwo@dotted#1.#2\@nil{#2}
\def\lastofthree@dotted#1.#2.#3\@nil{#3}
\let\submynthofm\lastoftwo@dotted
\let\subsubmynthofm\lastofthree@dotted
}{}
\def\lastoftwo@hyphen#1-#2\@nil{#2}%
\def\secondofthree@hyphen#1#2#3\@nil{#2}%
\def\submynthofm#1#2#3#4\@nil{\lastoftwo@hyphen#4\@nil}%
\def\subsubmynthofm#1#2#3#4\@nil{\secondofthree@hyphen#4\@nil}%
}{}
\newcommand{\subfirstofsix}[6]{\submynthofm#1\@nil}
\newcommand{\subsubfirstofsix}[6]{\subsubmynthofm#1\@nil}
\newcommand\subref[1]{\expandafter\real@setref\csname r@#1\endcsname\subfirstofsix{#1}}
\newcommand\subsubref[1]{\expandafter\real@setref\csname r@#1\endcsname\subsubfirstofsix{#1}}
}{
\newcommand{\subfirstofthree}[3]{\submynthofm#1\@nil}
\newcommand{\subsubfirstofthree}[3]{\subsubmynthofm#1\@nil}
\newcommand\subref[1]{\expandafter\@setref\csname r@#1\endcsname\subfirstofthree{#1}}
\newcommand\subsubref[1]{\expandafter\@setref\csname r@#1\endcsname\subsubfirstofthree{#1}}
}
\newcommand{\subrefvalue}{3}
\newcommand{\subsubrefvalue}{4}
}{}
\newcommand{\subrefvalue}{C}
\newcommand{\subsubrefvalue}{4}
}{}
\makeatother

\begin{document}
Last subsection is \subref{subsec:last},
\ifnum\pdfstrcmp{\subrefvalue}{\subref{subsec:last}}=0 OK! \else Error! \fi

Last subsubsection is \subsubref{subsubsec:last},
\ifnum\pdfstrcmp{\subsubrefvalue}{\subsubref{subsubsec:last}}=0 OK! \else Error! \fi

\section{Section}
\section{Section}
\subsection{Subsection}
\subsection{Subsection}
\subsection{Subsection}
\label{subsec:last}
\subsubsection{Test}
\subsubsection{Test}
\subsubsection{Test}
\subsubsection{Test}
\label{subsubsec:last}

\end{document}
• Your claim, that your solution (the \subref macro) works "with or without hyperref", needs to be qualified in two ways. First, if hyperref is loaded, the \subref command (unlike \ref) does not produce a hyperlink to the subsection in question. Second, the \autoref command doesn't give the expected solution, viz., "subsection 3"; instead, it produces "subsection 2.3" -- it ignores the requirement to strip off the section number. The second issue also affects \ref and \cref (the latter provided by the cleveref package).
– Mico
Aug 8 '19 at 5:10

Thanks for your question, your solution worked for me. I had to adapt because of two issues: (1) the article was using the IEEEtran document class which has sections I.A.1, I.A.2, II.A.1 and so on; and (2) I wanted to refer to subsection.subsubsection without the section number (e.g., A.1, B.3, etc.).

\documentclass[conference]{IEEEtran}

\newcommand{\labelsubseccounter}[1]{
\renewcommand\thesubsubsection{\Alph{subsection}.\arabic{subsubsection}}
\refstepcounter{subsubsection}
\label{#1}
\renewcommand\thesubsubsection{\thesection.\Alph{subsection}.\arabic{subsubsection}}
}

\begin{document}

\section{This is my section}
For further information see subsection \ref{subsecthree} within this section,
which is called \ref{onepointthree}, not \ref{onepointfour}.
\subsection{A}
\subsection{B}
\subsection{C}
\subsubsection{C1}\label{onepointthree}\labelsubseccounter{subsecthree}
\subsection{D}\label{onepointfour}

\end{document}

Hope this helps other people that need to adapt this as I did.