2

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}
\defbibheading{bibintoc}[\bibname]{%
\addchap{#1}%
}

\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$}}
\definecolor{ilinks}{rgb}{0.356,0.064,0.796}
\renewcommand*{\schemerefformat}{%
\fontsize{12pt}{12pt}\color{magenta}\textit%
}
\captionsetup[figure]{labelfont={color=ilinks}}
\captionsetup[scheme]{labelfont={color=ilinks}}
\captionsetup[table]{labelfont={color=ilinks}}
\addtokomafont{part}{\fontsize{43}{1.5em}\mdseries\scshape}
\addtokomafont{chapter}{\Huge\scshape}
\addtokomafont{section}{\mdseries\fontsize{19}{22}\normalfont\itshape}
\addtokomafont{subsection}{\fontsize{15}{18}\selectfont\bfseries}
\addtokomafont{subsubsection}{\fontsize{14}{17}\selectfont\mdseries\scshape}
\usepackage{fancyhdr}
\fancypagestyle{main}{ %
\fancyhf{}
\fancyhead[L]{\rightmark}
\fancyhead[R]{\leftmark}
\fancyfoot[C]{}
\fancyfoot[R]{\thepage}
\renewcommand{\headrulewidth}{0.4pt}}
\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{\headrulewidth}{0pt}
\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}      
\usepackage[colorlinks,linkcolor=black,citecolor=black]{hyperref}
\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}
  • 1
    Welcome to TeX.SX! – Paul Gessler Jan 5 '15 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 – clemens Jan 5 '15 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 '15 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. – clemens May 16 '15 at 14:52
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:

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

\printindex 
\end{document}

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,
Anaphora,
circle,
center,
radius,

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.