# Using psfrag with LaTeX counters (insert counter number directly in eps figure)?

I am writing an organic chemistry dissertation using (pdf)LaTeX. The content of the thesis contains a large amount of numbered chemical structures. The chemical structure drawings were generated using ChemDraw and exported to eps format. Since there are literally hundreds of numbered structures, I wrote a macro that uses a custom counter to keep track of the numbers:

\newcounter{compound}
\def\thecompound{\textbf{\thechapter.\arabic{compound}}}
\newcommand{\cmp}[1]{\refstepcounter{compound}\label{cmp:#1}\textsf{\scriptsize{\thecompound}}}


Each time I want to number a compound, I simply write the command (which automatically increments the counter):

\cmp{uniquestring}


The problem I am having is finding an efficient way to put the compound numbers on the structural drawings (eps figures). What I am using right now is a floating text block over the image that I have to manually align. Here is an example of one of my figures (I defined a custom float called a Scheme):

\begin{Scheme}[h]
\centering \includegraphics[scale=0.8]{chp_diazobkg/images/shioiritwo}
\begin{textblock}{1}(1.65,-0.8) \cmp{aax} \end{textblock}
\begin{textblock}{1}(7,-1) \cmp{aay} \end{textblock}
\begin{textblock}{1}(13,-1) \cmp{aaz} \end{textblock}
\begin{textblock}{1}(17.5,-0.5) \cmp{aba} \end{textblock}
\caption{Facile [1,3]-Brook rearrangement of $\alpha$-keto silane intermediate \ref{cmp:aay}.}
\label{sch:shioiritwo}
\end{Scheme}


Ideally what I would like to be able to do is use a package like pstool/psfrag so that the unique identifiers could be built into the eps file and get replaced with the compound numbers. Using the pstool package, I am able to do text replacements in eps figures, but when I try to do a replacement with a counter psfrag chokes and can't generate the figure. The code below works fine for me so I know the package is installed correctly.

\psfragfig[scale=0.8]{test}{
\psfrag{[aaa]}{testing}
}


However, when I try to do something like this no luck (in fact the whole package breaks and no image is generated):

\psfragfig[scale=0.8]{test}{
\psfrag{[aaa]}{\cmp{xxx}}
}


Or even something simpler like this just gives ?? for the replaced text:

\psfragfig[scale=0.8]{test}{
\psfrag{[aaa]}{\ref{somefigure}}
}


Any suggestions would be greatly appreciated.

I have posted a minimal working example with EPS files at: https://sites.google.com/site/vrendina/latex

-
I guess it's too late now but there are packages like chemnum for this task. –  clemens Jan 19 '13 at 7:48
The approach taken by my own chemscheme package is not dissimilar to the one you outline (today I would also recommend chemnum, but that's a different issue). Could you provide a small MWE of your input (i.e a complete document), including an EPS somewhere we can get hold of for testing? –  Joseph Wright Jan 19 '13 at 8:23
Works for me, even with the same string [aaa]. The ?? might mean that the label somefigure is not defined. At this point, you really have to provide a MWE. –  mafp Jan 19 '13 at 10:11
Assuming you're using \psfragfig as defined by auto-pst-pdf then your syntax is wrong. The last argument is optional: \psfragfig[scale=0.8]{scheme}[\psfrag{TMP}{\cmp{xxx}}]. –  clemens Jan 19 '13 at 13:36
I think I chose not to use chemnum because I didn't want to have to maintain a list of 100+ compounds at the beginning of each chapter. If I moved a scheme from the end of the chapter to the beginning every number would change and I wanted that updated automatically. –  Victor Rendina Jan 19 '13 at 17:09

I think this problem arose because the pstool package creates a separate document containing each figure, and then compiles this with latex -> dvips -> ps2pdf to create a new version of the figure with psfrag replacements included.

According to section 5.2 of the pstool package documentation, support for cross referencing within graphics was added in version 1.5, which I think is slightly newer than the question. The following example works on my machine, so I think the solution is to update your TeX distribution, if you have not already done so.

\documentclass{article}
\usepackage[process=all]{pstool}
\begin{document}
$$a = b \label{eqn}$$
\begin{center}
\psfragfig[scale=0.7]{myfig}{\psfrag{mouse}[][]{\ref{eqn}}}
\end{center}
\end{document}


The postscript code for myfig.eps is as follows:

%!PS-Adobe-2.0
%%Title: myfig.eps
%%BoundingBox: 0 0 80 50
%%Orientation: Portrait