# Read in number of citations from aux file

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.

-
Welcome to TeX.SE. From the example, it seems that you are loading the package natbib, but the package is not present. If you load it, please add it. At all, it would be nice if you converted your code into a Minimal (non-)Working Example. –  tohecz Feb 10 '13 at 18:04
Actually, I use LyX and select natbib as the citation style within the Bibliography section of Document->Settings, but "natbib" is not present in the .tex file that is exported by LyX, so I am not sure where I would add it. –  ws_e_c421 Feb 10 '13 at 19:19
By adding "this is the relevant part" to the explanatory text, you do not make your code a MWE as asked you to do above. Please notice that you come here to be helped by people who do so for free, and it would be more than nice if you showed your own effort in making the question proper. My other comment is still valid: NAT@ctr counter is an inner coutner of the package natbib, but the piece of code you show does not contain \usepackage{natbib}. –  tohecz Feb 10 '13 at 19:22
I didn't expect that the explanatory text would make it a MWE. It will take me more than 3 minutes to produce such a document. I am working on one right now. Please have a little more patience. –  ws_e_c421 Feb 10 '13 at 19:30
Sorry, you posted the comment mentioning LyX after I started writing mine and it didn't appear here. It is strange that LyX doesn't export natbib when it exports your document to LaTeX. –  tohecz Feb 10 '13 at 19:34

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}
\openin\auxfile = \maindoc .aux

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

\newif\ifnoteof
\loop

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

-