5

Hi what I'm trying to do is referencing the section when I have a \label in the subsection.

E.g. my code is:

\documentclass[12pt]{article}
\usepackage{lipsum}  

\begin{document}

\section{My Section} 
\lipsum[1]

\subsection{My Sub Section}
\label{subancor}
\lipsum[1]
Try to get the number of the section not of the subsection: \ref{subancor}

\end{document}

The regular behaviour is hat the \ref{subancor} will give me 1.1.

But is there a way that I can get the number of the "parent" section displayed? So in this case displays just the 1? (obviously without adding a \ref{whatever} below my \section{My Section} statement and referencing this one)

What I'm looking for is something like \refSectionNumberOf(subancor) does something like this exists?

5
  • 1
    Why don't you set a label at the beginning of the section?
    – Bernard
    Commented Jun 7, 2017 at 14:48
  • See my answer here: tex.stackexchange.com/a/353536/31729 -- it extracts the parent section
    – user31729
    Commented Jun 7, 2017 at 14:49
  • @Bernard because I'll move a couple of sections, subsections and subsubsection within my document therefore I just want to point (display) it to the correct section and linking the subsection.i.e in this example:" See Section 1 (My Sub Section)" (where "My Sub Section" is linked but not displayed as 1.1)
    – flor1an
    Commented Jun 7, 2017 at 14:52
  • Ah: I see. It should be more tricky.
    – Bernard
    Commented Jun 7, 2017 at 14:53
  • @Bernard: It is tricky
    – user31729
    Commented Jun 7, 2017 at 15:14

1 Answer 1

7

This is a shorter form of my answer at https://tex.stackexchange.com/a/353536/31729, adapted to provide only the number of the parent counter value, not its name as well.

Most of the functionality comes from the zref package, the counter reset list features are determined in my xassoccnt package.

The zref code adds the value of the parent counter. The first compilation stores the values of the parent counter, but that value is not accessible right from the start of course, that's why \zref@ifrefundefined is applied for checking.

With other words: Compile at least twice. After (or better: during) the first compilation macro \parentref returns -100.

The macro \parentref is expandable.

Make sure to keep the calls

\GetAllResetLists% Important
\RegisterPostLabelHook{\zlabel}% Important

right before \begin{document}!

If any counter is defined afterwards with \newcounter, \GetAllResetLists must be called again to get the correct information about the reset lists up to date.

Here's the very short version:

\documentclass{article}
\usepackage{lipsum}
\usepackage{xassoccnt}
\usepackage[user,counter]{zref}


\usepackage{xparse}
\makeatletter

% Define new properties
\zref@newprop{childcountervalue}{\arabic{\LastRefSteppedCounter}}% This is the naked value
\zref@newprop{parentcountervalue}{\csname the\GetParentCounter{\LastRefSteppedCounter}\endcsname}

% Add the new properties to the main property list stored with \zlabel
\zref@addprops{main}{childcountervalue,parentcountervalue}


\NewDocumentCommand{\parentref}{m}{%
  \zref@ifrefundefined{#1}{%
    -100%
  }{%
    \zref@extract{#1}{parentcountervalue}%
  }%
}


\makeatother

\GetAllResetLists% Important
\RegisterPostLabelHook{\zlabel}% Important

\begin{document}

\section{My Section} 
\lipsum[1]

\subsection{My Sub Section}
\label{subancor}


\lipsum[1]

\section{Foo section}

\subsection{Foobar subsection} \label{foobarsubsection}
Try to get the number of the section not of the subsection: \parentref{subancor} and \parentref{foobarsubsection}

The sum is \the\numexpr\parentref{subancor}+\parentref{foobarsubsection}

\end{document}

enter image description here

2
  • lovely. thx allot thats exactly what I was looking for
    – flor1an
    Commented Jun 7, 2017 at 15:05
  • @user2798895: You're welcome. Happy TeXing!
    – user31729
    Commented Jun 7, 2017 at 15:14

You must log in to answer this question.

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