3

In editing a large document where individual editors work on chapters, some of the team regularly need to refer to labels/keys in other chapters.

User jfbu put together a nice solution... the code down the page. Since the situation is always a bit involved for large multi-chapter multi-editor documents, the mwe is a bit involved too. I have posted one in mwekeys.zip file: http://hep.ucsb.edu/mwe/ . Three pages (TOC, lof, and lot) output showing the keys are displayed below.

There is still one flaw... when used with the package chapterbib, there is a hang at the end `(./mwekeys.aux (./ONE.aux) (./TWO.aux)) Runaway argument? ! File ended while scanning use of \@writefile. \par l.101 \end{document}

? q`

This hang only happens with chapterbib.

TOC lof lot

7
  • 1
    Do you mean a list of labels? The package showkeys can show the label inplace, i.e. where they are they defined
    – user31729
    Oct 27 '15 at 17:41
  • 1
    I was hoping for all of them in a centralized location, the easiest being just before or after the `short caption' in the listoffigures or listoftables. So an editor of Chapter X doesn't have to hunt and peck through Chapter Y to find a key. Ideally even the keys in the TOC would be shown too.
    – HNN
    Oct 27 '15 at 21:00
  • Which keys in the ToC???? What do you mean by keys in the TOC
    – user31729
    Oct 27 '15 at 21:01
  • I think each Chapter, Section, etc has a key that you can put in the \ref command... like, `for a discussion of quantum wormholes, refer to Chapter~\ref{quantworm}.' I suspect those keys are already internal to LaTeX... of course I could add a \label inside the various commands.
    – HNN
    Oct 27 '15 at 21:36
  • No, they aren't -- look into book.cls for example. You won't find automatic labels
    – user31729
    Oct 27 '15 at 21:40
5

last update: the hacky code below relies the aux file not containing at its very end a \@writefile line (when concerned with lot, lof, toc). Thus, one needs some means to put for example a \relax at the very end of the .aux file.

I achieve this with the help of package atveryend, and this modification appears robust enough for the sample files as provided by the OP.

I remove the very first version of the code, leaving only the one with the two added modifications (the one described here and the one with \KOMAoptions below).


penultimate update: adds also lof and lot to the entities handled by the hack. Appears to work. See code at bottom of answer.


Update as clarified in comments, the OP uses the scrbook document class. The code below was originally tested with article class which has no chapter's. The \chapter command by default (whether for book or scrbook) issues orders to add vertical spacings in List of Figures and List of Tables. These orders end up in the .aux file (or files) and my macro below does not see the \newlabel it is looking for.

  1. Under KOMA's scrbook, the following work-around is available. Add:

    \KOMAoptions{listof=nochaptergap}
    

    to the preamble.

  2. If with the standard book class, then the simplest would be to patch (conditionally on the boolean \ifHNNdraft) the kernel macro \@chapter, removing from it \addtocontents {lof}{\protect \addvspace {10\p@ }}\addtocontents {lot}{\protect \addvspace {10\p@ }}. As surely many packages patch the \chapter/\@chapter commands, this might prove somewhat fragile, I thus don't discuss that any further. The KOMA allows much simpler solution given above. The macro below does not cause errors, but it is simply ineffective with labels following \chapter. It works correctly for \part's and \section's.

remark from past experience I knew that biblatex has a very stubborn tendency to add commands in-between lines of the .toc file, and I was thus suspicious this could endanger the hack here. Fortunately, at the level of the aux file, this is done before the \@writefile lines whose behavior is patched here, thus my macro does find the \newlabel next in the .aux file, and there is perhaps compatibility with biblatex(at least my single test).

Final remark: the code below has been tested with hyperref and is compatible.


obsolete: Here is a hack, which I haven't tested at all (you did not provide a mwe I could copy paste). Perhaps it will break at the first opportunity. But gives the idea.


The original code has been removed. This is the final code which uses atveryend package and \KOMAoptions command to address various issues.

\documentclass {scrbook}
\usepackage{hyperref}% for testing compatibility
\usepackage{varioref}
\usepackage{chapterbib}% for testing compatibility

\usepackage{mwe}

% to put something at very end of main aux file
\usepackage{atveryend}% also loaded by hyperref
\makeatletter
    \AfterLastShipout{\immediate\write\@mainaux{\relax}}%
\makeatother

% boolean to activate or not insertion of label keys in toc, lof, lot:
\newif\ifHNNdraft
\HNNdrafttrue
% suppress key insertions in toc, lof, lot:
% \HNNdraftfalse

\makeatletter

\let\original@writefile\@writefile

