在子文件中匯入文件的正確方法是什麼?

在子文件中匯入文件的正確方法是什麼?

subfiles 文件解釋說,當主文件和子文件位於不同的目錄中並且子文件包含相對於使用包的自己目錄的路徑時,它會處理路徑問題\import它出現這意味著在新增傳遞到 的文件內容時subfiles使用。例如,大致相當於修改then 呼叫的前導碼。不清楚的是如何正確\import\subfile\subfile{ch/ch1.tex}ch1.tex\import{ch/ch1.tex}巢進口,即在導入的子文件中導入。

下面我提供了一個範例專案/目錄結構和兩個範例文件,假設 main.tex 應該導入 ch1.tex,ch1.tex 應該導入 Fig.png 和 text.txt。

 +-- main.tex
 +-- chapters
     |
     +-- ch1.tex
     +-- content
         |
         +-- text1.txt
         +-- fig1.png
%% main.tex
\documentclass{book}
\usepackage{graphicx}
\usepackage{subfiles}

\begin{document}
    \subfile{chapters/ch1.tex} % \imports ch1.tex
\end{document}
%% ch1.tex
\documentclass[../main.tex]{subfiles}
\begin{document}
    \import{content}{text.txt}        % adds some text - note use of \import
    \includegraphics{content/fig.png} % adds a figure
\end{document}

當我編譯上面的檔案時,它們會產生所需的輸出。但是,正如在 main.tex 中的呼叫\import中使用的那樣\subfile{chapters/ch1.tex},但隨後是用過的之內通話中的 ch1.tex \import{content}{text.txt}。這import 文件表示\subimport應該用於後者。作為 \subimport requires a path relative to the currently imported file, the call should be\subimport{content/}{text1.txt}`。

在子文件中導入是否應該始終使用subimport?如果是這樣,我很困惑為什麼交換\import\subimport產生相同的結果,而它們應該指向不同的目錄,如import文件中的範例所示。

答案1

您可以\input直接使用。

│  main.tex
└─chapters
    │  ch1.tex
    └─content
            fig1.png
            text1.txt

main.tex

\documentclass{book}
\usepackage{graphicx}
\graphicspath{{chapters/content/}}
\makeatletter
\newcommand{\input@path}{{chapters/}{chapters/content/}}
\makeatother
\begin{document}
aaa
\input{ch1.tex}
bbb
\end{document}

ch1.tex

ccc
\input{text1.txt}
\includegraphics[width=3cm]{fig1.png}
ddd

text1.txt

eee

在此輸入影像描述

相關內容