Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a label that references what is currently section 1.4. Can I tweak either the \ref or \label so that it appears as 4 (i.e. only the subsection number) when reffed?

share|improve this question

1 Answer 1

up vote 7 down vote accepted

Assuming you're sure that your reference have the form m.n, then the following code

\usepackage{refcount}
\newcommand\subsecref[1]{%
  \edef\temp{\getrefnumber{#1}}%
  \ifnum0=\temp ??\else
    \expandafter\eatsection\temp\eatsection
  \fi}
\def\eatsection#1.#2\eatsection{#2}

will do: call \subsecref{label}.

It's more complicated if the form of the references is different (say if they are chapter.section.subsection, but not conceptually more difficult.

A less efficient implementation that works also in expansion context is

\usepackage{refcount}
\newcommand\subsecref[1]{%
  \ifnum0=\romannumeral-`0\getrefnumber{#1}??%
  \else
    \expandafter\eatsection\romannumeral-`0\getrefnumber{#1}\eatsection
  \fi}
\def\eatsection#1.#2\eatsection{#2}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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