使用相对路径在子文件中嵌套子文件

使用相对路径在子文件中嵌套子文件

有没有办法使用子文件,以便可以编译中间章节,让我通过示例解释一下。

main.tex (root folder)
--Chapter 1 (Chapter 1 folder)
----Section 1.1 (Chapter 1 folder)

Capter 1 is subfile in main.tex
Section 1.1 is subfile in Chapter 1 docment

我可以做的是,我可以构建和查看每个部分以及 main.tex,但我无法单独构建 Chapter。代码:

%main.tex
\usepackage{subfiles}
\begin{document}
\subfile{chap1/chapter1}
...
\end{document}

%chapter1.tex
\documentclass[../main.tex]{subfiles}
\begin{document}
\chapter{Introduction}
\subfile{chap1/purpose}
...
\end{document}

%purpose.tex
\documentclass[../main.tex]{subfiles}
\begin{document}
\section{Purpose}
\end{document}

如果我将章节中的子文件更改为:我相信我遇到的麻烦源于相对路径:

\subfile(../chap1/purpose)

我可以创建章节并查看它。但是它无法从 main.tex 文件运行。有什么方法可以正确执行吗?

编辑:如果有方法或命令可以获取绝对路径,我就能轻松解决它,有这样的事情吗?

答案1

我遇到了和你同样的问题。

值得注意的是,\providecommand只定义\main一次,即第一次运行。

因此,当在子文件中提供时\main,它也会提供master.tex,并且master.tex不会重新定义它。

这也是为什么它需要在子文件中的文档类之前完成的原因,因为这是master.tex执行代码的地方。

如果您使用\def\main{..},那么每次包含子文件时它都会被覆盖。

解决办法如下:

%main.tex
\usepackage{subfiles}
\providecommand{\main}{.} % relative path to master.tex
\begin{document}
\subfile{chap1/chapter1}
...
\end{document}

%chapter1.tex
\providecommand{\main}{..} %Important: It needs to be defined before the documentclass
\documentclass[\main/main.tex]{subfiles}
\begin{document}
\chapter{Introduction}
\subfile{\main/chap1/purpose} %Insert relative before file
...
\end{document}

%purpose.tex
\providecommand{\main}{..}
\documentclass[\main/main.tex]{subfiles}
\begin{document}
\section{Purpose}
\end{document}

我希望有人会发现它很有用。

编辑:

也可以通过提供 \input@path 来解决这个问题。这使得解决方案可以在不在所有路径中插入 \main 的情况下工作

\makeatletter
\providecommand*{\input@path}{{..}}
\g@addto@macro\input@path{{..}}% append % if you cant override it
\makeatother

相关内容