# fancylabel not incrementing counter if table is include with ltxtable

I'm using ltxtable to use flexible width, tabularx, X columns in a long-table. I'm also using the chemschemex/fancylabel package to give me sweet sweet automatic numbering of my chemical structures.

However, if I include a table containing \fancylabel[<family>]{label} commands via the \LTXtable{<width>}{<file>} method the counter for the fancylabels doesn't increment. And so, all labels end up with the same number.

Including the same table, just with the X columns substituted for l columns, directly in the document or via an \input{} command gives the correct sequential numbering.

None of the inclusion method effects the numbering throughout the document, only that of the table. The table also recognises previously numbered structures and assigns the correct number to them, but then for the next structure fails to change the counter.

\documentclass[a4paper]{article}
\usepackage{graphicx}
\usepackage{siunitx}
\usepackage{chemmacros}
\chemsetup{modules = nomenclature}

\let\etoolboxforlistloop\forlistloop % save the good meaning of \forlistloop
\usepackage{chemschemex}
\let\forlistloop\etoolboxforlistloop % restore the good meaning of \forlistloop

\usepackage{threeparttable}
\usepackage{threeparttablex}
\usepackage{longtable}
\usepackage{tabularx}
\usepackage{ltxtable}
\usepackage{collcell}
\usepackage{afterpage}

\begin{filecontents}{table.tex}
\begin{ThreePartTable}
\begin{TableNotes}
\item [a] A note.
\end{TableNotes}
\begin{longtable}[c]{%
@{}%
>{\bfseries}l<{}%
>{\collectcell\iupac}X<{\endcollectcell}% This is the column with the tabularx X column when using ltxtable
S%
@{}%
}
\caption{Caption!}\\
\multicolumn{2}{@{}l}{\textbf{Molecule}} & {\pKa}\\

\insertTableNotes
\endlastfoot

\multicolumn{2}{@{}l}{\emph{Pyridines}}                             \\
\fancylabel[CSX]{pyr}      &  pyridine            &  5.23           \\
\fancylabel[CSX]{2me-pyr}  &  2-methylpyridine    &  5.97           \\
\multicolumn{2}{@{}l}{\emph{Diazines}}                              \\
\fancylabel[CSX]{pdz}      &  pyridazine          &  2.24\tnote{a}  \\
\multicolumn{2}{@{}l}{\emph{Phenols}}                               \\
\fancylabel[CSX]{phe}      &  phenol              &  9.99           \\
\fancylabel[CSX]{23me-phe} &  2,3-dimethylphenol  &  10.50          \\
\end{longtable}
\end{ThreePartTable}
\end{filecontents}

\begin{document}
This is a label: \fancylabel[CSX]{phe}\\
This is an image using chemschemex: \struct[text]{pyr}\\
This is the referenced number for pyridine: \structref{pyr}\\

% No sweet automatic numbering, but at least I get X columns.

\afterpage{\clearpage%
\LTXtable{\linewidth}{table.tex}
}

% Sweet sweet automatic numbering, but no lovely X columns.

% \afterpage{\clearpage%
%   \begin{ThreePartTable}
%     \begin{TableNotes}
%       \item [a] A note.
%     \end{TableNotes}
%     \begin{longtable}[c]{%
%       @{}%
%       >{\bfseries}l<{}%
%       >{\collectcell\iupac}l<{\endcollectcell}% This is the column with the tabularx X column when using ltxtable
%       S%
%       @{}%
%     }
%       \caption{Caption!}\\
%       \multicolumn{2}{@{}l}{\textbf{Molecule}} & {\pKa}\\
%
%       \insertTableNotes
%       \endlastfoot
%
%       \multicolumn{2}{@{}l}{\emph{Pyridines}}                             \\
%       \fancylabel[CSX]{pyr}      &  pyridine            &  5.23           \\
%       \fancylabel[CSX]{2me-pyr}  &  2-methylpyridine    &  5.97           \\
%       \multicolumn{2}{@{}l}{\emph{Diazines}}                              \\
%       \fancylabel[CSX]{pdz}      &  pyridazine          &  2.24\tnote{a}  \\
%       \multicolumn{2}{@{}l}{\emph{Phenols}}                               \\
%       \fancylabel[CSX]{phe}      &  phenol              &  9.99           \\
%       \fancylabel[CSX]{23me-phe} &  2,3-dimethylphenol  &  10.50          \\
%     \end{longtable}
%   \end{ThreePartTable}
% }

\end{document}


Ideally I'd like to keep the autonumbering of my compounds. There are a few... If I have to remove a feature to get this to work it'll be the X columns. I'll replace the X column with a p{} column. I can determine the width of the p column with a prelininary run through where I include my tables with the \LTXtable{<width>}{<file>} command, get the width from thw .aux file, and then just use an \input command. I'd just rather not have to do this manually for a large document, with potentially many tables.

Aside
I know of the conflict between etoolbox and etextools (and so autonum and fancylabel) and have implemented the fix here. While the MWE doesn't include biblatex my full document does. I've tested the MWE with and without the fix and it makes no difference.

• I'd like to tag chemschemex and fancylabel too, but I don't have enough reputaion to create the tags. – tmgriffiths Aug 25 '17 at 4:52
• regarding “sweet sweet automatic numbering of my chemical structures” – do you know the chemnum package? – clemens Sep 10 '17 at 12:25
• Hi @clemens! Yes I knew about chemnum, but to be honest I hadn't seen how in-depth it went for numbering. I chosechemschemex rather than chemnum, chemscheme, etc. as I wanted to do the auto-numbering in the schemes. All my previous attempts with chemnum etc. and the ps-frag approach failed (I'm using LuaLaTex and fontspec). So, I chose chemschemex to try making my schemes. It typesets figures and the numbers with tikz in matrices for auto-numbering and alignment (no psfrag). The reason I'm fancylabel is because it was written as the slave label package for chemschemex. – tmgriffiths Sep 12 '17 at 2:37
• I'm not especially attached to fancylabel. If I could re-write chemschemex to use chemnum instead I would do that. That in itself seems fairly simple as I'd just need to replace the fancyref calls in chemeschemex. What is more difficult is that chemeschemex uses some fancyref internal macros to sort and parse comma-seperated sublabels. Unfortunately I don't know the expl3 syntax, and that'll be where I get stuck, I suspect. I'll read the chemnum documentation to see how I go. – tmgriffiths Sep 12 '17 at 2:38
• I might cite your last sentence once in while :) – clemens Oct 12 '17 at 8:16