0

I am using Windows as OS and TeXstudio as editor.

My project has the following structure:

-project
    -myclass.cls
    -main.tex
    -chapter1
        -chapter1.tex
        -bibgraf.bib
        -figures
        -listings
    -chapter2
        -chapter2.tex
        -bibgraf.bib
        -figures
        -listings

I am using the subfiles package in order to compile independently the various chapters of my document. I would like the chapters to have their own references, both when compiling those independently and when compiling the entire document from main.tex. However, now I have to change the \bibliography argument according to whether I am compiling chapterN.tex or main.tex. More specifically, I have to type \bibliography{bibgraf} if I am compiling the former and \bibliography{chapterN/bibgraf} if I am compiling the latter.

How can I use \bibliography{something} so that both matin.tex and chapterN.tex compile successfully?

My MWE:

myclass.cls

%%%%%%%%%%%%%%%%%%%%%%%% Identification %%%%%%%%%%%%%%%%%%%%%%%%
\NeedsTeXFormat{LaTeX2e} % sets the LATEX version for the class to work
\ProvidesClass{myclass} % identification of the class
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%% Preliminary declarations %%%%%%%%%%%%%%%%%%%%%%%%
\LoadClass[a4paper,12pt]{report} % loads the class report

%----------------------- General use packages -----------------------
\RequirePackage[english]{babel} % management of culturally-determined typographical (and other) rules for the document language
\RequirePackage[utf8]{inputenc} % use of non-ASCII characters
\RequirePackage[T1]{fontenc} % use of even more non-ASCII characters (T1 font enconding)
\RequirePackage{float} % include [H] option for figures and tables
\RequirePackage{graphicx} % provides a key-value interface for optional arguments to the \includegraphics command

% Package for customising citations (especially author-year citation schemes) when using BibTeX
\RequirePackage[numbers,square,sectionbib]{natbib}
% numbers: allow numeric citations
% square: enable squared brackets
% sectionbib: to be used with the package chapterbib to add the bibliography to the table of contents as a unnumbered section instead of an unnumbered chapter

% Package that facilitates multiple bibliographies in a LATEX document
\RequirePackage{chapterbib}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%% Options %%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%% More declarations %%%%%%%%%%%%%%%%%%%%%%%%

% The commands define \main as . (same folder)
\providecommand{\main}{.}

% Set graphics path
\graphicspath{{\main/figures/}}

% Set BibTeX style to apsrev4-1
\bibliographystyle{apsrev4-1}

% Change name of bibliography
\addto\captionsenglish{%
    \renewcommand{\bibname}{References}%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

main.tex

\providecommand{\main}{.}

\documentclass{myclass}

\usepackage{subfiles}
\newcommand{\onlyinsubfile}[1]{#1}
\newcommand{\notinsubfile}[1]{}

\begin{document}

\renewcommand{\onlyinsubfile}[1]{}
\renewcommand{\notinsubfile}[1]{#1}

Hallo main

\subfile{chapter1/chapter1}

\end{document}

chapter1.tex

\providecommand{\main}{..}

\makeatletter
\def\input@path{{\main/}}
\makeatother

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

\graphicspath{
    {"\main/chapter1/figures/"}
    {"\main/\main/chapter1/figures/"}
}


\begin{document}
%% my chapter 1 content
Hallo, this is a test:
\begin{equation}
\label{eq:sec1:1}
\int dx =0
\end{equation}

\onlyinsubfile{this only appears if chapter1.tex is compiled (not when main.tex is compiled)}
\notinsubfile{this only appears if main.tex is compiled (not when chapter1.tex is compiled)}

This is a reference test: \cite{anderson}

\onlyinsubfile{Call reference}
\bibliography{bibgraf}
%% 
\end{document}

bibgraf.bib

@CONTROL{apsrev41Control,author="00",editor="1",pages="1",title="0",year="0"}

@book{anderson,
    title={Fundamental of Aerodynamics},
    author={Anderson, Jr., J. D.},
    isbn={9781259010286},
    series={McGraw-Hill Series in Aeronautical and Aerospace engineering},
    url={https://books.google.nl/books?id=xVsiAwAAQBAJ},
    year={2011},
    edition={5},
    publisher={McGraw-Hill}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.