# Unsorted citations and floats

In attempting to answer this question Citations in order of appearance it's clear that unsorted citation schemes (ones in which the citations are numbered simply by order of appearance) can be easily messed up with floats. Here is a minimal example exhibiting the problem.

\documentclass[12pt,oneside]{book}
\usepackage[super,sort&compress]{natbib}
\bibpunct{[}{]}{,}{s}{}{}
\bibliographystyle{unsrtnat}
\usepackage{hyperref}
\usepackage[demo]{graphicx}
\usepackage{lipsum}
\usepackage{filecontents}
\usepackage{caption}
\begin{filecontents}{\jobname.bib}
@incollection{Baauw2001,
Author = {Sergio Baauw},
Booktitle = {Proceedings of the 25th Annual Boston University Conference on Language Development},
Editor = {A. H.-J. Do and L. Dom{\'\i}nguez and A. Johansen},
Pages = {82-93},
Title = {Expletive determiners in child Dutch and Spanish},
Year = {2001}}

@article{barker1998,
Author = {Chris Barker},
Journal = {Natural Language \& Linguistic Theory},
Pages = {679-717},
Title = {Partitives, Double Genitives and Anti-Uniqueness},
Volume = {16},
Year = {1998}}

@book{Berwick1985,
Author = {Berwick, Robert C.},
Publisher = {MIT Press},
Title = {Acquisition of syntactic knowledge},
Year = {1985}}

@phdthesis{Carlson1977,
Author = {Carlson, Gregory N.},
School = {University of Massachusetts, Amherst},
Title = {Reference to Kinds in {E}nglish},
Year = {1977}}

\end{filecontents}

\begin{document}

\chapter{A chapter}
\lipsum
Some text.\cite{Carlson1977}
\lipsum[3]
\cite{Barker1998,Baauw2001}
\begin{figure}[tbp]
\centering
\includegraphics[width=.5\textwidth]{demo.jpg}
\captionof{figure}[A figure from Berwick]{A figure from \protect\cite{Berwick1985}}
\end{figure}
\lipsum[2]
\bibliography{\jobname}
\end{document}


Because the citation in the figure is the fourth cited in the document source, it appears with number [4]. However, because the figure has floated to the top of the page, the number now appears out of order. I don't use this citation style, but I'm wondering if there is any way around it short of the following two (non-)solutions:

1. Avoid using citations in captions altogether.
2. Avoid using floats for figures that have citations and use the \captionof command of the caption package (or similar methods) to add the caption.
-

I tried the following which seems to work in the MWE. I don't know what will happen in an other document.

\documentclass[12pt,oneside]{book}
\usepackage[super,sort&compress]{natbib}
\bibpunct{[}{]}{,}{s}{}{}
\bibliographystyle{unsrtnat}
\usepackage{hyperref}
\usepackage[demo]{graphicx}
\usepackage{lipsum}
\usepackage{filecontents}
\usepackage{caption}

\begin{filecontents}{\jobname.bib}
@incollection{Baauw2001,
Author = {Sergio Baauw},
Booktitle = {Proceedings of the 25th Annual Boston University Conference on Language Development},
Editor = {A. H.-J. Do and L. Dom{\'\i}nguez and A. Johansen},
Pages = {82-93},
Title = {Expletive determiners in child Dutch and Spanish},
Year = {2001}}

@article{barker1998,
Author = {Chris Barker},
Journal = {Natural Language \& Linguistic Theory},
Pages = {679-717},
Title = {Partitives, Double Genitives and Anti-Uniqueness},
Volume = {16},
Year = {1998}}

@book{Berwick1985,
Author = {Berwick, Robert C.},
Publisher = {MIT Press},
Title = {Acquisition of syntactic knowledge},
Year = {1985}}

@phdthesis{Carlson1977,
Author = {Carlson, Gregory N.},
School = {University of Massachusetts, Amherst},
Title = {Reference to Kinds in {E}nglish},
Year = {1977}}

\end{filecontents}
\makeatletter
\renewcommand\NAT@sort@cites[1]{%
\let\NAT@cite@list\@empty
\@for\@citeb:=#1\do{\expandafter\NAT@star@cite\@citeb\@@}%
\if@filesw
\expandafter\write\expandafter\@auxout
\expandafter{\expandafter\string\expandafter\citation\expandafter{\NAT@cite@list}}%
\fi
\@ifnum{\NAT@sort>\z@}{%
\expandafter\NAT@sort@cites@\expandafter{\NAT@cite@list}%
}{}%
}%
\makeatother
\begin{document}
\listoffigures
\chapter{A chapter}
\lipsum
Some text.\cite{Carlson1977}
\lipsum[3]
\cite{Barker1998,Baauw2001}
\begin{figure}[tbp]
\centering
\includegraphics[width=.5\textwidth]{demo.jpg}
\caption[A figure from]{A figure from \cite{Berwick1985}}
\end{figure}
\lipsum[2]
\bibliography{\jobname}
\end{document}

-
Very nice. That seems to work quite well. I messed around with different placements of the float and it seems to work properly. –  Alan Munn Oct 22 '11 at 19:38
There's additional space before the citation superscript in the caption. –  lockstep Oct 22 '11 at 19:44
@lockstep: Thanks. I didn't saw it. But now I have no idea. –  Marco Daniel Oct 22 '11 at 19:52