1489

There are two different commands to incorporate another file into the source of some document, \input and \include. When should I use one or the other? What are the differences between them? Are there more things like them to be aware of?

4
  • 14
    I didn't see mentioned anywhere that \include doesn't seem to recognize \label's in the included file. I changed to \input and the cross-references worked fine.
    – Jeff
    Apr 4, 2014 at 3:26
  • 39
    There is actually a third variant in LaTeX: \@input is used just like \input except that it does not throw an error if the file does not exist. This can be very useful sometimes.
    – user30471
    Apr 28, 2016 at 14:01
  • 4
    @Jeff, I've got lots of \included files (chapters of my notes), and use cross references all the time without a hitch.
    – vonbrand
    Sep 8, 2020 at 2:38
  • @user30471 Nice one May 18, 2022 at 16:27

7 Answers 7

1371
+250

\input{filename} imports the commands from filename.tex into the target file; it's equivalent to typing all the commands from filename.tex right into the current file where the \input line is.

\include{filename} essentially does a \clearpage before and after \input{filename}, together with some magic to switch to another .aux file, and omits the inclusion at all if you have an \includeonly without the filename in the argument. This is primarily useful when you have a big project on a slow computer; changing one of the include targets won't force you to regenerate the outputs of all the rest.

\include{filename} gets you the speed bonus, but it also can't be nested, can't appear in the preamble, and forces page breaks around the included text.

12
  • 181
    In terms of when to use them, \include is commonly used to put each chapter of a book or thesis into its own file. I keep a set of shortcut commands that I use in almost all of my documents in a .tex file in my path, and then \input it in the preamble of any document that needs to use them. Jul 27, 2010 at 6:44
  • 21
    For completeness, note that \includeonly is the way that you select chapters to include, and so get the speed payoff. See also another question, which mentions this solution. Aug 28, 2010 at 13:31
  • 37
    From what I have observed right now \input is also a whole lot faster than \include. Splitting my thesis in 10 parts with \include just quadrupled my compile time, with \input I don't have this issue...
    – fgysin
    Feb 21, 2013 at 13:29
  • 31
    The statement "\input{filename} ... is equivalent to typing all the commands from filename right into the current file" might be somewhat misleading, because this sounds as if it would mess up line numbers. It should be mentioned that this is not the case: If a (maybe even nested) \input contains a syntax error, LaTeX will correctly point to the line of the file you included, not the file which embeds the \input.
    – bluenote10
    Jan 16, 2014 at 14:47
  • 15
    @MichaelUnderwood You should use a custom package for that. Keeps it cleaner.
    – Gustaphe
    Oct 20, 2014 at 8:48
451

Short answer:

\input is a more lower level macro which simply inputs the content of the given file like it was copy&pasted there manually. \include handles the file content as a logical unit of its own (like e.g. a chapter) and enables you to only include specific files using \includeonly{filename,filename2,...} to save times.


Long answer:

The \input{<filename>} macro makes LaTeX to process the content of the given file basically the same way as if it would be written at the same place as \input. The LaTeX version of \input only does some sanity checks and then uses the TeX \input primitive which got renamed to \@@input by LaTeX.

Mentionable properties of \input are:

  • You can use \input basically everywhere with any content.
    It is usable in the preamble, inside packages and in the document.
  • You can nest \input macros.
    You can use \input inside a file which is read using \input.
  • The only thing \input does is to input the file.
    You don't have to worry about any side effects, but don't get any extra features.

The \include{<filename>} macro is bigger and is supposed to be used with bigger amounts of content, like chapters, which people might like to compile on their own during the editing process.

\include does basically the following thing:

  • It uses \clearpage before and after the content of the file. This ensure that its content starts on a new page of its own and is not placed together with earlier or later text.
  • It opens a new .aux file for the given file.
    There will be a filename.aux file which contains all counter values, like page and chapter numbers etc., at the begin of the filename. This way the file can be compiled alone but still has the correct page and chapter etc. numbers. Such part aux files are read by the main aux file.
  • It then uses \input internally to read the file's content.

