I was looking for a way to output a list of all the labels used in the document, but so far have only found this answer that uses a built in function in WinEdt. Is there a similar functionality in MikTex/TeXworks, or will I need to search the file using one of the regular expression and running it in command prompt or something? I'm currently using TeXworks Version 0.6.1 (MikTeX 2.9.6100 64-bit) on Windows 10. Thanks!
Is there a built in tool in MikTex/TeXworks to output a list of labels used in the current document?
-
4you can easily extract that from the .aux file – David Carlisle Mar 03 '18 at 20:13
-
@DavidCarlisle ok, I see it in the .aux file now; didn't know it was there. Would the solution be to just search through the file then? I was looking to a way to list just the labels is a different window than the main document to reference as I'm writing. – beez Mar 04 '18 at 02:24
-
I, too, think this constitutes an answer. It's specific to LaTeX. – thymaro Mar 04 '18 at 21:20
2 Answers
So, based on the comment by @DavidCarlisle, I was able to figure out how to search the *.aux file for the labels. First I had to tell TeXworks to show the *.aux file using the instructions found in this question and this question. Then I was able to use PowerShell to search the contents of the file using the Select-String cmdlet based on the instructions here and searching for "label" in the *.aux file.
- 11
Well, I don't know if this counts as built-in, but you can always let LaTeX do the work for you:
\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 outputs a list of the labels in the document (if hyperref is not used, only the first three columns exist):
\typeoutlabellist will write the label list to the log file (and the console):
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
(If anybody knows of a way to better format this table (i.e. pad the short entries with spaces), let me know.)
- 14,892
