1

I want to repair the references in the magnum opus after a lot of editing which essentially destroyed all the label-references correspondances.

To check systematically, I would like to use something like

\documentclass[11pt]{book}
\usepackage{cleveref}% should be the last package
\Crefname{subsection}{Subsection}{Subsections}
\begin{document}
\chapter{ONE}\label{1-0-0}
\section{one}\label{1-1-0}
\subsection{aaaa}\label{1-1-1}

\cref{1-0-0}

\cref{1-1-0}

\cref{1-1-1}

\chapter{TWO}\label{2-0-0}
\section{one}\label{2-1-0}
\subsection{aaaa}\label{2-1-1}

\cref{2-0-0}

\cref{2-1-0}

\cref{2-1-1}

\end{document}

but even that is a lot of work by hand and I was wondering if I could use something like

\cref{\chapternumber-0-0}

\cref{\chapternumber-1-0}

\cref{\chapternumber-1-1}

which I could then just paste in each chapter file. What could I use for \chapternumber?

  • ...try with \thechapter to replace your \chapternumber. Alternatively, if all references are of the form x-y-z, then you can use \thesubsection. – Werner Dec 4 '18 at 23:38
  • @Werner I saw your comment a few minutes after you made it, adjusted my list of crefs and started checking. So, yours is the "answer" I used. – schremmer Dec 6 '18 at 1:38
2

As the aim is to save typing, and as the labels are so regular, I suggest making the labels automatic. The easiest way to do this is to "save" the real commands using

\let\realchapter\chapter

and then we can redefine \chapter so that it adds the label:

\renewcommand\chapter[1]{\realchapter{#1}\label{\arabic{chapter}-0-0}}

So, the new \chapter command delegates the "real work" to the real chapter command, via \realchapter, and then adds the desired label. In fact, this may not be good enough because the \chapter command accepts an optional argument and it also has a *-variant. My guess is that you don't need the *-variant, and you probably do not need the optional short title, but we can "support" both of these using \RenewDocumentCommand from the xparse package. The redefinition of the \chapter command now becomes slightly more complicated:

\RenewDocumentCommand\chapter{ som }{%
  \IfBooleanTF{#1}
    {\IfNoValueTF{#2}{\realchapter*{#3}}{\realchapter*[#2]{#3}}}
    {\IfNoValueTF{#2}{\realchapter{#3}}{\realchapter[#2]{#3}}}
  \label{\arabic{chapter}-0-0}
}

On the first line, the som says that this command accepts a star as #1, an optional argument as #2 and a mandatory argument as #3. The \IfBooleanTF and \IfNoValue commands are tests for the * and the optional argument but otherwise this macro is much as before.

Putting this all together, the MWE in the OP can be rewritten without any \label commands as:

\documentclass[11pt]{book}
\usepackage{cleveref}% should be the last package
\Crefname{subsection}{Subsection}{Subsections}

\usepackage{xparse}

\let\realchapter\chapter%  save "copies" of the default \chapter,
\let\realsection\section%  \section and \subsection commands
\let\realsubsection\subsection
% redefine the \chapter, \section and \subsection commands to add labels
\RenewDocumentCommand\chapter{ som }{%
  \IfBooleanTF{#1}
    {\IfNoValueTF{#2}{\realchapter*{#3}}{\realchapter*[#2]{#3}}}
    {\IfNoValueTF{#2}{\realchapter{#3}}{\realchapter[#2]{#3}}}%
  \label{\arabic{chapter}-0-0}%
}
\RenewDocumentCommand\section{ som }{%
  \IfBooleanTF{#1}
    {\IfNoValueTF{#2}{\realsection*{#3}}{\realsection*[#2]{#3}}}
    {\IfNoValueTF{#2}{\realsection{#3}}{\realsection[#2]{#3}}}%
  \label{\arabic{chapter}-\arabic{section}-0}%
}
\RenewDocumentCommand\subsection{ som }{%
  \IfBooleanTF{#1}
    {\IfNoValueTF{#2}{\realsubsection*{#3}}{\realsubsection*[#2]{#3}}}
    {\IfNoValueTF{#2}{\realsubsection{#3}}{\realsubsection[#2]{#3}}}%
  \label{\arabic{chapter}-\arabic{subsection}-\arabic{subsection}}%
}

\begin{document}
\chapter{ONE}
\section{one}
\subsection{aaaa}

\cref{1-0-0}

\cref{1-1-0}

\cref{1-1-1}

\chapter{TWO}
\section{one}
\subsection{aaaa}

\cref{2-0-0}

\cref{2-1-0}

\cref{2-1-1}

\end{document}

and the output is as expected:

enter image description here

The question really asks about testing but if you add the labels this way then there is no need to test them because they will automatically be correct. Still, you could also add automatic tests by adding them to the new \chapter etc commands:

\RenewDocumentCommand\chapter{ som }{%
  \IfBooleanTF{#1}
    {\IfNoValueTF{#2}{\realchapter*{#3}}{\realchapter*[#2]{#3}}}
    {\IfNoValueTF{#2}{\realchapter{#3}}{\realchapter[#2]{#3}}}%
  \label{\arabic{chapter}-0-0}%
  Testing: \cref{\arabic{chapter}-0-0}%
}

Finally, if you do want to support the *-variant of these commands then the labels above do not really make sense. So it might be a good idea to use something like:

\RenewDocumentCommand\chapter{ som }{%
  \IfBooleanTF{#1}
    {\IfNoValueTF{#2}{\realchapter*{#3}}{\realchapter*[#2]{#3}}
     \label{\arabic{chapter}*-0-0}% *-label
    }
    {\IfNoValueTF{#2}{\realchapter{#3}}{\realchapter[#2]{#3}}
     \label{\arabic{chapter}-0-0}}%
  Testing: \cref{\arabic{chapter}-0-0}%
}
  • I wish I had asked the question a few years ago. Switching to this now ... But it's very tempting. Will see. – schremmer Dec 6 '18 at 1:36
  • 1
    If the obstacle to switching is disabling the existing \label commands then you could do this with minimal changes with something like \let\reallabel\label followed by \def\label#1{} and then use \reallabel in the macros above. This would disable all existing labels, so it is only an option if you only use labels for the chapters, sections and subsections as above. – Andrew Dec 6 '18 at 2:36
  • 1. I did give in to the temptation and started before I saw your comment. It works of course but, because of the particular way I get around \includeonly, see (tex.stackexchange.com/questions/251087/…), I ran into a bug: When I compile a chapter, all is well. When I compile ALL.tex, I get appendix A instead of chapter 1. The sections, toc, your code for refering to indexed words all work. 2. I am using tcolorbox which automatically manages box numbering but could not extend your code to tcboxes. – schremmer Dec 6 '18 at 17:47
  • ALL.tex = BOOK.tex – schremmer Dec 6 '18 at 17:54
  • @schremmer If you post a question about the tcolorbox issue I"ll have a look at it. I'm not sure if you are saying that appendices work or fail, but I would guess the latter. Depending on what you do in your preamble, it might be enough to use \thechapter in the code above, instead of \arabic{chapter}, as this should/would/could make the appendix labels take the form A-0-0, A-1-0, A-1-1...of course, this depends both on your preamble and what you want these labels to look like. – Andrew Dec 7 '18 at 0:10

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.