[Tex/LaTex] Read in number of citations from aux file

auxiliary-filesbibliographiesexternal filessubdividing

In the aux file produced by latex, I see the following line:

\newlabel{LastBibItem}{{30}{10}{}{}{}}

How can I pull the 30 (the total number of citations) into a counter in the preamble of another document?

Here is a sample document:

\documentclass{article}
\usepackage[numbers]{natbib}

\usepackage{etoolbox}
\makeatletter
\apptocmd{\thebibliography}{\global\c@NAT@ctr 30\relax}{}{}
\makeatother

\begin{document}

Here we cite reference \citealp{firstCite}.

\begin{thebibliography}{99}
    \bibitem{firstCite} Dow, W. \& Jones, E.A.,
     {\it Wall Street Journal},
     March 29, 1929.
\end{thebibliography}

\end{document}

Running pdflatex on that document, the sentence reads "Here we cite reference 31." How can I replace the 30 on the \apptocmd line with a number taken by parsing the aux file from the other document for the LastBibItem number? (Or what is an alternate way of shifting the reference count by the number of items in the bibliography of another document?). (EDIT: I feel like it should possible to copy the contents of xcite.sty into the preamble of my document and make only a small change to increment a counter when \bibcite is found in \XC@test, but I have not been able to get the syntax right myself so far).

I'll explain why I am trying to do this in case there is a different solution or in case someone trying to do the same thing finds this. I am trying to create a supplement to another document that continues the bibliography numbering of the main document but restarts the label numbering (and prepends an "S" to the labels). I use the xr and xcite packages to reference the labels and citations of the original document. I use a command with the etoolbox package to shift the natbib bibliography number to the right starting value (I don't totally understand the command — I just took from another answer online). So here is the relevant part of my preamble:

\usepackage[numbers]{natbib}
\def\maindoc{mymaindoc}

\usepackage{xr}
\externaldocument{\maindoc}

\usepackage{xcite}
\externalcitedocument[M-]{\maindoc}

\renewcommand{\thefigure}{S\arabic{figure}}

\usepackage{etoolbox}
\makeatletter
\apptocmd{\thebibliography}{\global\c@NAT@ctr 30\relax}{}{}
\makeatother

with mymaindoc.tex being my main document and mymaindoc.aux already created. This works fine, starting the citation numbers in the document at 31. I'd just like to read the LastBibItem number from the aux file, so that I don't have to keep it updated by hand. I don't know if it matters, but I'd like the solution to work with pdflatex, natbib, and bibtex because those are what I use right now.

Best Answer

Okay, after playing around with the syntax a lot, I got something to work by modifying the code in xcite (which loops through the aux file to pull out all of the \bibcite items) so that it just counts the number of \bibcite items in the aux file. Here is my modified version of the sample document, with added code between the lines beginning with %%. I don't have much experience with writing TeX macros, so I'd appreciate any suggestions for improvement.

\documentclass{article}
\usepackage[numbers]{natbib}

\newcount\bibcounter
\bibcounter=0

%% start citation counting
\def \maindoc {mymaindoc}
\newread\auxfile
\openin\auxfile = \maindoc .aux

\long\def\bibcitecheck#1#2\bibcitecheckstop{%
\ifx#1\bibcite
\advance \bibcounter by 1
\fi
}

\newif\ifnoteof
\loop
\read\auxfile to \holder

\ifeof\auxfile
\noteoffalse
\else
\expandafter\bibcitecheck\holder...\bibcitecheckstop
\noteoftrue
\fi

\ifnoteof
\repeat

\closein\auxfile
%% End citation counting
\usepackage{etoolbox}
\makeatletter
\apptocmd{\thebibliography}{\global\c@NAT@ctr \bibcounter\relax}{}{}
\makeatother

\begin{document}

Here we cite reference \citealp{firstCite}.

\begin{thebibliography}{99}
    \bibitem{firstCite} Dow, W. \& Jones, E.A.,
     {\it Wall Street Journal},
     March 29, 1929.
    \end{thebibliography}

\end{document}
Related Question