1

How can I put a label on \thesubsection without using \subsection?

MWE:

\documentclass{article}
\usepackage[english]{babel}

\newcommand*{\subsectioncounter}{\stepcounter{subsection}\thesubsection}

\begin{document}
\section{A section}
\subsection*{An un-numbered subsection}
\subsectioncounter\label{somesubsection}\quad Some text
\subsection*{Later in the document}
See \ref{somesubsection}. % I want this to print 1.1, not the section label.
\end{document}
4
  • 1
    use \refstepcounter instead of \stepcounter – Willie Wong Nov 17 '20 at 5:02
  • @WillieWong wow, thanks, I've been searching for a solution for this for quite a while! If you make this an answer I can make it the accepted answer :-) – Jake Ireland Nov 17 '20 at 5:06
  • 1
    Done; incidentally, many moons ago that ended up as an answer to one of my questions. However, since I didn't realize that was what I should have asked about, I doubt you would've been able to find it in the site search. – Willie Wong Nov 17 '20 at 5:13
  • For an in-depth discussion, see Understanding how references and labels work – Mico Nov 17 '20 at 5:51
2

You should use \refstepcounter instead of \stepcounter for this.

The former does the same as the latter, except it also associate subsequent \label statements with the value of the counter being incremented.

2
  • +1. However, I would replace "sets the current \ref value to be the counter being incremented" with "associates the next \label statement with the value of the counter being incremented". – Mico Nov 17 '20 at 5:47
  • @Mico: I agree that is a better phrasing. – Willie Wong Nov 17 '20 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.