Mentionable properties of \include are:

  • It can't be used anywhere except in the document and only where a page break is allowed.
    Because of the \clearpage and the own .aux file \include doesn't work in the preamble, inside packages. Using it in restricted modes or math mode won't work properly, while \input is fine there.
  • You can't nest \include files.
    You can't use \include inside a file which is read by \include. This is by intention and is because to avoid issues with the .aux files. Otherwise three .aux files (main, parent \include, child \include) would be open at the same time which was deemed to complicated I guess.
    You can use \input inside an \include file and also \input an \include file.
  • Biggest benefit: You can use \includeonly{filename1,filename2,...} in the preamble to only include specific \include files.
    Because the state of the document (i.e. above mentioned counter values) was stored in an own .aux file all page and sectioning numbers will still be correct. This is very useful in the writing process of a large document because it allows you to only compile the chapter you currently write on while skipping the others. Also, if used persistently it can be used to create PDFs of sub-parts of your document, like only the front matter or everything but/only the appendix, etc.
    There is also the excludeonly package which provides an \excludeonly to exclude only certain files instead of including all other files.
9
  • 13
    I've read several times that \include won't work in the preamble; @egreg in tex.stackexchange.com/questions/91167/why-use-sty-files even states never use \include for packages/definitions. However, due to limited knowledge when stating with LaTeX, I did exactly that, i.e. used \include in the preamble to load packages. And it worked flawlessly for numerous documents using TeTeX/TexLive2011, so I kept it till today (never change a running setup ;) The produced dvi file is binary identical when using \include or \input. But I keep in mind to try input if strange things happen.
    – mpy
    Mar 2, 2013 at 14:50
  • 15
    @mpy: Please don't tell other people to use \include in the preamble especially not to load packages! For these use \usepackage. \include causes a page break and does several things in the background, e.g. opens an .aux file for every file, etc. and should only be used for chapters or similar things. Mar 2, 2013 at 15:13
  • 9
    That was not my intention, I am only confused when I read \include won't work in preamble... Please feel free to delete my comment if you consider it as dangerous.
    – mpy
    Mar 2, 2013 at 15:18
  • 3
    What happens with the new imported equations and tables? Are there automatically renumbered?
    – skan
    Nov 19, 2016 at 16:34
  • 1
    I would guess, in order to be able to compile the \included content on their own during the editing phase, each \included file must have its own preamble and \begin{document} and \end{document}, right? Is LaTeX smart enough not to load the packages that are in the preamble of several included files multiple times?
    – Andyc
    Nov 29, 2020 at 14:12
112

\input effectively replaces the command with the contents of the input file. \input's can be nested. So, you can write:

\documentclass{article}

\begin{document}

AAA

\input{b}

AAA

\end{document}

where b.tex is:

BBB

\input{c}

BBB

and c.tex is:

CCC

to get output like:

AAA
BBB
CCC
BBB
AAA

include triggers a newpage both before and after the included material, rather as though you'd used an \input flanked by \clearpage commands. include also supports the \includeonly mechanism. So, the file:

\documentclass{article}

\includeonly{c}

\begin{document}

AAA

\include{b}

\include{c}

AAA 

\end{document}

with b.tex and c.tex as before, will produce output with AAA on page one, CCC on page two, and AAA on page 3.

The \include and \includeonly pair is very useful for working on long documents: you can \includeonly the file which you are editing, and compilation is much faster. If you do two runs on the full file before using \includeonly, page numbers and cross-references will remain valid for the quicker \includeonly compilation.

3
  • 12
    +1 for \includeonly. However, IIRC the page numbers and cross-references will only remain valid for files \include'd before the current file. So if you are working on chapter four, and there's a reference to chapter 5 which is currently not looked at, references to chapter 5 (if you use just sequential numbering disregarding the chapters) may be wrong. Jul 27, 2010 at 1:32
  • @Willie Wong is absolutely right. If you are working on Chapter 2 and add 5 pages, cross-references to pages of Chapter 3 will be off. Thanks for keeping things honest.
    – vanden
    Jul 27, 2010 at 1:35
  • 1
    @WillieWong you can make forward references so long as you have previously processed chapter 5. May 20, 2017 at 17:05
85

From the LaTeX Wikibook :

When working on big documents, you might want to split the input file into several parts. LaTeX has three commands to insert a file into another when building the document.

The simplest is the \input command:

\input{filename}

\input inserts the contents of another file, named filename.tex; note that the .tex extension is omitted. For all practical purposes, \input is no more than a simple, automated cut-and-paste of the source code in filename.tex.

