I came accross the same problem you did when i compiled my first multi-file document using the subfiles package.
Since I am not a long term latex user i do not fully understand the mechanics of the problem but I suspect the problem is that when you compile the "slave" file (in your case 'subpiece1.tex') your compiler searches for the custom package in the same directory as 'subpiece1.tex' and the other default tex directories.
I managed to solve the problem by changing the \usepackage{} command to also include a relative path that will be common for both the "master" and the "slave" .tex files.
What you need to do:
- Add a folder in your home directory for the master document. i.e.
your home directory should have folders: master folder (containing
main.tex), tex folder, images folder.
- Edit your main.tex folder so that the \usepackage{} command includes the relative path to 'styling.sty' (it should read
\usepackage{../styling} with no file extension)
- If you did step one correctly 'styling.sty' will have the same relative path from 'main.tex' and 'subpiece1.tex' (the relative path
for both is one folder up. This is achieved by '../' in the
\usepackage{} command)
- Update all the other relative file paths so they read as required.
main.tex now reads
\documentclass[11pt,letterpaper]{article}
\usepackage{../styling} %includes \usepackage{subfiles}
\begin{document}
%\maketitle (I just removed these because for the demonstartion i didnt actually need them)
%\tableofcontents
\subfile{../tex/subpiece1}
\end{document}
subpiece1 now reads
\documentclass[../master/main.tex]{subfiles}
% again I just removed the graphics path because I have no need for it
\begin{document}
\section{sectiontitle}
%Images and text
\end{document}
I personally prefer to group my preamble.sty into the same folder as my main.tex but the basic idea is the same. I believe any path would be fine as long as the relative paths are the same for both the 'main.tex' and 'subpiece1.tex' files.
I also suspect there are better/more elegant ways of getting around this problem but this worked for me so far.
Sub-bibliographies work with subfiles
from version 1.5 onwards, available from Github or from CTAN. See its documentation. Note that it is no longer necessary to manipulate the path of files oneself. Below I show how to typeset sub-bibliographies with package
(A) chapterbib
(B) bibunits
(C) biblatex
For each of these scenarios, we consider two file structures.
(1) bib files in the top directory
main.tex
bib1.bib
bib2.bib
chapter1/chapter1.tex
chapter2/chapter2.tex
(2) bib files in the subdirectories
main.tex
chapter1/bib1.bib
chapter1/chapter1.tex
chapter2/bib2.bib
chapter2/chapter2.tex
The bib files contain the following entries.
% bib1.bib
@book{A,
title={The meaning of A},
author={A. Alpha},
year=2019,
publisher={Apublisher}
}
% bib2.bib
@book{B,
title={The meaning of B},
author={B. Beta},
year=2019,
publisher={Bpublisher}
}
(A1) Package chapterbib
, bib files in main directory
% main.tex
\documentclass{report}
\usepackage{chapterbib}
\usepackage{subfiles}
\begin{document}
\subfileinclude{chapter1/chapter1}
\subfileinclude{chapter2/chapter2}
\end{document}
% chapter1/chapter1.tex
\documentclass[../main]{subfiles}
\begin{document}
\chapter{First chapter}
My references: \cite{A}
\bibliographystyle{alpha}
\bibliography{../bib1,../bib2}
\end{document}
% chapter2/chapter2.tex
\documentclass[../main]{subfiles}
\begin{document}
\chapter{Second chapter}
My references: \cite{B}
\bibliographystyle{alpha}
\bibliography{../bib1,../bib2}
\end{document}
To typeset main.tex
, run
pdflatex main
bibtex chapter1/chapter1
bibtex chapter2/chapter2
pdflatex main
pdflatex main
To typeset one of the chapters separately, run
cd chapter1
pdflatex chapter1
bibtex chapter1
pdflatex chapter1
pdflatex chapter1
(A2) Package chapterbib
, bib files in sub-directory
Like (A1), but change the \bibliography
commands to \bibliography{bib1}
in chapter1.tex
and to \bibliography{bib2}
in chapter2.tex
.
(B1) Package bibunits
, bib files in main directory
% main.tex
\documentclass{report}
\usepackage{bibunits}
\defaultbibliographystyle{apalike}
\usepackage{subfiles}
\begin{document}
\subfile{chapter1/chapter1}
\subfile{chapter2/chapter2}
\end{document}
% chapter1/chapter1.tex
\documentclass[../main]{subfiles}
\begin{document}
\begin{bibunit}
\chapter{First chapter}
My references: \cite{A}
\putbib[\subfix{../bib1},\subfix{../bib2}]
\end{bibunit}
\end{document}
% chapter2/chapter2.tex
\documentclass[../main]{subfiles}
\begin{document}
\begin{bibunit}
\chapter{Second chapter}
My references: \cite{B}
\putbib[\subfix{../bib1},\subfix{../bib2}]
\end{bibunit}
\end{document}
To typeset the main document, run
pdflatex main
bibtex bu1
bibtex bu2
pdflatex main
pdflatex main
To typeset the chapters separately, run
cd chapter1
pdflatex chapter1
bibtex bu1
pdflatex chapter1
pdflatex chapter1
(B2) Package bibunits
, bib files in sub-directory
Like (B1), but change the \putbib
commands to \putbib[\subfix{bib1}]
in chapter1.tex
and to \putbib[\subfix{bib2}]
in chapter2.tex
.
(C1) Package biblatex
, bib files in main directory
% main.tex
\documentclass{report}
\usepackage{biblatex}
\usepackage{subfiles}
\bibliography{bib1,bib2}
\begin{document}
\subfileinclude{chapter1/chapter1}
\subfileinclude{chapter2/chapter2}
\end{document}
% chapter1/chapter1.tex
\documentclass[../main]{subfiles}
\begin{document}
\newrefsection
\chapter{First chapter}
My references: \cite{A}
\printbibliography
\end{document}
% chapter2/chapter2.tex
\documentclass[../main]{subfiles}
\begin{document}
\newrefsection
\chapter{Second chapter}
My references: \cite{B}
\printbibliography
\end{document}
To typeset the main document, run
pdflatex main
biber main
pdflatex main
pdflatex main
To typeset the chapters separately, run
cd chapter1
pdflatex chapter1
biber chapter1
pdflatex chapter1
pdflatex chapter1
(C2) Package biblatex
, bib files in sub-directory
Like (C1), but replace the \bibliography
command in main.tex
by \bibliography{chapter1/bib1,chapter2/bib2}
.
Note on filenames and their paths
In general, filenames (with an optional path) have to be surrounded by \subfix{...}
, as demonstrated in (B1) and (B2). The \bibliography
command has this \subfix
magic already built-in, therefore \subfix
is not needed in (A1), (A2), (C1) and (C2) (in fact, it rather would mess up things).
Best Answer
For posterity, I was having the same issue - and the answer from 'bibliographies when using subfiles' didn't work for me because I wasn't using
biblatex
and didn't have the\printbibliography
command.I found a nice, working solution at latex-community.org, slightly modified*:
main.tex:
tex/sub.tex:
*Modifications are to let a bibliography file in a different directory than the subfile (
sub.tex
) work. If yoursub.tex
file is in the same directory asms.tex
andbibliography_file
then you don't need this.