# Main bibliography at the end of the main document and at the end of the subfiles

I already read this article, this article and related, and still couldn't make it. Any help would be appreciated! Long post, so if you want to skip, go to last paragraph, and last bit of Latex code.

This is what I want:

• When compiling my main tex file, have all subfiles compile, and then the global bibliography at the end, once.

• When compiling each subfile individually, have the global bibliography at the end of the subfile text.

Here is my folder structure:

• 01-Int / 01-Introduction.tex
• 02-Pro / 02-Process.tex
• Figures / >SomeFigures.jpg<
• 00-Main.tex
• library.bib

Here is some parts of the 00-Main.tex file:

\documentclass[12pt,a4paper]{article}
\usepackage{cite}       % For citations
\usepackage{etoolbox}
\usepackage{subfiles}
% and other packages

\ifstrequal{\jobname}{\detokenize{00-Main}}{}{%
\AtEndDocument{%
\medskip
\nocite{*}                  % To display bibliography items not used
\bibliography{library}
\bibliographystyle{plain}
}
}

\begin{document}

\maketitle
\subfile{01-Int/Introduction}
\subfile{02-Pro/Process}

\end{document}


And this works well when compiling with the main file, but not with the subfiles, because the ifstrequal returns an error. Here is an example of a subfile text:

\documentclass[../00-main.tex]{subfiles}

\begin{document}

\section{III-IV Group}\label{sec:3-5G}
Bla bla blah\cite{Feifel2018,Runge2018}

\end{document}


Finally, here is the library.bib text a little shortened:

Automatically generated by Mendeley Desktop 1.19.3
Any changes to this file will be lost if it is regenerated by Mendeley.

BibTeX export options can be customized via Options -> BibTeX in Mendeley Desktop

@article{Feifel2018,
abstract = {{\textcopyright} 2018 IEEE. Blablabla},
author = {Feifel, Markus and Ohlmann, Jens and Benick, Jan and Hermle, Martin and Belz, Jurgen and Beyer, Andreas and Volz, Kerstin and Hannappel, Thomas and Bett, Andreas W. and Lackner, David and Dimroth, Frank},
doi = {10.1109/JPHOTOV.2018.2868015},
file = {<SomePath>},
issn = {21563381},
journal = {IEEE Journal of Photovoltaics},
keywords = {III-V on silicon,Metal-organic vapor phase epitaxy (MOVPE),multi-junction solar cell,semiconductor materials},
number = {6},
pages = {1590--1595},
title = {{Direct growth of III-V/Silicon triple-junction solar cells with 19.7{\%} efficiency}},
volume = {8},
year = {2018}
}
@article{Feifel3018,
(...)
}
@article{Runge2018,
(...)
}


I think the best workaround would be to add some kind of an else statement after the ifstrequal, because when I changed the \detokenize{00-Main} to \detokenize{../00-Main} and \bibliography{library} to \bibliography{../library}, it only worked for the subfiles, but not with the main file anymore. Then the condition would look like:

\ifstrequal{\jobname}{\detokenize{00-Main}}{}{%
\AtEndDocument{%
\medskip
\nocite{*}                  % To display bibliography items not used
\bibliography{library}
\bibliographystyle{plain}
}
}>ElseFunctionICouldNotFind<{%
\AtEndDocument{%
\medskip
\nocite{*}
\bibliography{../library}
\bibliographystyle{plain}
}
}


Use the \ifthenelse command from package ifthen.

\documentclass[12pt,a4paper]{article}
\usepackage{cite}       % For citations
\usepackage{etoolbox}
\usepackage{subfiles}
\usepackage{ifthen}

\AtEndDocument{%
\medskip
\nocite{*}  % To display bibliography items not used
\bibliographystyle{plain}
}

\ifthenelse{\equal{\jobname}{\detokenize{00-Main}}}
{\AtEndDocument{\bibliography{library}  }   }
{\AtEndDocument{\bibliography{../library} } }

\begin{document}

\maketitle

\subfile{01-Int/01-Introduction}

\end{document}