1

Is there a way to reference the labelled things via their "relative" paths? (This is how Bourbaki referenced in their books.)

More precisely, I am using the book with chapters, sections and subsections. To maintain simplicity, assume that everything is numbered. The following cases illustrate what I want:

  1. References to Chapter 1 from within the same chapter should be "this chapter" and those from other places should be "Chapter 1".
  2. References to Chapter 1's Section 2 from within the same section should be "this section", those from Chapter 1's any other places within Chapter 1 should be "Section 2", and those from the remaining paces should be "Section 2 of Chapter 1".
  3. References to Chapter 1's Section 2's Subsection 3 from within the same subsection should be "this subsection", those from other places within Chapter 1's Section 2 should be "Subsection 3", those from other places within Chapter 1 should be "Subsection 3 of Section 2", and those from the remaining places should be "Subsection 3 of Section 2 of Chapter 1".

Further, let's say I have theorems (via amsthm) interspersed throughout (they needn't occur only in subsections) numbered within section. I want the references to Theorem 2.1 (which occurs in Chapter 1's Section 2's Subsection 3) from within Chapter 1 to be called "Theorem 2.1", and from any other places to be called "Theorem 2.1 of Chapter 1".

Can this be achieved?

Atom
  • 665
  • 2
    Do you know the varioref package. Somehow https://tex.stackexchange.com/q/33017/277964 could also be related. – cabohah Jan 26 '24 at 16:56
  • zref-check can assist you with some of your requirements. (Though not all, and even in those in which it can, it offers just some support to keep consistency rather than proper automation). – gusbrs Jan 26 '24 at 23:17
  • Yes you can do that. You only need to record enough data and then implement the logic. See eg https://tex.stackexchange.com/a/325319/2388 for something similar where zref was used (today I would use properties but they didn't exist at that time). – Ulrike Fischer Jan 27 '24 at 07:41

1 Answers1

3

A proof of concept as you won't get warnings if the labels are wrong.

This style of references is very heavy and not a good service to your readers, in my opinion.

\documentclass{book}

\NewProperty{chapter}{now}{0}{\arabic{chapter}} \NewProperty{section}{now}{0}{\arabic{section}} \NewProperty{subsection}{now}{0}{\arabic{subsection}}

\NewDocumentCommand{\chapterlabel}{m}{% \label{ch:#1}% \RecordProperties{ch@:#1}{chapter}% } \NewDocumentCommand{\sectionlabel}{m}{% \label{sec:#1}% \RecordProperties{sec@:#1}{chapter,section}% } \NewDocumentCommand{\subsectionlabel}{m}{% \label{subsec:#1}% \RecordProperties{subsec@:#1}{chapter,section,subsection}% }

\ExplSyntaxOn \NewDocumentCommand{\chapref}{m} { \str_if_eq:eeTF { \property_ref:nn { ch@:#1 } { chapter } } { \arabic{chapter} } { this~chapter } { chapter\nobreakspace\property_ref:nn { ch@:#1 } { chapter } } } \NewDocumentCommand{\secref}{m} { \str_if_eq:eeTF { \property_ref:nn { sec@:#1 } { chapter } } { \arabic{chapter} } {% we're in the same chapter \str_if_eq:eeTF { \property_ref:nn { sec@:#1 } { section } } { \arabic{section} } {% we're in the same section this~section } {% different section, same chapter section\nobreakspace\property_ref:nn { sec@:#1 } { section } } } {% different chapter section\nobreakspace\property_ref:nn { sec@:#1 } { section }~ of~chapter\nobreakspace\property_ref:nn { sec@:#1 } { chapter } } } \NewDocumentCommand{\subsecref}{m} { \str_if_eq:eeTF { \property_ref:nn { subsec@:#1 } { chapter } } { \arabic{chapter} } {% we're in the same chapter \str_if_eq:eeTF { \property_ref:nn { subsec@:#1 } { section } } { \arabic{section} } {% we're in the same section \str_if_eq:eeTF { \property_ref:nn { subsec@:#1 } { subsection } } { \arabic{subsection} } {% we're in the same subsection this~subsection } {% different subsection, same section subsection\nobreakspace\property_ref:nn { subsec@:#1 } { subsection } } } {% different section, same chapter subsection\nobreakspace\property_ref:nn { subsec@:#1 } { subsection }~ of~section\nobreakspace\property_ref:nn { subsec@:#1 } { section } } } {% different chapter subsection\nobreakspace\property_ref:nn { subsec@:#1 } { subsection }~ of~section\nobreakspace\property_ref:nn { subsec@:#1 } { section }~ of~chapter\nobreakspace\property_ref:nn { subsec@:#1 } { chapter } } } \ExplSyntaxOff

\begin{document}

\chapter{Test}\chapterlabel{testchapter1}

\chapref{testchapter1} --- \chapref{testchapter2}

\section{Test}\sectionlabel{testsection1}

\subsection{Test}\subsectionlabel{testsubsection1}

\section{Second}\sectionlabel{testsection2}

A: \secref{testsection1}. B: \secref{testsection2}. C: \secref{testsection3}

\subsection{AAA}\subsectionlabel{testsubsection2}

A: \subsecref{testsubsection1}. B: \subsecref{testsubsection2}. C: \subsecref{testsubsection3}.

\chapter{Second}\chapterlabel{testchapter2}

\section{Again}\sectionlabel{testsection3}

\subsection{BBB}\subsectionlabel{testsubsection3}

\end{document}

We record more properties for a single \...label and so we're able to check them one by one.

enter image description here

egreg
  • 1,121,712
  • Nice! I do have a suggestion though. Why not use the label hook to set the properties in a single auxiliary label and use just \label all around instead of relying on the \chapterlabel, \sectionlabel, \subsectionlabel complexity? – gusbrs Jan 27 '24 at 00:35
  • Going through your answer. It turns out that \NewProperty is a recent addition to LaTeX. Is it properly documented somewhere (apart from so that I can understand what it does? – Atom Jan 27 '24 at 06:07
  • 1
    @Atom texdoc ltproperties – egreg Jan 27 '24 at 08:00
  • @gusbrs I hope the OP doesn’t want something like “equation 3 of subsection 4 of section 7 of chapter 11” – egreg Jan 27 '24 at 08:13
  • Thanks! Feels so cool to use the recent additions to LaTeX kernel! (And yeah, I intend to keep it to "Eqn 3 of Sec 7.4 of Chap 11" ;) – Atom Jan 27 '24 at 08:50
  • Also, in your opinion, where do I start learning expl3 to be able to understand your code? – Atom Jan 27 '24 at 08:54
  • @egreg :-) :-). – gusbrs Jan 27 '24 at 10:17
  • @Atom texdoc interface3. Sorry, but I won't help you along this road that I find really awful. – egreg Jan 27 '24 at 11:21
  • Is there a reason why you used @ while recording new properties? – Atom Jan 28 '24 at 15:38
  • @Atom You can't have the same label twice, one for \label and one for \RecordProperties. – egreg Jan 28 '24 at 16:53
  • So, \RecordProperties records properties to a "separate label" called ch@:#1? If so, what's the purpose of having \label{ch:#1} inside the definition of \chapterlabel? – Atom Jan 28 '24 at 17:41
  • @Atom In case you change your mind… – egreg Jan 28 '24 at 20:14
  • Finally, I understand the ins and outs of your programme. :D Moving forward, I wish to combine the \chapref, \secref, \subsecref into a single command as I describe here. You think you can help? – Atom Jan 29 '24 at 17:56