1

I try to automatically generate a list of relevant \label in the form of hyperrefs.

I almost managed to do what I want, using a solution proposed in this topic: List of all labels with hyperlinks

Here is a picture of the result:

result using input

However I have two remaining issues:

First: My list of hyperlinks is in a chapter, which is included into a document with a large number of \include. And it seems that it does not find those outside of the main file as in the exemple below.

result using include

EDIT: solution found, I put the code that reads the .aux file into a function:

\NewDocumentCommand\readauxfile{m}{
\newread\zz
\immediate\openin\zz=#1.aux
\loop
\ifeof\zz\else
\read\zz to \tmp
\expandafter\findlabel\tmp\relax\findlabel
\repeat
}

Then I have to call it to read the main.aux file

\readauxfile{\jobname}

Finally I replace the \include by a new \includewithlabels function which read the .aux in addition to include the .tex:

\NewDocumentCommand\includewithlabels{m}{
\readauxfile{./#1}
\include{#1}
}

Second: I separated all the hyperrefs by commas to have a compact list. However the last comma is not desired. I wonder if you have ideas to fix it.

Here is the main file:

\documentclass{book}

\usepackage{xstring} % function IfSubStr to select the labels I want based on kewords
\usepackage{amsthm} % package I use for theorems
\usepackage{thmtools} % package I use for theorems    
\usepackage{hyperref}

% To color the hyperlinks
\hypersetup{
    colorlinks=true,
    linkcolor=blue
}

\newtheorem{theorem}{Theorem}

% If it is a definition of a new label send it to the list
\long\def\findlabel#1#2\findlabel{%
 \ifx\newlabel#1\lablist\expandafter{\the\lablist\showlabel#2}\fi}

% Select the label of it contains the word "relevant" and format the list
\def\showlabel#1#2{\IfSubStr{#1}{relevant}{\hyperref[#1]{#1},}{}}

% Read a .aux file
\NewDocumentCommand\readauxfile{m}{
\newread\zz
\immediate\openin\zz=#1.aux
\loop
\ifeof\zz\else
\read\zz to \tmp
\expandafter\findlabel\tmp\relax\findlabel
\repeat
}

% Read the main.aux file
\readauxfile{\jobname}

% New command we have to use instead of \include
\NewDocumentCommand\includewithlabels{m}{
\readauxfile{./#1}
\include{#1}
}

\begin{document}

\section{A relevant section}\label{relevant section}

A section in the main file.

\includewithlabels{chapter/chapter}

\section{Another section}\label{other section}

We are back in the main file. Let's present an equation and a figure I also want to refer to.

\begin{equation}
1=2\label{relevant equation}
\end{equation}

\begin{figure}[ht]
\centering \huge XXX

\caption{a relevant figure}\label{relevant figure}
\end{figure}

\end{document}

Here is the chapter:

\section{A relevant included section}\label{relevant included section}

Another file containing another section and the list of every relevant sections, figures, equations...

Here is the list: \the\lablist

%\begin{theorem}
%This theorem causes issues with thmtools and input but works well with inlude.
%\end{theorem}
2
  • The second part of code you copied parses the main.aux file and looks for relevant labels. This file inputs the included.aux files, which may also contain relevant labels. These files, however, are not parsed, so the labels therein are ignored. If you want to support including files, I'm afraid you'll need to redesign the whole approach.
    – nickie
    Commented Feb 18, 2023 at 14:07
  • Absolutly right... except that it gave me the solution. Reading an included.aux is not so difficult since we already enter the path into the \include.
    – user290444
    Commented Feb 19, 2023 at 19:23

0

You must log in to answer this question.

Browse other questions tagged .