我使用子文件将文档拆分为多个文件,每个章节一个。它工作得很好,但参考书目除外:如果我编译主文档,可以找到这些项目,但如果我单独编译一章,则找不到任何参考书目项目。文档子文件表明应该“正常工作”:
一些命令已经即时应用了修复。目前这些是标准 LaTeX 命令 \bibliography 和 \graphicspath。
知道什么地方出了问题吗?
主要文件:
章节:
梅威瑟:
main.tex
:
\RequirePackage{fix-cm} % Correct scaling of CM font
\documentclass[a4paper,12pt]{memoir}
\usepackage{subfiles}
% Print bibliography at end of each subfile when compiling separately,
% but also in main document when compiling together. To treat subfile and
% main file differently, see https://tex.stackexchange.com/a/358429/116348
\AtEndDocument{%
\bibliographystyle{alpha}
\bibliography{biblio}
}
\begin{document}
\chapter{Thanks}
This is my main document.
\subfile{Chapters/01_chapter_intro.tex}
\end{document}
biblio.bib
:
@article{Mer89_WhatWrongThis,
title = {What's {{Wrong}} with This {{Pillow}}?},
author = {Mermin, N. David},
year = {1989},
month = apr,
volume = {42},
pages = {9--11},
publisher = {{American Institute of Physics}},
issn = {0031-9228},
doi = {10.1063/1.2810963},
file = {/home/leo/Zotero/storage/WV73TT5N/1.html},
journal = {Physics Today},
number = {4}
}
Chapters/01_chapter_intro.tex
:
\documentclass[../main]{subfiles}
\begin{document}
\chapter{Hello}
Hello \cite{Mer89_WhatWrongThis}.
\end{document}
答案1
使用
\AtEndDocument{%
\bibliographystyle{alpha}
\bibliography{biblio}
}
将使子文件缺少该biblio.bib
文件,因为对于它们来说,正确的路径是../biblio
。使用指向单个文件的bibtex
命令\bibliography{biblio}
,您不能添加多个搜索路径。
(使用相反的方法\bibliography{../biblio}
可以在章节上成功,但在编译时会失败main.tex
)
避免这种情况的一种方法是检测您是否在文档的序言中或正文中。
这是通过命令完成的\insubfile
(来自\renewcommand 仅在具有子文件包的从属文件中)
或者查看最后的我的更新!
在编译时将执行子文件\bibliography{../References/biblio}
;相反,这些命令将被忽略编译main.tex
,但\bibliography{./References/biblio}
将在文档结束时执行。
我选择了一个稍微复杂一点的目录结构来测试解决方案。我喜欢将属于一个项目的所有文件放在同一个屋檐下。
对各章分别进行编译后,得到以下单独的结果:
01_章节简介.tex
02_章节一.tex
为了main.tex你得到
文件 MyProject/main.tex
%% MyProject/main.tex
\RequirePackage{fix-cm} % Correct scaling of CM font
\documentclass[a4paper,12pt]{memoir}
\usepackage{subfiles}
%% https://tex.stackexchange.com/a/515672/161015
%% https://tex.stackexchange.com/a/16298/161015
\makeatletter
\newcommand{\insubfile}[1]{\ifx\@onlypreamble\@notprerr\else#1\fi}
\makeatother
\bibliographystyle{alpha}
\begin{document}
\chapter{Thanks}
This is my main document. Starts with \cite{Mer89_WhatWrongThis}.
\subfile{./Chapters/01_chapter_intro}
\subfile{./Chapters/02_chapter_one}
\bibliography{./References/biblio}
\end{document}
文件MyProject/Chapters/01_chapter_intro.tex
%% MyProject/Chapters/01_chapter_intro.tex
\documentclass[../main]{subfiles}
\insubfile{\AtEndDocument{\bibliography{../References/biblio}}}
\begin{document}
\chapter{Hello}
And then \cite{einstein}.
\end{document}
文件MyProject/Chapters/02_chapter_one.tex
%% MyProject/Chapters/02_chapter_one.tex
\documentclass[../main]{subfiles}
\insubfile{\AtEndDocument{\bibliography{../References/biblio}}}
\begin{document}
\chapter{One}
Only \cite{dirac}.
\end{document}
文件MyProject/References/biblio.bib
%%% MyProject/References/biblio.bib
@article{Mer89_WhatWrongThis,
title = {What's {{Wrong}} with This {{Pillow}}?},
author = {Mermin, N. David},
year = {1989},
month = apr,
volume = {42},
pages = {9--11},
publisher = {{American Institute of Physics}},
issn = {0031-9228},
doi = {10.1063/1.2810963},
file = {/home/leo/Zotero/storage/WV73TT5N/1.html},
journal = {Physics Today},
number = {4}
}
@article{einstein,
author={Albert Einstein},
title={Zur Elektrodynamik bewegter Korper},
journal={Annalen der Physik},
volume={322},
number={10},
pages={891--921},
year={1905},
DOI ={http://dx.doi.org/10.1002/andp.19053221004}
}
@book{dirac,
title={The Principles of Quantum Mechanics},
author={Paul Adrien Maurice Dirac},
isbn={9780198520115},
series={International series of monographs on physics},
year={1981},
publisher={Clarendon Press},
keywords = {physics}
}
更新(出色地 ...)
阅读软件包subfile
文档后,我发现 2020 年 10 月向该类添加了一个新命令。
3.3 条件执行命令
该命令
\ifSubfilesClassLoaded
可用于有条件地执行命令,取决于主文件是否排版或子文件。
\ifSubfilesClassLoaded{% then branch
. . . commands executed when the subfile is typeset . . .
}{% else branch.
. . commands executed when the main file is typeset . . .
}
举个例子,这可用于将参考书目添加到主文档或子文档(以排版方式为准):