# Automatically return list of all uses of a command, without repeats e.g. \compound using chemstyle

I'd like to automatically generate a comma separated list of all occurrences in my .tex file of a particular command.

Specifically, I want to ensure the correct numbering of chemical structures using the chemstyle package and \compound{<structurelabel>} in a long document by initially, invisibly declaring all structures, in the order they appear in the mainmatter via \compound*{<1ststructurelabel>,<2ndstructurelabel>,...}. [N.B. incorrect numbering can occur because of the inclusion of references to structures within headings and hence the TOC].

I am aware of this fantastic online tool, but find that it sometimes works perfectly and at other times doesn't respond at all (as far as I am aware I attempt to use it in exactly the same way each time), so would love an alternative.

Here is a MWE to hopefully make my query clearer (I use Texmaker and MiKTeX, with ActivePerl installed and shell-escape enabled):

\documentclass[oneside,fontsize=13pt,parskip=full]{scrbook}
\usepackage{scrhack,graphicx,libertine,lipsum,caption,setspace,gensymb,amsmath,amsthm,array,lastpage,color,upgreek,soul,multirow}
\usepackage[left=1.75in,right=1in,top=1.3in,bottom=1.3in]{geometry}
\usepackage[runs=2]{auto-pst-pdf}
\usepackage{chemstyle}
\usepackage[autostyle]{csquotes}
\usepackage[english]{babel}

\usepackage[
backend=biber,
style=numeric,
autocite=superscript,
url=false,
doi=false,
natbib=true
]{biblatex}
}

\usepackage[flushleft]{threeparttable}
\RawFloats[figure,scheme]
\newcommand\fnote[1]{\captionsetup{font=small,skip=4pt}\caption*{#1}}
\newcommand*{\myfont}{\fontfamily{lmr}\selectfont}
\newcommand{\mytilde}{\raise.17ex\hbox{$\scriptstyle\sim$}}
\renewcommand*{\schemerefformat}{%
\fontsize{12pt}{12pt}\color{magenta}\textit%
}
\usepackage{fancyhdr}
\fancypagestyle{main}{ %
\fancyhf{}
\fancyfoot[C]{}
\fancyfoot[R]{\thepage}
\newcommand{\markedsection}[2]{\section[#2]{#2%
\sectionmark{#1}}
\sectionmark{#1}}
\newcommand{\markedchapter}[2]{\chapter[#2]{#2%
\chaptermark{#1}}
\chaptermark{#1}}
\fancypagestyle{plain}{%
\fancyhf{}
\fancyfoot[R]{\thepage}
\renewcommand{\footrulewidth}{0pt}}
\usepackage[nottoc]{tocbibind}
\setcounter{tocdepth}{3}
\newcommand*{\TitleFont}{\mdseries\scshape
\fontsize{43}{1.5em}
\selectfont}
\newcommand*{\AuthorFont}{\sffamily%
\fontsize{25}{15}%
\selectfont}
\newcommand*{\DateFont}{%
\fontsize{19}{1.2em}%
\selectfont}
\newcommand*{\PublishersFont}{%
\fontsize{19}{1.2em}%
\selectfont}
\renewcommand*{\sectionautorefname}{Section}
\renewcommand*{\subsectionautorefname}{Section}
\renewcommand*{\subsubsectionautorefname}{Section}
\newcolumntype{P}%
{>{\centering\arraybackslash}%
m{2cm}%
<{}}
\newcolumntype{C}[1]%
{>{\centering\arraybackslash}%
p{#1}}
\newcolumntype{N}%
{@{}m{0pt}%
@{}}
\usepackage{microtype}

\begin{document}

\frontmatter
\tableofcontents

\mainmatter
\section{Reference to \compound{one}}

Jumbled references to \compound{one}, then \compound{two}, \compound{three} and \compound{four}.  Now repeats of \compound{three} and \compound{one}.

\section{Reference to \compound{four}}

\end{document}

• Welcome to TeX.SX! Jan 5, 2015 at 14:20
• Hi. Not what you ask for but maybe interesting nonetheless: the chemnum package for numbering of chemical compounds has a “showlabels” option for visual checking of labels Jan 5, 2015 at 14:26
• @cgnieder Thanks for the info - I did temporarily attempt to use chemnum with chemstyle but as a complete novice couldn't get my document to compile successfully’. I’ve decided not to update my question with that attempt as I think the problems I had didn’t relate to my original query, but will include if anyone requests. Jan 6, 2015 at 15:35
• BTW: do you know that \compound has an optional + for usage in section headings and the like? With the + option the number is only read but not defined. This is described in the manual for chemcompounds. May 16, 2015 at 14:52

One way to do this would be to patch \compound: the etoolbox package contains commands that allow one to prepend or append code to an already defined command. See this post for a good explanation of this.

In order to get a comma-separated list, we'll patch the command to write its argument to a file whenever it is called:

\usepackage{etoolbox}
\newwrite\myfile
\immediate\openout\myfile=\jobname.foo
\apptocmd{\compound}{\write\myfile{#1,}}{}{}


Here's a small, full example. In this code, \term marks definitions, but also handles indexing automatically.

\documentclass{article}
\usepackage{makeidx}
\usepackage{etoolbox}

\newwrite\myfile
\immediate\openout\myfile=\jobname.foo

\newcommand{\term}[1]{\emph{#1}\index{#1}}
\apptocmd{\term}{\write\myfile{#1,}}{}{}

\makeindex

\begin{document}
The \term{mitochondria} are the powerhouse of the cell. \term{Anaphora}
is the repetition of a word at the start of several phrases. A
\term{circle} is a set of points in the plane equidistant from a
given point, called its \term{center}.\footnote{The distance

If this file is called defn.tex, then after compiling, defn.foo contains a comma-separated list of all of the arguments I provided to \term:
mitochondria,
`