> For the complete documentation index, see [llms.txt](https://overleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://overleaf-pro.ayaka.space/latex/ja/wen-shu-gou-zao/08-multi-file-latex-projects.md).

# 複数ファイルの LaTeX プロジェクト

## はじめに

では [大きなLaTeX文書では](/latex/ja/wen-shu-gou-zao/07-management-in-a-large-project.md) 通常、いくつかの `.tex` ファイルを使います。各章や節ごとに1つずつ用意し、それらをまとめて1つの出力を生成します。こうすると全体を整理しやすくなり、文書のデバッグもしやすくなりますが、文書が大きくなるにつれてコンパイルに時間がかかります。通常は特定のファイルだけを毎回作業したいので、これは煩わしく感じることがあります。

これを解決する自然な方法は、各ファイルを個別にコンパイルすることです。マルチファイルプロジェクトで単一ファイルのコンパイルを可能にする主なパッケージは2つあります。どちらを選ぶかは、必要なものによって決まります。

* 〜を使うと [その **subfiles** パッケージ](#the-subfiles-package) 各サブファイルを独立してコンパイルでき、各サブファイルはメインファイル内のプリアンブルを自動的に使用します。
* 〜を使うと [その **standalone** パッケージ](#the-standalone-package) 各サブファイルは独立したファイルとして機能し、サブファイルは後でメイン文書にまとめることができ、その際それぞれのプリアンブルを取り込みます。同じファイルを複数の文書で再利用したい場合に特に便利で、TikZの図はそのよい例です。

## Overleafの.texファイルにおける\documentclassについての注意

次の動画クリップで示すように、プロジェクトのメイン `.tex` ファイルを設定していても、別のファイルを選んでコンパイルできます。ただし、そのファイルに `\documentclass` 宣言が含まれていることが条件です。ただし、グロッサリーや、 `.bib` ファイルへのパスの参照など、プロジェクト内のすべての要素が正しくコンパイルされるようにするため、プロジェクトのルートディレクトリ内にあるファイルだけをコンパイルすることを強く推奨します。

{% embed url="<https://videos.ctfassets.net/nrgyaltdicpt/U2nGfRx9NwNGX0vWXNebP/1ca4b9a4b2f36d5ca9c0dbaa87f44bfa/maincompile.mp4>" %}

## subfilesパッケージ

このパッケージはほとんどの状況に適しており、使い方もとても簡単です。

このセクションの例では、次の階層的なファイル構成を使います:

![CompileSubfilesEx1OverleafV2.png](/files/c7ec9879f3fbc3bfd9e6da2cdae63a361b303dcf)

### メインファイル

メインファイルでは2つの特別なコマンドが必要です。

```latex
\documentclass{article}
\usepackage{graphicx}
\graphicspath{{images/}}

\usepackage{blindtext}

\usepackage{subfiles} % プリアンブルの最後に読み込むのが最適です

\title{Subfilesパッケージの例}
\author{Overleaf}
\date{ }

\begin{document}

\maketitle

\section{導入}

\subfile{sections/introduction}

\section{第2節}

\subfile{sections/section2}

\end{document}
```

![CompileSubfilesEx1aOverleafV2.png](/files/065ef91ee68a2eb9d5a28591c6721efaee372f17)

ファイルごとのコンパイルには、次の行が必要です

```latex
\usepackage{subfiles}
```

をプリアンブルに記述すると、これで `subfiles` パッケージが有効になります。次に、各外部サブファイルはコマンド `\subfile{}`で読み込む必要があります。例では、ファイル `introduction.tex` や `section2.tex` は `sections` フォルダからメインファイルに取り込まれます。ファイル拡張子は必須ではないことに注意してください。

[1つ開く `subfiles` パッケージの例を Overleaf で開く](https://www.overleaf.com/project/new/template/19626?id=66392908\&templateName=Subfiles+package+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

### subfiles

メインファイルを設定したら、各サブファイルには特別な構造が必要です。

```latex
\documentclass[../main.tex]{subfiles}
\graphicspath{{\subfix{../images/}}}
\begin{document}
\textbf{こんにちは、世界！}
\begin{figure}[bh]
\centering
\includegraphics[width=3cm]{overleaf-logo}

\label{fig:img1}
\caption{Overleafのロゴ}
\end{figure}

こんにちは、意味のないテキストがここにあります...

\end{document}
```

![CompileSubfilesEx2OverleafV2.png](/files/992c9311909be8107a65dee093cf09ee1628ac20)

これでこのファイルは単独ファイルとしてコンパイルでき、文書クラスと残りのプリアンブルはメイン文書で定義されたものと同じになります。

最初のコマンドは

```latex
\documentclass[../main.tex]{subfiles}
```

角括弧内のパラメータ `../main.tex`で、メイン文書への相対パスです。この場合、ファイル `introduction.tex` はフォルダ `sections`の中にあるため、ファイル `main.tex` は現在のフォルダの1つ上の階層にあります（これが `../` の意味です）。

また、 `\subfix` を使って相対的なフォルダパスを指定する必要もあります。 `\graphicspath` を `introduction.tex`:

```latex
\graphicspath{{\subfix{../images/}}}
```

その後、実際の内容は `\begin{document}` や `\end{document}`の中に記述します。この環境の外側はすべて無視されるか、より正確にはプリアンブルの一部として扱われます。ファイルの先頭と末尾に空行を残さないようにしてください。

[1つ開く `subfiles` パッケージの例を Overleaf で開く](https://www.overleaf.com/project/new/template/19626?id=66392908\&templateName=Subfiles+package+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

## standaloneパッケージ

このパッケージ **standalone** は **subfiles** と同じ機能を提供し、より柔軟ですが、構文はより複雑でミスが起こりやすいです。主な違いは、各サブファイルが独自のプリアンブルを持つことです。

このセクションの例では、次の階層的なファイル構成を使います:

![CompileSubfilesEx3OverleafV2.png](/files/08031fa6ade35262bafea9ca519d252f86162437)

### メインファイル

メインファイルは、他の複数ファイルのプロジェクトと非常によく似ています。

```latex
\documentclass{article}
\usepackage[subpreambles=true]{standalone}
\usepackage{import}

\title{standaloneパッケージの例}
\author{Overleaf}
\date{2021年5月}

\begin{document}

\maketitle

\section{第1節}
\import{sections/}{introduction}

\section{第2節}
\import{sections/}{section2}

\end{document}
```

![CompileSubfilesEx3Overleaf.png](/files/010e1f44398f9aca34df1b59bde316286e06947c)

この行は

```latex
\usepackage[subpreambles=true]{standalone}
```

パッケージを有効にします。 **standalone** このパッケージは文書の早い段階で読み込む必要があります。角括弧内のパラメータは、LaTeXに各サブファイルからプリアンブルを取り込むよう指示します（パッケージは一度だけ読み込まれます）。省略した場合は、サブファイルが動作するために必要なコマンドがメイン文書のプリアンブルにすべて含まれていることを確認してください。異なるプリアンブル間の非互換性の可能性があるため、注意が必要です。

メイン文書の本文では、各サブファイルを `\import{}{}`で読み込みます。標準の `\input{}` コマンドも使用できますが、この例のものは [大規模プロジェクトの管理](/latex/ja/wen-shu-gou-zao/07-management-in-a-large-project.md) に適しているため推奨されます。ネストされたファイルでのエラーを防げるからです。

[Overleafで `standalone` パッケージの例を開く](https://www.overleaf.com/project/new/template/19636?id=66440758\&templateName=Standalone+package+example\&latexEngine=pdflatex\&texImage=\&mainFile=)

### subfiles

各サブファイルには独自のプリアンブルが必要で、単独文書として動作するために必要なすべてのパッケージを読み込まなければなりません。

```latex
\documentclass[class=article, crop=false]{standalone}
\usepackage[subpreambles=true]{standalone}
\usepackage{import}
\usepackage{blindtext}

\begin{document}
この行の下にTikZ図が描画されます

\begin{figure}[ht]
\centering
\subimport{../}{diagram.tex}
\label{fig:tikzexample}
\caption{きれいでシンプルな図}
\end{figure}

\blindtext
\end{document}
```

![CompileSubfilesEx4OverleafV2.png](/files/9787699d7936c978756d00c7295f8c01193ea65f)

サブファイルの最初の行は

```latex
\documentclass[class=article, crop=false]{standalone}
```

これは、 **standalone** パッケージで使用するためのファイルであることを宣言しています。括弧内には2つのオプションパラメータがあります。

* `class=article`。 `article` を基底クラスとして設定します。ほかのクラスも使用できます: book、report など（beamerを除く）。
* `crop=false`。このオプションを省略すると、出力は最小サイズに切り詰められます。

次のコマンドは必須ではありません

```latex
\usepackage[subpreambles=true]{standalone}
```

が、この例ではネストされた **standalone** ファイルがあるため使用する必要があります。 [TikZの図](/latex/ja/to/05-tikz-package.md) は [`\subimport{../}{diagram.tex}`](/latex/ja/wen-shu-gou-zao/07-management-in-a-large-project.md#importing-files)で挿入されます。下に "diagram.tex" ファイルの内容を示します:

```latex
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}[
roundnode/.style={circle, draw=green!60, fill=green!5, very thick, minimum size=7mm},
squarednode/.style={rectangle, draw=red!60, fill=red!5, very thick, minimum size=5mm},
]
%ノード
\node[squarednode]      (maintopic)                              {2};
\node[roundnode]        (uppercircle)       [above=of maintopic] {1};
\node[squarednode]      (rightsquare)       [right=of maintopic] {3};
\node[roundnode]        (lowercircle)       [below=of maintopic] {4};

%線
\draw[->] (uppercircle.south) -- (maintopic.north);
\draw[->] (maintopic.east) -- (rightsquare.west);
\draw[->] (rightsquare.south) .. controls +(down:7mm) and +(right:7mm) .. (lowercircle.east);

\end{tikzpicture}
\end{document}
```

[Overleafでこのstandalone TikZの例を開く](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Standalone+TikZ+example\&snip=%5Cdocumentclass%7Bstandalone%7D%0A%5Cusepackage%7Btikz%7D%0A%5Cusetikzlibrary%7Bpositioning%7D%0A%5Cbegin%7Bdocument%7D%0A%0A%5Cbegin%7Btikzpicture%7D%5B%0Aroundnode%2F.style%3D%7Bcircle%2C+draw%3Dgreen%2160%2C+fill%3Dgreen%215%2C+very+thick%2C+minimum+size%3D7mm%7D%2C%0Asquarednode%2F.style%3D%7Brectangle%2C+draw%3Dred%2160%2C+fill%3Dred%215%2C+very+thick%2C+minimum+size%3D5mm%7D%2C%0A%5D%0A%25Nodes%0A%5Cnode%5Bsquarednode%5D++++++%28maintopic%29++++++++++++++++++++++++++++++%7B2%7D%3B%0A%5Cnode%5Broundnode%5D++++++++%28uppercircle%29+++++++%5Babove%3Dof+maintopic%5D+%7B1%7D%3B%0A%5Cnode%5Bsquarednode%5D++++++%28rightsquare%29+++++++%5Bright%3Dof+maintopic%5D+%7B3%7D%3B%0A%5Cnode%5Broundnode%5D++++++++%28lowercircle%29+++++++%5Bbelow%3Dof+maintopic%5D+%7B4%7D%3B%0A%0A%25Lines%0A%5Cdraw%5B-%3E%5D+%28uppercircle.south%29+--+%28maintopic.north%29%3B%0A%5Cdraw%5B-%3E%5D+%28maintopic.east%29+--+%28rightsquare.west%29%3B%0A%5Cdraw%5B-%3E%5D+%28rightsquare.south%29+..+controls+%2B%28down%3A7mm%29+and+%2B%28right%3A7mm%29+..+%28lowercircle.east%29%3B%0A%0A%5Cend%7Btikzpicture%7D%0A%5Cend%7Bdocument%7D)

![CompileSubfilesEx5.png](/files/6e3449fa38df68d08f9334b3b3ca030cc1d7da8d)

これが **standalone**の主な機能です。このファイルをほかの文書に取り込んで、コードを再利用できます。たとえば、この図は後でプレゼンテーションでも変更なしに使えます。

[Overleafで `standalone` パッケージの例を開く](https://www.overleaf.com/project/new/template/19636?id=66440758\&templateName=Standalone+package+example\&latexEngine=pdflatex\&texImage=\&mainFile=)

## さらに読む

詳細については

* [大規模プロジェクトでの管理](/latex/ja/wen-shu-gou-zao/07-management-in-a-large-project.md)
* [節や数式の相互参照](/latex/ja/wen-shu-gou-zao/03-cross-referencing-sections-equations-and-floats.md)
* [索引](/latex/ja/wen-shu-gou-zao/04-indices.md)
* [用語集](/latex/ja/wen-shu-gou-zao/05-glossaries.md)
* [ハイパーリンク](/latex/ja/wen-shu-gou-zao/09-hyperlinks.md)
* [ページ番号](/latex/ja/shu-shi-she-ding/03-page-numbering.md)
* [片面印刷と両面印刷の文書](/latex/ja/shu-shi-she-ding/08-single-sided-and-double-sided-documents.md)
* [複数カラム](/latex/ja/shu-shi-she-ding/09-multiple-columns.md)
* [段落の整形](/latex/ja/shu-shi-she-ding/04-articles-how-to-change-paragraph-spacing-in-latex.md)
* [ページサイズと余白](/latex/ja/shu-shi-she-ding/07-page-size-and-margins.md)
* [カウンタ](/latex/ja/shu-shi-she-ding/10-counters.md)
* [欄外メモ](/latex/ja/shu-shi-she-ding/15-margin-notes.md)
* [太字、斜体、下線](/latex/ja/latexno/03-bold-italics-and-underlining.md)
* [フォントサイズ、ファミリー、スタイル](/latex/ja/fonto/01-font-sizes-families-and-styles.md)
* [フォントの書体](/latex/ja/fonto/02-font-typefaces.md)
* [XeLaTeXでの現代的なフォントのサポート](/latex/ja/fonto/03-xelatex.md)
* [国際言語サポート](/latex/ja/yan-yu/03-international-language-support.md)
* [フォントサイズ、ファミリー、スタイル](/latex/ja/fonto/01-font-sizes-families-and-styles.md)
* [大規模プロジェクトのコンパイル](/latex/ja/narejjibsu/038-fixing-and-preventing-compile-timeouts.md#compiling-big-projects)
* [画像の挿入](/latex/ja/sononotopikku/27-inserting-images.md)
* [TikZパッケージ](/latex/ja/to/05-tikz-package.md)
* [独自のパッケージを書く](/latex/ja/kurasufairu/03-writing-your-own-package.md)
* [独自のクラスを書く](/latex/ja/kurasufairu/04-writing-your-own-class.md)
* [この **subfiles** パッケージのドキュメント](http://repositorios.cpai.unb.br/ctan/macros/latex/contrib/subfiles/subfiles.pdf)
* [この **standalone** パッケージのドキュメント](http://repositorios.cpai.unb.br/ctan/macros/latex/contrib/standalone/standalone.pdf)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://overleaf-pro.ayaka.space/latex/ja/wen-shu-gou-zao/08-multi-file-latex-projects.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
