使用子文件時的參考書目

使用子文件時的參考書目

我正在寫我的博士學位。使用 Biblatex 在 XeLaTeX 中寫論文,有時我需要編譯各個章節,有時我需要編譯整個文檔,所以我使用子文件,效果非常好。然而,在每一章中我都有一個\printbibliography命令,我想在編譯最終文檔時關閉該命令,以便僅\printbibliography在主文件中運行該命令。

文件結構類似:

Main.tex:

\usepackage{subfiles, biblatex}
\begin{document}

\subfile{Chapter1.tex}

\printbibliography
\end{document}

Chapter1.tex:

\documentclass[Main.tex]{subfiles}
\begin{document}
\cite{Someguy1981}
\printbibliography
\end{document}

答案1

subfiles.sty命令\subfile定義為:

\newcommand\subfile[1]{\begingroup\skip@preamble\input{#1}\endgroup}

因此,可以直接掛鉤一些程式碼以在本地重新定義\printbibliography而不執行任何操作。例如,將以下內容新增至主檔案的前導碼:

\makeatletter

\newrobustcmd*{\nobibliography}{%
  \@ifnextchar[%]
    {\blx@nobibliography}
    {\blx@nobibliography[]}}

\def\blx@nobibliography[#1]{}

\appto{\skip@preamble}{\let\printbibliography\nobibliography}

\makeatother

請注意,這biblatex使得書目資料可以透過引文命令獲得。因此,除非您想查看主文件之外的參考書目條目,否則子文件不需要呼叫\printbibliography.

答案2

我也使用了奧黛麗的方法(https://tex.stackexchange.com/a/107111/85983\printbibliography)有一段時間,但我對必須在每個子文件的末尾寫入感到惱火。現在我在主文件的序言中使用以下 Latex Hooks:

%%% Default start and end for each subfile
\AtBeginDocument{%
}

\AtEndDocument{%
    \printbibliography
    \listoffigures
    \listoftables
    \newpage
    \printacronyms[include-classes=abbrev,name=Acronyms]
}

答案3

從子檔2.0版本開始,也可以使用條件宏\ifSubfilesClassLoaded{}{}來執行類似 Audrey 的方法的操作(https://tex.stackexchange.com/a/107111/85983)。

根據我的設置,MWE 如下:

主文件

\documentclass{book}
\usepackage{biblatex}
\usepackage{subfiles} % must be last usepackage

\providecommand{\topdir}{.}
\addglobalbib{\topdir/references.bib} % topdir is needed here so that
                                      % we can resolve the path in the subfile
                                      % correctly. There we re-define the
                                      % topdir macro to the location of the
                                      % bib file.

\begin{document}
\subfile{dir/sub}
\printbibliography[heading=bibintoc]{}
\end{document}

亞特克斯

\providecommand{\topdir}{..} % reset all paths to location of main.tex
\documentclass[../main.tex]{subfiles}

\begin{document}
% some content with citations

\ifSubfilesClassLoaded{%
    \printbibliography{}
}{} % we have no 'else' action
\end{document}

答案4

另一種方法是透過作業名稱來區分子檔案和主檔案。假設你的主檔案被調用Thesis.tex,那麼作業名稱是正常的Thesis,你可以測試

\IfEq{\jobname}{\detokenize{Thesis}}{}{%
    \AtEndDocument{%
        \printbibliography%
    }
}

這將在子文件的末尾插入參考書目,並且在主文件中不執行任何操作。

(如果可能的話,之前使用這個\usepackage{biblatex},否則可能會導致警告,請參閱\AtEndDocument{\printbibliography} 的未定義引用警告

相關內容