4

I would like to include just the subsection number of a subsection, without the section number.

I think this MWE summarizes what I am after. Is there an alternative to \ref?

\documentclass{article}
\begin{document}
\section{Section A}
\section{Section B}
\section{Section C}
\subsection{Subsection 1}
\subsection{Subsection 2}
\label{subsec:Subsection}
I would like "\ref{subsec:Subsection}" to just return "2".
\end{document}
7
  • How should the reader distinguish if this ref points to section 2 or to subsection 1.2, 2.2, 3.2, ...?
    – leandriis
    Aug 7, 2019 at 19:21
  • 1
    Probably related: ref to subsection number only
    – leandriis
    Aug 7, 2019 at 19:23
  • 2
    @leandriis The same way we distinguish "on Friday": context. If you say in subsection 2, you mean of the current section. (I'm not defending the system, but I don't think the disambiguation problem is real.)
    – Alan Munn
    Aug 7, 2019 at 19:24
  • 1
    @hair-splitter This doesn't do what is asked for, since it will change the format of the subsection number in the heading, which is not desirable.
    – Alan Munn
    Aug 7, 2019 at 19:28
  • 1
    Dear all: I've taken the liberty of re-opening this posting, as the solutions provided to the earlier posting ref to subsection number only are all incompatible, to varying degrees, with \autoref of the hyperref package and \cref of the cleveref package. The answer given below does not suffer from this issue.
    – Mico
    Aug 8, 2019 at 5:05

1 Answer 1

4

Here's a solution that's based on a technique that I learned from the book "The LaTeX Companion" (2nd ed.). It executes \renewcommand\thesubsection{\arabic{subsection}} to reset the representation of the subsection counter, and it makes use of the low-level LaTeX command \subsection@cntformat, which governs how the subsection counter is displayed in subsection-level entries.

This solution is, by design, compatible with the hyperref and cleveref packages and their cross-referencing macros, e.g., \autoref, \cref, and \labelcref.

Addendum 2019/08/08: I've added two lines to the code to generalize the solution to handle subsubsection-level headers as well.

enter image description here

\documentclass{article}
\usepackage{geometry} % optional

\renewcommand\thesubsection{\arabic{subsection}}
\renewcommand\thesubsubsection{\arabic{subsubsection}}
% Method proposed in "The LaTeX Companion", 2nd ed.:
\makeatletter
    \def\@seccntformat#1{\@ifundefined{#1@cntformat}%
       {\csname the#1\endcsname\space}%    default
       {\csname #1@cntformat\endcsname}}%  enable individual control
    \def\subsection@cntformat{\thesection.\thesubsection\space} 
    \def\subsubsection@cntformat{\thesection.\thesubsection.\thesubsubsection\space}
\makeatother

%Optional:
\usepackage[colorlinks,allcolors=blue]{hyperref}
\usepackage[noabbrev,nameinlink]{cleveref}
\crefname{subsection}{subsection}{subsections} 
\crefname{subsubsection}{subsubsection}{subsubsections} 

\begin{document}
\setlength\parindent{0pt}  % just for this example
The instructions \verb+\ref{sec:C2}+  and \verb+\labelcref{sec:C2}+ return ``\ref{sec:C2}'' and ``\labelcref{sec:C2}''.

The instructions \verb+\ref{sec:C11}+ and \verb+\labelcref{sec:C11}+ return ``\ref{sec:C11}'' and ``\labelcref{sec:C11}''.

\verb+\autoref+: \autoref{sec:C2} and \autoref{sec:C11}

And \verb+\cref+: \cref{sec:C2,sec:C11}

\addtocounter{section}{2} % just for this example
\section{Section C}
\subsection{Subsection 1}
\subsubsection{Subsubsection 1} \label{sec:C11}
\subsection{Subsection 2} \label{sec:C2}

\end{document}
1
  • 2
    A little side remark: The second upvote on this answer earned me the gold "cross-referencing" badge. Many thanks!!!
    – Mico
    Aug 8, 2019 at 4:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .