# How can I make sure about the order of the loaded packages?

I have a references.bib file, however when I load the paper, I get only one reference in my bibliography list, the other few ones I tried to cite using \cite appear with "?" although the name is cited correctly.

I am aware that this problem has to do with my loaded packages and maybe their sequence. I think part of the error comes from there. For now, I care about having a complete list more than the style.

\documentclass{article}
\usepackage[utf8]{inputenc}
% Required package
\usepackage{amsmath}
\usepackage{multirow}
\usepackage{float}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{threeparttable}
\usepackage{apacite}
\usepackage{multirow}
\usepackage{ulem}
\usepackage[table]{xcolor}
\usepackage{longtable}
\usepackage{pdflscape}
\usepackage{fancyhdr}
\usepackage{afterpage}
\usepackage{geometry}

\begin{document}

\subsection{A}

\cite{Dustmann_1996}.
\cite{Kuhlenkasper_2012}.

\section{A}

\begin{document}
\bibliography{references.bib}
\bibliographystyle{apacite}
\end{document}

\appendix
\section{Appendix}
\setcounter{table}{0}
\renewcommand{\thetable}{A\arabic{table}}

\begin{table}[H]
\caption{Tabulation of x information}
\label{Tabulation of x information}

\end{tabular}
\end{table}

\end{document}



The last \bibliography part appears with errors, e.g.: Citation Kuhlenkasper_2012' undefined on input line 27. \bibliography{references.bib} runaway argument? l.33 \begin{document} can only used in the preamble

This is my references.bib (contains other references too)

@TECHREPORT{Kuhlenkasper_2012,
title = {Who Leaves and When?: Selective Outmigration of Immigrants from Germany},
author = {Kuhlenkasper, Torben and Steinhardt, Max},
year = {2012},
institution = {DIW Berlin, The German Socio-Economic Panel (SOEP)},
type = {SOEPpapers on Multidisciplinary Panel Data Research},
number = {490},
url = {https://EconPapers.repec.org/RePEc:diw:diwsop:diw_sp490}
}

@article{Dustmann_1996,
DOI= {10.2307/1344525},
ISSN = {02664658, 14680327},
URL = {http://www.jstor.org/stable/1344525},
author = {Christian Dustmann and Samuel Bentolila and Riccardo Faini},
journal = {Economic Policy},
number = {22},
pages = {213--250},
publisher = {[Center for Economic Studies, Maison des Sciences de l’Homme, Centre for Economic Policy Research, Wiley]},
title = {Return Migration: The European Experience},
urldate = {2022-07-31},
volume = {11},
year = {1996}
}


• Please make sure you example can be tested by others by just coying your code. Additionally it is best with some sample bibtex data as well. Sep 23 at 11:03
• I'm not away of any loading errors that affect bibliography from the preamble you provide. Please elaborate. Sep 23 at 11:04
• Usually you put the \bibliographystyle first and the \bibliography after that, but that is probably not causing the problem. Generally when some references work but others don't then you forgot to run BibTeX again (you need to run this every time that you add some references if you want them to appear in the pdf), or you did run BibTeX again but there were some errors in the .bib file that prevented the bibliography from being updated. If you got errors (shown in your editor and in the .blg file) then please post them here together with the .bib entries that caused the error. Sep 23 at 11:32
• getting ? for a citation is rather unlikely to be related to the packages loaded or their order. in 999 times out of 1000 it is just a failure to run bibtex (or getting an error in bibtex so a failure to get a generated bibliography) If you want help with an error show the error message, just saying "appears with errors" and not providing any code that produces the error means no one can debug this Sep 23 at 12:00
• Your tex code doesn't make sense. It has two \begin{document}, and then two \end{document}. Sep 23 at 13:54

There were two main issues in the code (apart from some problems with making the MWE, like the two \begin{document} statements and such).

The first is the use of the ulem package for underlining. By default this package redefines \emph (a command used to give emphasis to the argument, by default in italics) to produce underlines. Apacite uses \emph in the bibliography which leads to an error. This can be fixed by using the normalem package option for ulem, i.e., delete \usepackage{ulem} and add \usepackage[normalem]{ulem}.

The second issue is that Apacite does not recognize url in @techreport (it does for @article). Therefore url formatting is not applied to the web address in the Kuhlenkasper_2012 entry. Because that url contains _ another error appears. To fix this you can use howpublished = {\url{...}}. This requires \usepackage{url}.

Another small improvement is an extra set of braces around E in The {E}uropean Experience to prevent automatic lowercasing.

MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
% Required package
\usepackage{amsmath}
\usepackage{multirow}
\usepackage{float}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{threeparttable}
\usepackage{apacite}
\usepackage{multirow}
\usepackage[normalem]{ulem}
\usepackage[table]{xcolor}
\usepackage{longtable}
\usepackage{pdflscape}
\usepackage{fancyhdr}
\usepackage{afterpage}
\usepackage{geometry}
\usepackage{url}

\begin{document}

\subsection{A}

\cite{Dustmann_1996}.
\cite{Kuhlenkasper_2012}.

\section{A}

\bibliography{references.bib}
\bibliographystyle{apacite}

\appendix
\section{Appendix}
\setcounter{table}{0}
\renewcommand{\thetable}{A\arabic{table}}

\begin{table}[H]
\caption{Tabulation of x information}
\label{Tabulation of x information}
\end{table}

\end{document}

@TECHREPORT{Kuhlenkasper_2012,
title = {Who Leaves and When?: Selective Outmigration of Immigrants from Germany},
author = {Kuhlenkasper, Torben and Steinhardt, Max},
year = {2012},
institution = {DIW Berlin, The German Socio-Economic Panel (SOEP)},
type = {SOEPpapers on Multidisciplinary Panel Data Research},
number = {490},
howpublished = {\url{https://EconPapers.repec.org/RePEc:diw:diwsop:diw_sp490}}
}

@article{Dustmann_1996,
DOI= {10.2307/1344525},
ISSN = {02664658, 14680327},
URL = {http://www.jstor.org/stable/1344525},
author = {Christian Dustmann and Samuel Bentolila and Riccardo Faini},
journal = {Economic Policy},
number = {22},
pages = {213--250},
publisher = {[Center for Economic Studies, Maison des Sciences de l’Homme, Centre for Economic Policy Research, Wiley]},
title = {Return Migration: The {E}uropean Experience},
urldate = {2022-07-31},
volume = {11},
year = {1996}
}
`

Result: