我有一份很大的文档,附录中有 100 多个表格,文档中各处都有许多参考文献。
有没有办法创建一个参考文献列表,以便找到\ref{...}
文中所有表格和图形的参考文献?
例子:考虑以下包含两页文本和列出所有参考表格的部分的文档。
\documentclass{article}
\begin{document}
\section{text}
blabla \ref{fo} blabla \ref{fi} blabla \ref{th}
\clearpage
blabla \ref{on} blabla \ref{tw}
\section{tables}
\begin{table}\caption{on}\label{on}\end{table}
\begin{table}\caption{tw}\label{tw}\end{table}
\begin{table}\caption{th}\label{th}\end{table}
\begin{table}\caption{fo}\label{fo}\end{table}
\begin{table}\caption{fi}\label{fi}\end{table}
\end{document}
现在我想要一些信息,比如\listofreferences
告诉我表 4、5 和 3 在第 1 页被引用,表 1 和 2 在第 2 页被引用
答案1
像这样吗?
代码重新定义\ref
并获取标签名称,将其放入\seq
列表中,并获取其当前出现位置的页码,同样存储在序列中。然后,这些列表将存储到.lor
使用 检索的文件中\listofreferences
。
宏\trackonlylabeltypes
存储应跟踪的标签类型,例如figure
或,除非使用table
,否则所有其他类型都将被忽略。\nottrackallfalse
\crtrefcounter
请注意,由于尝试使用一些启发式方法确定底层计数器,因此代码可能会失败。如果不确定,请使用\nottrackallfalse
。
\documentclass{article}
\usepackage{letltxmacro}
\usepackage{xparse}
\usepackage{hyperref}
\usepackage{cleveref}
\usepackage{crossreftools}
\ExplSyntaxOn
\seq_new:N \g_label_names_seq
\seq_new:N \g_trackonly_labeltype_seq
\NewDocumentCommand{\trackonlylabeltypes}{+m}{%
\clist_map_inline:nn {#1} {
\seq_gput_right:Nn \g_trackonly_labeltype_seq {##1}
}
}
\NewDocumentCommand{\addlabelname}{+m}{%
\clist_map_inline:nn {#1} {
\seq_if_in:NnF \g_label_names_seq {##1} {
\seq_gput_right:Nn \g_label_names_seq {##1}
\seq_new:c {g_label_ ##1 _seq }
}
}
}
\NewDocumentCommand{\addlabelpos}{+m}{%
\seq_gput_right:cx {g_label_ #1 _seq } {\thepage}%
}
\NewDocumentCommand{\addlistofrefentry}{}{%
\seq_map_inline:Nn \g_label_names_seq {
\addtocontents{lor}{\protect\contentsline{section}{\cref*{##1} }{\seq_use:cn {g_label_ ##1 _seq } {,\space}}{}{}}
}%
}
\NewDocumentCommand{\CheckIfInTrackTypesT}{+m+m}{%
\seq_if_in:NxT \g_trackonly_labeltype_seq { \crtrefcounter{#1}} {#2}%
}
\ExplSyntaxOff
\NewDocumentCommand{\writelistofreferences}{}{%
\addtocontents{lor}{\protect\contentsline{section}{Referenced Value}{Page(s)}{}{}\addvspace{15pt}\par}%
% Loop through labels
\addlistofrefentry%
}
\makeatletter
\NewDocumentCommand{\listofreferences}{}{%
\clearpage
\section*{List of reference positions}
\@starttoc{lor}%
}
\newif\ifnottrackall
\nottrackalltrue
\AtBeginDocument{%
\LetLtxMacro{\origref}{\ref}
\RenewDocumentCommand{\ref}{s+m}{%
% Check for label existence first
\@ifundefined{r@#2}{%
}{% Only add the label if the type is correct
\ifnottrackall
\CheckIfInTrackTypesT{#2}{%
\addlabelname{#2}%
\addlabelpos{#2}%
}%
\else
\addlabelname{#2}%
\addlabelpos{#2}%
\fi
}%
\IfBooleanTF{#1}{%
\origref*{#2}%
}{%
\origref{#2}%
}%
}
}
\makeatother
\AtEndDocument{%
\writelistofreferences
}
\usepackage{blindtext}
\begin{document}
\trackonlylabeltypes{figure,table,equation}
\tableofcontents
\listofreferences
\section{text} \label{Footext}
See \ref{einstein}
\blindtext \ref{fo} blabla \ref{fi} blabla \ref{th} but \ref{Footext}
\clearpage
blabla \ref{on} blabla \ref{tw}
\begin{equation}
E = mc^{2} \label{einstein}
\end{equation}
\clearpage
\blindtext
\section{Normal section}
See \ref{on} too and \ref{fo}
\section{tables}
\begin{table}\caption{on}\label{on}\end{table}
\begin{table}\caption{tw}\label{tw}\end{table}
\begin{table}\caption{th}\label{th}\end{table}
\begin{table}\caption{fo}\label{fo}\end{table}
\begin{table}\caption{fi}\label{fi}\end{table}
\end{document}
\end{document}
答案2
我修改了 Christan Hupfer 的答案,使其也可以使用 cleveref 引用和多个 ref,例如\Cref{table1,table2}
。我将其作为单独的答案发布,但它 99% 基于 Christian 的解决方案:
\documentclass{article}
\usepackage{letltxmacro,xparse,hyperref,cleveref,crossreftools}
\ExplSyntaxOn
\seq_new:N \g_label_names_seq
\seq_new:N \g_trackonly_labeltype_seq
\NewDocumentCommand{\trackonlylabeltypes}{+m}{%
\clist_map_inline:nn {#1} {
\seq_gput_right:Nn \g_trackonly_labeltype_seq {##1}
}
}
\NewDocumentCommand{\addlabelname}{+m}{%
\clist_map_inline:nn {#1} {
\seq_if_in:NnF \g_label_names_seq {##1} {
\seq_gput_right:Nn \g_label_names_seq {##1}
\seq_new:c {g_label_ ##1 _seq }
}
}
}
\NewDocumentCommand{\addlabelpos}{+m}{%
\seq_gput_right:cx {g_label_ #1 _seq } {\thepage}%
}
\NewDocumentCommand{\addlistofrefentry}{}{%
\seq_map_inline:Nn \g_label_names_seq {
\addtocontents{lor}{\protect\contentsline{section}{\cref*{##1} }{\seq_use:cn {g_label_ ##1 _seq } {,\space}}{}{}}
}%
}
\NewDocumentCommand{\CheckIfInTrackTypesT}{+m+m}{%
\seq_if_in:NxT \g_trackonly_labeltype_seq { \crtrefcounter{#1}} {#2}%
}
\NewDocumentCommand{\addtolor}{+m}{%
\clist_map_inline:nn {#1} {%
\ifnottrackall\CheckIfInTrackTypesT{##1}{\addlabelname{##1}\addlabelpos{##1}}\else\addlabelname{##1}\addlabelpos{##1}\fi%
}%
}
\ExplSyntaxOff
\NewDocumentCommand{\writelistofreferences}{}{%
\addtocontents{lor}{\protect\contentsline{section}{Referenced Value}{Page(s)}{}{}\addvspace{15pt}\par}%
\addlistofrefentry%
}
\makeatletter
\NewDocumentCommand{\listofreferences}{}{%
\clearpage
\section*{List of reference positions}
\@starttoc{lor}%
}
\makeatother
\newif\ifnottrackall
\nottrackalltrue
\AtBeginDocument{%
\LetLtxMacro{\origref}{\ref}
\LetLtxMacro{\origcref}{\cref}
\LetLtxMacro{\origCref}{\Cref}
\RenewDocumentCommand{\ref}{s+m}{\addtolor{#2}\IfBooleanTF{#1}{\origref*{#2}}{\origref{#2}}}
\RenewDocumentCommand{\Cref}{s+m}{\addtolor{#2}\IfBooleanTF{#1}{\origCref*{#2}}{\origCref{#2}}}
\RenewDocumentCommand{\cref}{s+m}{\addtolor{#2}\IfBooleanTF{#1}{\origcref*{#2}}{\origcref{#2}}}
}
\AtEndDocument{\writelistofreferences}
\usepackage{blindtext}
\begin{document}
\trackonlylabeltypes{figure,table,equation}
\tableofcontents
\listofreferences
\section{text} \label{Footext}
See \ref{einstein}
\blindtext \ref{fo} blabla \ref{fi} blabla \ref{th} but \ref{Footext}
\clearpage
blabla \ref{on} blabla \ref{tw}
\begin{equation}
E = mc^{2} \label{einstein}
\end{equation}
\clearpage
\blindtext
\section{Normal section}
See \Cref{on} too and \ref{fo}
Both \Cref{si,se}
\section{tables}
\begin{table}\caption{on}\label{on}\end{table}
\begin{table}\caption{tw}\label{tw}\end{table}
\begin{table}\caption{th}\label{th}\end{table}
\begin{table}\caption{fo}\label{fo}\end{table}
\begin{table}\caption{fi}\label{fi}\end{table}
\begin{table}\caption{si}\label{si}\end{table}
\begin{table}\caption{se}\label{se}\end{table}
\end{document}
\end{document}