# Can I get a list of labels written to a separate file, or at least in one place in the document?

I've checked several Q&As about labels and it seems that there are two packages (showlabels and showtags, I think) that list labels. However, from what I've read, they list the labels in that margins of the document and that is not what I want. My preference isbe for a list of labels in a separate file, or at least in one place in the document, perhaps at the end.

The discussions I've read explain that the information is available in the .aux file, and yes, I could make a copy of same and edit it to produce the file I want, but that is tedious to do repeatedly.

I would like such a simple (text) file because the ol' memory is going and I need reminding about what I called things.

• This may not be your problem, but a beginner mistake with LaTeX is having labels like \label{theorem4.2}, so that you have to remember that it's theorem 4.2. Instead, you should use a descriptive name like \label{continuity-of-composition}. That and an editor's autocomplete could be enough. But if that's not your problem, then this comment isn't all that helpful. Aug 5 at 18:13
• @Teepeemm. Thanks. I try to adhere to \label{type:name}, e.g., \label{fig:SomeName}. Even with that convention, I often can't remember how I began the name, in which case, the auto complete doesn't help. It would be great if the editor provided a drop-down list once the type is specified, but that's asking a lot. Aug 5 at 18:48

You can modify the \label command to write to a file.

Writing to a file is done by creating a new filehandle, then opening a file for this filehandle, then write to it using \write\filehandle{some text}, and in the end closing the filehandle. To make sure writing is not buffered you can use \immediate.

Modifying the \label command can be done with the etoolbox package that provides a command \apptocmd (append to command) with as arguments the command, the code you want to append, and two arguments for success or failure of the modification which can be left empty.

MWE:

\documentclass{article}
\usepackage{etoolbox}
% create new filehandle
\newwrite\labelfile
% open a file for the filehandle
\immediate\openout\labelfile=labels.txt
% modify \label to write to the filehandle
\apptocmd{\label}{\immediate\write\labelfile{page \thepage: #1^^J}}{}{}
\begin{document}
\section{Introduction}
\label{sec:introduction}
\begin{figure}[h]
\centering\fbox{Figs are used to make fig jam}
\caption{Boxed text}
\label{fig:box}
\end{figure}
\newpage
\section{Conclusion}
\label{sec:conclusion}
As shown in Figure \ref{fig:box}, fig jam is made from figs.
% close the filehandle
\immediate\closeout\labelfile
\end{document}


Resulting labels.txt:

page 1: sec:introduction
page 1: fig:box
page 2: sec:conclusion

• Pretty damned amazing! Aug 5 at 22:13
• Interesting. Your output has line breaks. Mine is a single text string. Not a big deal, just thought I should let you know. Aug 5 at 23:03
• @TonyK Strange. Maybe a Linux/Windows difference? You can also add an explicit newline ^^J at the end of the line, see edit. For me that does not make a difference in the output, but maybe for you it does? Aug 6 at 8:27
• Nope, still a text string. But as I said, it's not a big hassle to hit a few CRs to create a readable file for reference. It's been great to see all my labels in one place. Of course, it does make me wonder why I labelled so many things that, as I reflect on it now, I'm unlikely ever to reference! Aug 6 at 16:47
• To the moderator. I think something is screwed up on this Q&A. I've checked the tick mark but when I leave and return it seems to be unchecked. Is this thread corrupted in some way? Aug 8 at 3:22

Marijn's answer is great, and has the advantage that the labels.txt file is external to the document, where it might be a useful separate reference.

Another possibility, which includes the list of labels at the end of the document, is the following. This accumulates an internal list of the labels by monkeypatching \newlabel in time for the aux file to be read in, and then formatting that list at \end{document}.

The redefined \newlabel uses a token list to build up, in \printlabellist a value

\t {sec:introduction}{1}{1}
\t {fig:box}{1}{1}
\t {sec:discussion}{2}{1}
\t {sec:conclusion}{3}{2}.


Within the macro \displayprintlabels we define (just in time) the macro \t to format this appropriately when \printlabellist is expanded.

Extending this to sort the list by label name is cough left as an exercise for the reader (this would of course be very easy with Marijn's external list).

\documentclass{article}

%\usepackage{hyperref}

\newtoks\printlabellist
\let\orignewlabel\newlabel
\def\newlabel#1#2{%
\global\printlabellist=\expandafter{%
\the\printlabellist
\t{#1}#2\endt} % #2 is normally two args, but hyperref adds two more
\orignewlabel{#1}{#2}}
% Two alternative ways of formatting the result
\def\displayprintlabels{\newpage
\def\t##1##2##3##4\endt{\noindent\hbox to 8em{##1\hss}${}\mapsto{}$##2 (p.##3)\par}
\parskip=0pt \parindent=0pt
Labels:\par
\the\printlabellist}
\def\displayprintlabelstable{\newpage
\def\t##1##2##3##4\endt{##1&##2&##3\\}
\begin{tabular}{lll}
Label & value & page\\
\the\printlabellist
\end{tabular}}

%\AtEndDocument{\displayprintlabels}
\AtEndDocument{\displayprintlabelstable}

\begin{document}
\section{Introduction}
\label{sec:introduction}
\begin{figure}[h]
\centering\fbox{Figs are used to make fig jam}
\caption{Boxed text}
\label{fig:box}
\end{figure}
\section{Further discussion}
\label{sec:discussion}
It figures that if two figs are jammed into a single Figure, then that's a
very figgy Figure.
\newpage
\section{Conclusion}
\label{sec:conclusion}
As shown in Figure \ref{fig:box}, fig jam is made from figs.
\end{document}

• You guys are clearly in a different league. I'm just a user and not a very good one at that. I thought the cough comment was cute. Anyway, thanks for weighing in, but for some reason your code just causes my compiler (MikTex) to hang, so I never got to see the output. Pity. BTW, I did a simple copy/paste into the preamble of my document so whatever caused the hiccup, it wasn't my fat fingers this time. Aug 6 at 17:12
• Strange: any helpful chatter? Hmm: hypertex, for example, changes the format of \newlabel commands in the .aux file, so if you're using that in your document, it's possible that what I've written isn't robust against that. Aug 6 at 22:18
• No, the compile just ends -- no messages. As it compiles, the bottom panel fills with the compile progress as usual for a few seconds,, then it vanishes. Until you mentioned it I didn't think to check the .aux file -- there ain't none. It didn't write one. If you like I can comment out the commands and step through it (from the bottom up) to see which line causes the failure. Aug 6 at 23:21
• I should have said not to worry about the code causing problems. I have a little test bed document that I use for checking my markup instructions. I use it to test code I get online too, so no harm, no foul if it crashes and burns. Aug 6 at 23:46
• (edited, to make the suggestion work with hyperref, too) Aug 7 at 10:17