\def\@writefile #1#2#3{\def\HNN@fileext{#1}%
     \let\HNN@next\original@writefile
     \in@{#1}{toc,lof,lot}%
     \ifin@
       \ifx\newlabel#3%
          \def\HNN@next ##1##2\newlabel
         {\futurelet\HNN@tmp\HNN@writefile #2\empty\HNN@writefile}%
       \fi
     \fi
     \HNN@next {#1}{#2}#3% fingers extra crossed
}%
\def\HNN@writefile 
{%
    \ifx\HNN@tmp\contentsline\expandafter\HNN@writefile@a\else
                             \expandafter\HNN@writefile@abort
    \fi
}%
\def\HNN@writefile@a \contentsline #1#2#3\HNN@writefile #4%
{%
    \original@writefile{\HNN@fileext}{\contentsline{#1}{#2\HNN@showkey {#4}}#3}%
    \newlabel {#4}%
}%
\def\HNN@writefile@abort #1\HNN@writefile
{%
    \original@writefile{\HNN@fileext}{#1}\newlabel
}%
\protected\def\HNN@showkey #1%
         {\ifHNNdraft\space\space \textit{key:} \texttt{#1}\fi}

\ifHNNdraft
   \KOMAoptions{listof=nochaptergap}
\fi
\makeatother

\begin{document}
\tableofcontents

\listoffigures

\listoftables

\thispagestyle{empty}

\part{PART WITH LABEL}\label{hello}

\chapter{CHAPONE}\label{chap1}

\begin{figure}
\includegraphics[width=.48\linewidth]{example-image-a}\hfill
\includegraphics[width=.48\linewidth]{example-image-b}
\caption{FIRST MWE to demonstrate how to place to images side-by-side}
\label{fig:BU}
\end{figure}

\section{foo}\label{foo}

Let me check \autoref{bar}.
Let me check \autoref{bob}.

\section{bar}\label{bar}

Let me check \autoref{foo}.
\begin{figure}
\includegraphics[width=.48\linewidth]{example-image-a}\hfill
\includegraphics[width=.48\linewidth]{example-image-b}
\caption{SECOND MWE to demonstrate how to place to images side-by-side}
\label{fig:JF}
\end{figure}

\section{with no label}
\subsection{with no label}

\part{PART WITHOUT LABEL}

\chapter{CHAPONE OF PART TWO}\label{chapII1}
\section{without label}
\begin{table}
  \centering
  \begin{tabular}{ccc}
    mountain&sea&lake
  \end{tabular}
  \caption{Sample table}\label{JF}
\end{table}
\section{bob}\label{bob}
\begin{table}
  \centering
  \begin{tabular}{ccc}
    apple&banana&strawberry
  \end{tabular}
  \caption{Sample table II}\label{BU}
\end{table}
\section{bib}
Let me check \autoref{foo}.

\end{document}

The image below illustrate the initially posted test file. Result with above code is similar, with keys also in LOT and LOF.

Blockquote

21
  • sections foo, bar, bob have labels of the same name by sheer laziness. The part example with label hello shows the macro is really picking up the label. Other remark: I always put \label externally to \section argument, and the macro assumes that.
    – user4686
    Oct 27 '15 at 22:57
  • I implemented your code in a simple scrbook condiment (from KOMA-script). Works fine. But there is an aspect that breaks it... I use this snippet to read in a chapter from a lower directory: \newcommand{\ChapLabel}{chap:empty} \newcommand{\tdrinclude}[1] { %\befchap \renewcommand{\ChapLabel}{chap:#1} % define the label for the chapter \subincludefrom{#1/}{#1} %\aftchap } So, for a directory/chapter named ONE, if I put \label{\ChapLabel} next to the \chapter{} command, I'm able to successfully use either ref{\ChapLable} or \ref{chap:ONE}. But I don't see chap:ONE in the key
    – HNN
    Oct 28 '15 at 8:44
  • possibly a problem of multiple .aux files. I don't know how \subincludefrom works. Without an explicit mwe, hard to provide anything concrete.
    – user4686
    Oct 28 '15 at 11:42
  • I've not found it easy to post files/formats outside of the primary post... it does look like the TeX to put the key for the chapter is ending up in ONE.aux, and not in mwekeys.aux, in the mwe I have posted at hep.ucsb.edu/mwe . \subincludefrom is from the import package. thanks.
    – HNN
    Oct 28 '15 at 14:10
  • I tried changing from \subincludefrom to \subimport, and the multiple .aux files disappear. The label/key combination for the chapter is declared in the single .aux file, but for Chapter One, somehow does not get into the .toc file.
    – HNN
    Oct 28 '15 at 14:26
3

Something like this? This hooks into \label and adds a toc entry to .lab ToC and provides a list of labels command.

It will probably not work with cleveref (at the moment)

Short note: It does not take the draft option so far.

\documentclass{book}




\makeatletter
\newcommand{\listoflabels}{%
  \section*{List of labels}
  \@starttoc{lab}
}
\makeatother



\usepackage{xpatch}

\xapptocmd{\label}{\addcontentsline{lab}{section}{\texttt{#1}}}{}

\begin{document}
\tableofcontents


\listoflabels

\chapter{First}\label{firstchapter}

\section{First}\label{first}


\begin{equation}
  E=mc^2 \label{firstequation}
\end{equation}


\end{document}

enter image description here

Update Using a labels documentclass option:

\documentclass[labels]{book}

\newif\iflabeloptionused

\DeclareOption{labels}{\labeloptionusedtrue}
\ProcessOptions*



\usepackage{blindtext}


\makeatletter
\newcommand{\listoflabels}{%
  \iflabeloptionused
  \section*{List of labels}
  \@starttoc{lab}
  \fi
}
\makeatother



\usepackage{xpatch}

\xapptocmd{\label}{\addcontentsline{lab}{section}{\texttt{#1}}}{}

\begin{document}
\tableofcontents


\listoflabels

\chapter{First}\label{firstchapter}

\section{First}\label{first}

\blindtext[15]
\begin{equation}
  E=mc^2 \label{firstequation}
\end{equation}


\end{document}
3
  • Ideally, I'd like to just get the keys to be right there in each line of the TOC, or list of figures/tables... that way the info like chapter/section & short captions would be easily correlated with the keys.
    – HNN
    Oct 27 '15 at 21:08
  • @HNN: You demand a lot of things for not providing a MWE or a clear question ;-) If you would have made it clearer right from the start I could have saved some time -- not wasting time on this solution :-(
    – user31729
    Oct 27 '15 at 21:09
  • Sorry, I posted some images that depict what I hope for.
    – HNN
    Oct 27 '15 at 21:42

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.