# Sort thebibliography by citation order when References is located before Appendix

Edit: Corrected MWE to include \natbib and removed superfluous code.

I am using Overleaf compiled with XeLaTeX to write my document. I am using the solution given here to input and order my references as this gives more customisation to the bibliography list compared with other solutions I've tried.

Following the References list are my Appendices which can also include references. Currently, my solution (MWE below) correctly assigns a reference number to the reference in the Appendix, but it does not reproduce the reference in the bibliography list.

According to this post, the issue is that the system has not compiled enough times although its my understanding that Overleaf compiles the required number of times to sort these issues.

Is there any way I can amend the below code or edit Overleaf settings such that references in the Appendix are correctly listed in the bibliography?

\documentclass[english, footlines=2, fontsize=11pt, DIV=12, twoside, numbers=noendperiod]{scrartcl}
\usepackage{natbib}

% Reference Set Up

\newcounter{mycite}
\newtoks\citetoks
\makeatletter
\DeclareRobustCommand\unscite[1]{%
\@ifundefined{uns@cite#1}
{\refstepcounter{mycite}\label{citelabel@#1}%
\expandafter\xdef\csname uns@cite#1\endcsname{\arabic{mycite}}%
\toks\z@=\expandafter{\the\citetoks}%
\toks\tw@=\expandafter\expandafter\expandafter{%
\csname uns@bibitem#1\endcsname}%
\edef\@tempcite{\the\toks\z@\the\toks\tw@}%
\global\citetoks=\expandafter{\@tempcite}%
}{}[\@nameuse{uns@cite#1}]}
\newcommand{\mybibitem}[2]{%
\@namedef{uns@bibitem#1}{\bibitem[\ref{citelabel@#1}]{#1}#2}}
\makeatother

\begin{document}

\section{Initial Section}
Initial mention of reference \unscite{3rdTest} then \unscite{2ndTest}

\mybibitem{1stTest}{Here is the 1st reference}
\mybibitem{2ndTest}{here is the 2nd reference}
\mybibitem{3rdTest}{here is the 3rd reference}

\bibliographystyle{plain}
\renewcommand\refname{References}
\begin{thebibliography}{\arabic{mycite}}
\itemsep1em
\the\citetoks
\footnotesize
\end{thebibliography}

\appendix
\section{Appendix}
Mention of reference in Appendix \unscite{1stTest}

\end{document}


• If I understand your document's setup correctly, you would like to ask if and how the list of citation tokens could be written to the aux file at the \end{document} stage -- and read back in at the \begin{document} stage during the next compilation run. Please confirm. (Do note that the issue you are experiencing is not caused by an inssufficient number of compilation runs.) Incidentally, in view of the fact that BibTeX isn't being used, what's the purpose of the \bibliographystyle{plain} statement? – Mico Jan 25 at 12:23
• Apologies, the MWE was not quite complete, \bibliographystyle{plain} is because I'm using natbib to control some of the formatting of the reference list. In response to your question, that is exactly what I would like to do. Essentially, whatever is required for the References list to be complete, even when generated before references made in the following sections. – Scott Jan 25 at 13:20
• With that approach you need to write out your bib entries to a temporary file and input it later. tex.stackexchange.com/q/458422/35864 shows a slightly different approach (with different interface) that automatically makes use of the .aux file and should work even with citations in the appendix. – moewe Jan 25 at 14:58
• And from that, where would one express that only the used references are to be shown in the references list? – Scott Jan 26 at 0:13
• The code contains the line \ifdefvoid{\lodbib@notcited} if you remove it and its true and false branches, the bibliography will ignore entries that were not cited. – moewe Jan 26 at 6:41