2

I am writing a long report that needs the help of the subfiles package to break it into individual sections. It works like a charm until I further split one of the subfiles into two. Previously, the \ifSubfilesClassLoaded command helps distinguish whether a given file is compiled from main or subfiles shown in this thread. But when I get two levels of subfiles, it can no longer tell which level of subfiles it is working on.

Consider a hierarchy of files like this:

main.tex
myBib.bib
sections/section1.tex
materials/a_complex_table.tex

Here is a MWE:

%%%%%% main.tex
\documentclass[10pt]{article}

\usepackage[numbers,sort&compress]{natbib} \usepackage{subfiles} \begin{document} \section{Section One} \subfile{sections/section1.tex}

\bibliographystyle{ieeetr}
\bibliography{myBib}

\end{document}

%%%%%% section1.tex % !TeX root = section1.tex \documentclass[../main.tex]{subfiles} \begin{document} I like to cite \cite{A}

\subfile{../materials/a_complex_table.tex}

\ifSubfilesClassLoaded{
    \bibliographystyle{ieeetr}
    \bibliography{../myBib}
}{}

\end{document}

%%%%%% a_complex_table.tex % !TeX root = a_complex_table.tex \documentclass[../main.tex]{subfiles} \begin{document} \begin{table}[!htb] \begin{tabular}{ l r } Name & Reference \ A & \cite{A} \ B & \cite{B} \ G & \cite{G} \end{tabular} \end{table} \ifSubfilesClassLoaded{ \bibliographystyle{ieeetr} \bibliography{../myBib} }{} \end{document}

and my bibfile:

@book{A,
    title={The meaning of A},
    author={A. Alpha},
    year=2020,
    publisher={Apublisher}
}
@book{B,
    title={The meaning of B},
    author={B. Beta},
    year=2021,
    publisher={Bpublisher}
}
@book{G,
    title={The meaning of G},
    author={G. Gamma},
    year=2022,
    publisher={Gpublisher}
}

Typesetting main.tex and a_complex_table.tex both are fine, but compiling my section1.tex executes \bibliography{../myBib} twice since \ifSubfilesClassLoaded doesn't distinguish between subfiles. How can I fix this duplication?

enter image description here

Shixi
  • 23
  • Welcome to tex.sx. – barbara beeton Jul 17 '23 at 11:55
  • 1
    For future reference: when preparing MWEs, please avoid unnecessary directory structures. You are just making extra busy work for people who are trying to help you. – Willie Wong Jul 17 '23 at 16:06
  • 2
    @WillieWong In fact, it is helpful, and necessary, to provide a MWE with a directory structure similar to the full project. This way we can test that TeX and especially BibTeX finds all files, no matter which file is compiled. With the search mechanics and expansion mechanisms involved, I'd never claim that a solution works by testing it just with all files in the same directory. – gernot Jul 17 '23 at 17:13

2 Answers2

5

If your goal is to print the complete bibliography at the end of every subfile, a simpler thing to do is the following:

  1. Remove all \biblio... and \IfSubfil... commands from your current files.
  2. Change the main.tex file to read
\documentclass[10pt]{article}

\usepackage[numbers,sort&compress]{natbib} \usepackage{subfiles}

\bibliographystyle{ieeetr} % <===== new \AtEndDocument{\bibliography{myBib}} % <=== new

\begin{document} \section{Section One} \subfile{sections/section1.tex}

\end{document}

  1. For building the subfiles, one has to do one thing extra when using bibtex. Either:
    • Run bibtex from the main folder: so bibtex sections/section1
    • Or add the main folder to the search path when running bibtex , so from within the folder sections/ you can run BIBINPUTS=../ bibtex section1.

Explanation:

  • By putting \bibliographystyle{ieeetr} in the preamble, it is automatically propagated to all subfiles.
  • The \AtEndDoc... is likewise propagated to each subfile when they are compiled as a standalone document, but will be run only once for each compile.
Willie Wong
  • 24,733
  • 8
  • 74
  • 106
  • This answer assumes that the bibliography is the final thing to be printed. If that's not the case, the other answer is more flexible. – Willie Wong Jul 17 '23 at 16:43
  • Your approach works fine, if all files are in the same directory. In this case one has to run bibtex also on the subfiles, since this is how the bbl files for the sufiles are generated. However, if the files are distributed over subdirectories, like in the OP's setting, your approach does not seem to work. How do you obtain the bbl files for the subfiles? When latexing, say, section1.tex, TeX tries to load section1.bbl, not main.bbl. – gernot Jul 18 '23 at 08:44
  • @gernot : oops, I had some leftover files that influenced the process. But I've also found a correct work-flow that would work. Let me edit to include the correct list of steps. – Willie Wong Jul 18 '23 at 13:59
4
  • In the main file, add the line

    \usepackage{xstring}
    
  • In the subfiles, replace \ifSubfilesClassLoaded by \IfStrEq*{\jobname}{filename}, where filename is the name of the subfile (without .tex).

As an example, sections/section1.tex becomes

\documentclass[../main.tex]{subfiles}
\begin{document}
    I like to cite \cite{A}
\subfile{../materials/a_complex_table.tex}

\IfStrEq*{\jobname}{section1}{
    \bibliographystyle{ieeetr}% &lt;&lt;&lt; will apply to all subfiles if moved to the preamble of the main file, no need to repeat it
    \bibliography{../myBib}
}{}

\end{document}

gernot
  • 49,614
  • 2
    As a side comment: I would also just load \bibliographystyle{ieeetr} in the preamble of the main.tex (before \begin{document}) so it is automatically propagated to all the subfiles. – Willie Wong Jul 17 '23 at 16:32
  • 1
    @WillieWong Sure, if the bibliography style is the same everywhere, it is better to put the command into the preamble of the main file. – gernot Jul 17 '23 at 16:34