\label
我尝试以超链接的形式自动生成相关列表。
我几乎成功地做到了我想要的,使用了这个主题中提出的解决方案:所有带超链接的标签列表
结果如下:
但是我还有两个问题:
第一的:我的超链接列表位于一个章节中,该章节包含在一个包含大量 的文档中\include
。 并且似乎没有找到主文件之外的那些,如下例所示。
编辑:找到解决方案后,我将读取.aux 文件的代码放入一个函数中:
\NewDocumentCommand\readauxfile{m}{
\newread\zz
\immediate\openin\zz=#1.aux
\loop
\ifeof\zz\else
\read\zz to \tmp
\expandafter\findlabel\tmp\relax\findlabel
\repeat
}
然后我必须调用它来读取 main.aux 文件
\readauxfile{\jobname}
最后,我\include
用一个新\includewithlabels
函数替换它,该函数除了包含.tex 之外还读取.aux:
\NewDocumentCommand\includewithlabels{m}{
\readauxfile{./#1}
\include{#1}
}
第二:我用逗号分隔所有超链接,以便获得紧凑的列表。但是最后一个逗号是不需要的。我想知道您是否有办法修复它。
这是主要文件:
\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}
以下是章节:
\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}