0

Consider this basic document

\part{Alpha}
  \chapter{Beginning A}
    \section{Early Beginning A} \label{eba}
    \section{Late Beginning A}
        #1 See \myref{eba} then \myref{lea}
  \chapter{End A}
    \section{Early End A}
        #2 See \myref{eba} then \myref{lea}
    \section{Late End A}        \label{lea}
\part{Beta}
  \chapter{Beginning B}
    \section{Early Beginning B}
    \section{Late Beginning B}}
        #3 See \myref{eba} then \myref{lea}
  \chapter{End B}
    \section{Early End B}
    \section{Late End B}
        #3 See \myref{eba} then \myref{lea}

I want myref to produce different levels of context depending on how much context the label shares with the place of invocation.

How could I write myref such that the output is like this?

Part I: Alpha

Chapter 1: Beginning A

1.1 Early Beginning A

1.2 Late Beginning A

#1 See Section 1 then Chapter 2 Section 2

Chapter 2: End A

2.1 Early End A

#2 See Chapter 1 Section 1 then Section 2

Part II: Beta

Chapter 1: Beginning B

1.1 Early Beginning B

1.2 Late Beginning B

#3 See Part I Chapter 1 Section 1 then Part I Chapter 2 Section 2

Chapter 2: End B

1.1 Early End B

1.2 Late End B

#3 See Part I Chapter 1 Section 1 then Part I Chapter 2 Section 2

So basically, myref will produce

  • Section X if it is called from within the same chapter of the same part,

  • Chapter X Section Y if it is called from within a different chapter of the same part,

  • Part X Chapter Y Section Z if it is called from within a different part.

How do I write such a thing? Thanks.

2

1 Answer 1

1

This is a similar version (but extended) to the answer I gave here

https://tex.stackexchange.com/a/347161/31729

Basically it comes down to store the part, chapter and section counter values to the .aux and retrieve them using the zref package.

\documentclass{book}

\usepackage[counter,user,hyperref]{zref}

\makeatletter
\AtBeginDocument{%
\@ifpackageloaded{hyperref}{%
}{
  \providecommand{\phantomsection}{}
  \providecommand{\hyperlink}[2]{#2}
}

\zref@newlist{sectionprop}
\zref@newprop{partinfo}[-1]{\thepart}
\zref@newprop{chapterinfo}[-1]{\thechapter}
\zref@newprop{sectioninfo}[-1]{\thesection}

\newcommand{\contextlabel}[1]{%
  \zref@labelbyprops{#1}{partinfo,chapterinfo,sectioninfo,anchor,counter}%
}

% Wrapper macros that extract the reference counter type and the reference value -- this way, no additional \label is necessary

\newcommand{\countercref}[1]{%
  \expandafter\csname cref@\zref@extract{#1}{counter}@name\endcsname\ \hyperlink{\zref@extract{#1}{anchor}}{\zref@extract{#1}{sectioninfo}}%
}

\newcommand{\Countercref}[1]{%
  \expandafter\csname Cref@\zref@extract{#1}{counter}@name\endcsname\ \hyperlink{\zref@extract{#1}{anchor}}{\zref@extract{#1}{sectioninfo}}%
}


\newcommand{\contextref}[1]{%
  % Check first whether the label is defined at all, otherwise \Countercref etc. would fail!
  \zref@ifrefundefined{#1}{}{% 
    % Preexpand some information
    \edef\@tmp@a{\zref@extract{#1}{chapterinfo}}%
    \edef\@tmp@b{\zref@extract{#1}{partinfo}}%
    \edef\@tmp@c{\thechapter}%
    \edef\@tmp@d{\thepart}%
    \ifx\@tmp@a\@tmp@c\relax% Compare \thechapter with the result of the zref - label - value for chapter
    %No % at the end of the lines here!
    \Countercref{#1}
    \else
    \Countercref{#1} in \chaptername\ \zref@extract{#1}{chapterinfo}
    \ifx\@tmp@b\@tmp@d\relax% Compare \thepart with the result of the zref - label - value for part
    %
    \else
    in \partname\ \zref@extract{#1}{partinfo}% 
    \fi
    \fi
  }%
}

}
\makeatother


\usepackage{hyperref}
\usepackage{cleveref}

\renewcommand{\thepart}{\Alph{part}}


\begin{document}


\part{Alpha}
  \chapter{Beginning A}
    \section{Early Beginning A} \contextlabel{eba}
    \section{Late Beginning A}
         See \contextref{eba} then \contextref{lea}
  \chapter{End A}
  \section{Early End A}
  See \contextref{eba} then \contextref{lea}
  \section{Late End A}        \contextlabel{lea}
  \part{Beta}
  \chapter{Beginning B}
    \section{Early Beginning B}
    \section{Late Beginning B}
        See \contextref{eba} then \contextref{lea}
  \chapter{End B}
    \section{Early End B}
    \section{Late End B}
     See \contextref{eba} then \contextref{lea}
\end{document}

enter image description here

You must log in to answer this question.

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