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):



\fancypagestyle{main}{ %



\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}}

  • 1
    Welcome to TeX.SX! Jan 5, 2015 at 14:20
  • 1
    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
    – cgnieder
    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.
    – chemdent
    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.
    – cgnieder
    May 16, 2015 at 14:52

1 Answer 1


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:


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





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
itself is called the \term{radius}.} 


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:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.