The other main inclusion command is \include:

\include{filename}

The \include command is different from \input in that it's the output that is added instead of the commands from the other files. Therefore a new page will be created at every \include command, which makes it appropriate to use it for large entities such as book chapters.

Very large documents (that usually include many files) take a very long time to compile, and most users find it convenient to test their last changes by including only the files they have been working on. One option is to hunt down all \include commands in the inclusion hierarchy and to comment them out:

%\include{filename1}

\include{filename2}

\include{filename3}

%\include{filename4}

In this case, the user wants to include only filename2.tex and filename3.tex. If the inclusion hierarchy is intricate, commenting can become error-prone: page numbering will change, and any cross references won't work. A better alternative is to retain the include calls and use the \includeonly command in the preamble:

\includeonly{filename2,filename3}

This way, only \include commands for the specified files will be executed, and inclusion will be handled in only one place. Note that there must be no spaces between the filenames and the commas.

Also, you cannot do \include in an \included document, so then just use \input.

1
  • The main part of the text quoted from the wikibook on the difference between \input and \include "The \include command is different from \input in that it's the output that is added " is completely wrong unfortunately. Jul 6, 2021 at 15:07
16

Great answers about input and include commands. If you need to nest this .tex archives, have import package that brings the \import.

% at preamble
\usepackage{import}

% where you need
\import{〈full_path〉}{〈file〉}

Imagine you have directories named preamble and text. Inside preamble you have general.tex (where you call most part of packages), fonts.tex (where you call about fonts, encodings and configuration about it) and commands.tex (where you define your custom macros and commands). You have another colors.tex, where you define a lot of colors, and it's called inside your general.tex with import. In text you have your work name by chapter and section. You can do your main.tex like:

\documentclass{memoir}

\usepackage{import}

\import{preamble/}{general}
\import{preamble/}{fonts}
\import{preamble/}{commands}

\begin{document}

\frontmatter
 \import{text/}{titlepage}
 \tableofcontents
...

\mainmatter
%--- Part 1
 \import{text/}{1-1.1-Name1.1}
 \import{text/}{1-1.2-Name1.2}
...
%--- Part 2
 \import{text/}{1-2.1-Name2.1}
 \import{text/}{1-2.2-Name2.2}
.
.
.
\end{document}

The best thing is, inside any of those .tex you call you can call another .tex with \import - like general.tex calls colors.tex inside itself. You need to put / into final of path in that first parameter.

1
  • +1 Nesting is a very desirable property! Jul 18, 2022 at 0:02
1

Miller's Question: \input and \include. When should I use one or the other? What are the differences between them?

LaTeX commands \includeonly and \include are used to design the chapter structure in a book or thesis. Chapters in progress appear in the \includeonly comma-separated list, in the preamble of the main LaTeX file. All other planned chapters are also in the \includeonly list, but commented out with a percent character. LaTeX command \include appears after \begin{document}, one for each planned chapter.

Command \include is designed for a whole chapter, not a section or subsection. By default, it inserts a \clearpage before it starts, then inserts the file, then inserts a second \clearpage.

Command \input inserts the file contents without either \clearpage. It generates a LaTeX error on compile, if the file does not exist. By comparison, so does \include. However, if the \includeonly list excludes the file name in question, then \include emits no error. File names excluded from the list in \includeonly are ignored by \include - such files are not inserted.

A book with 16 planned chapters can be designed quickly with \includeonly and \include. The chapters can be nearly empty files, with only a \chapter command in each file. The work flow proceeds by commenting out the unfinished file names in the \includeonly list. Then the writing task begins, a single chapter at a time.

Bothered by the \clearpage commands? The effect of \clearpage commands hidden in \include or \chapter commands can be visualized by adding this preamble line:

\let\include\input

The effect is to change every \include to \input and ignore the \includeonly list. In a production book, the difference is many pages, due to the missing \clearpage commands from \include.

0

There are two biggest differences between them:

  1. Can't nest include, but input can be. For example, we can't input include{file} in the file B, which had been inserted by include{file B}.
  2. include can't input the file which locates in the parent directory of the tex file you edit. For example, \input{../location1/chapter1_1} works but \include{../location1/chapter1_1} won't work, as .. represents going to the parent directory of the file you editing.

include has more restrictions than input.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .