我正在寻找一种方法来输出文档中使用的所有标签的列表,但到目前为止只找到这个答案它使用 WinEdt 中的内置函数。MikTex/TeXworks 中是否有类似的功能,或者我是否需要使用正则表达式之一搜索文件并在命令提示符中运行它?我目前在 Windows 10 上使用 TeXworks 版本 0.6.1(MikTeX 2.9.6100 64 位)。谢谢!
答案1
答案2
好吧,我不知道这是否算作内置的,但你总是可以让 LaTeX 为你完成这项工作:
\documentclass{article}
\usepackage{ltxcmds}
\usepackage{etoolbox}
\usepackage{pgffor}
% These packages are only used in the example content:
\usepackage{lipsum}
\usepackage{mwe}
\usepackage{amsmath, amssymb}
\usepackage{hyperref}
\makeatletter
% Just for demonstration purposes
\def\setcurrentlabelname#1{%
\def\@currentlabelname{#1}%
}
% I use this to format the output.
% basically taken from https://tex.stackexchange.com/a/275957/48973
\def\flexbox#1#2{%
{%
\settowidth{\@tempdima}{#2}%
\ifdim\@tempdima < #1%
\makebox[#1][l]{#2}%
\else
#2%
\fi
}%
}
% \newlabel is the command used in the .aux file to register labels
\let\original@newlabel\newlabel
\def\newlabel#1#2{%
\gappto\@labellist{,{{#1}{#2}}}%
\original@newlabel{#1}{#2}%
}
% its format changes when using hyperref
\ltx@ifpackageloaded{hyperref}{%
\def\@labellist{{{<label>}{{<num>}{<page>}{<title>}{<anchor>}{<>}}}}%
\def\@makelabelparameterslist#1#2#3#4#5{%
{#1}/.1\textwidth,%
{#2}/.1\textwidth,%
{#3}/.2\textwidth,%
{#4}/.2\textwidth,%
{#5}/.05\textwidth
}%
}{%
\def\@labellist{{{<label>}{{<num>}{<page>}}}}%
\def\@makelabelparameterslist#1#2{%
{#1}/.1\textwidth,%
{#2}/.1\textwidth
}%
}
% for printing the label list to the document
\def\printlabellist{%
\clearpage\noindent
{%
\small\raggedright
\def\@print@labelwithinfo##1##2{%
\@hangfrom{%
\flexbox{.2\textwidth}{[\texttt{##1}]}%
}{%
\hskip .025\textwidth
\expandafter\def\expandafter\@tempa\expandafter{\@makelabelparameterslist ##2}%
\foreach \field/\minwidth in \@tempa {%
\flexbox{\minwidth}{[\texttt{\field}]}%
\hskip .025\textwidth
}%
}\par
}%
\foreach \entry in \@labellist {%
\expandafter\@print@labelwithinfo\entry
}%
}%
\clearpage
}
% for writing the label list to the log file and console
{\catcode`\^^I=11\gdef\@tabchar{^^I}}
\def\typeoutlabellist{%
{%
\def\@typeout@labelwidthinfo##1##2{%
\message{^^J##1}%
\expandafter\def\expandafter\@tempa\expandafter{\@makelabelparameterslist ##2}%
\foreach \field/\minwidth in \@tempa {%
\message{\@tabchar\@tabchar\field}%
}%
}%
\message{^^JLabels used in this document:}%
\foreach \entry in \@labellist {%
\expandafter\@typeout@labelwidthinfo\entry
}%
\message{^^J^^J}%
}%
}
\makeatother
% print the label list at the end of the document
\AtEndDocument{\printlabellist}
% also write it to the log file
\AtEndDocument{\typeoutlabellist}
\begin{document}
\section{One}\label{sec:one}
\subsection{Alpha}\label{sec:alpha}
\lipsum[1]
\subsection{Beta}\label{sec:beta}
\lipsum[2]
\section{Two}\label{sec:two}
\lipsum[3-4]
\begin{figure}
\centering
\includegraphics[width=.6\textwidth]{example-image}
\caption{Ooh, an image!}
\label{fig:example}
\end{figure}
\lipsum[5]
\section{Three}\label{sec:three}
\subsection{Aleph}\label{sec:aleph}
\lipsum[6]
\subsection{Beth}\label{sec:beth}
\lipsum[7]
\begin{equation}\setcurrentlabelname{Fermat's last theorem}\label{eq:fermat}
\nexists \; a, b, c, n \in \mathbb Z_+, n > 2 : a^n + b^n = c^n
\end{equation}
\lipsum[8]
\subsection{Gimel}\label{sec:gimel}
\lipsum[9]
\end{document}
\printlabellist
输出文档中的标签列表(如果hyperref
不使用,则仅存在前三列):
\typeoutlabellist
将会把标签列表写入日志文件(和控制台):
Labels used in this document:
<label> <num> <page> <title> <anchor> <>
sec:one 1 1 One section.1
sec:alpha 1.1 1 Alpha subsection.1.1
sec:beta 1.2 1 Beta subsection.1.2
sec:two 2 1 Two section.2
fig:example 1 2 Ooh, an image! figure.1
sec:three 3 2 Three section.3
sec:aleph 3.1 2 Aleph subsection.3.1
sec:beth 3.2 2 Beth subsection.3.2
eq:fermat 1 3 Fermat's last theorem equation.3.1
sec:gimel 3.3 3 Gimel subsection.3.3
(如果有人知道更好地格式化该表格的方法(即用空格填充短条目),请告诉我。)