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?
- 54,637
- 20,208
- 7
- 27
- 28
7 Answers
\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.
- 29,535
- 16,127
- 1
- 23
- 26
-
181In terms of when to use them,
\includeis 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\inputit in the preamble of any document that needs to use them. – Michael Underwood Jul 27 '10 at 06:44 -
21For completeness, note that
\includeonlyis the way that you select chapters to include, and so get the speed payoff. See also another question, which mentions this solution. – Norman Gray Aug 28 '10 at 13:31 -
2@Michael, I add one more: see http://tex.stackexchange.com/questions/7010/input-can-work-with-quoted-string-path-containing-spaces-but-include-cannot-h where \include cannot be used for path with spaces. – Display Name Dec 13 '10 at 07:02
-
37From what I have observed right now
\inputis also a whole lot faster than\include. Splitting my thesis in 10 parts with\includejust quadrupled my compile time, with\inputI don't have this issue... – fgysin Feb 21 '13 at 13:29 -
31The 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)\inputcontains 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 '14 at 14:47 -
15@MichaelUnderwood You should use a custom package for that. Keeps it cleaner. – Gustaphe Oct 20 '14 at 08:48
-
What happens with the new imported equations and tables? Are there automatically renumbered? – skan Nov 19 '16 at 16:34
-
6@LeonMeier of course
\inputis faster than include in that sense (although I'm surprised it is measurable on a large document on a modern machine as the extraauxfile processing is fairly negligible compared to processing the document body in most documents). The reference to speed of\includerefers to the fact that you can just process one chapter of your 100 chapter book. that gives real speedup during the edit/preview cycle. – David Carlisle May 20 '17 at 17:10 -
3I think it is important to mention the package
newcludewhich claims to remove the aforementioned restrictions withinclude. I do not have much experience with this package, but the author's claims are bold. This package is also quite old dating back to 1999. Also it is worth mentioningchilddoc, a modern (2017) package that allows standalone compilation of included files without having to fiddle around\includeonly. – Dr Krishnakumar Gopalakrishnan Jun 14 '18 at 20:08 -
\incudeis finicky about where the component source files are located. For example, including a file../theotherdir/theotherfile.texgenerates an error, but inputing the same path works fine. I want to point out that another advantage ofnewcludeis that it redefines\includeso that this problem is avoided. – Mars May 21 '19 at 17:48 -
1i found that contents inside of
\includes were not able to find the references for\citecalls using\bibliographys placed elsewhere in the document (they came up as(?)s).\inputdid not have this problem. – Richard DiSalvo Jun 25 '19 at 22:26 -
2For a large book, I use
\include{chapters/<chaptername>}in the main.texfile and then use\input{chapters/<chaptername>/<sectionname>}inchapters/<chaptername>.texto keep it all manageable. This fine-grained approach could also have benefits for collaborative work. – apriori Jul 04 '19 at 17:17
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
\inputbasically everywhere with any content.
It is usable in the preamble, inside packages and in the document. - You can nest
\inputmacros.
You can use\inputinside a file which is read using\input. - The only thing
\inputdoes 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
\clearpagebefore 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
.auxfile for the given file.
There will be afilename.auxfile 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
\inputinternally 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\clearpageand the own.auxfile\includedoesn't work in the preamble, inside packages. Using it in restricted modes or math mode won't work properly, while\inputis fine there. - You can't nest
\includefiles.
You can't use\includeinside a file which is read by\include. This is by intention and is because to avoid issues with the.auxfiles. Otherwise three.auxfiles (main, parent\include, child\include) would be open at the same time which was deemed to complicated I guess.
You can use\inputinside an\includefile and also\inputan\includefile. - Biggest benefit: You can use
\includeonly{filename1,filename2,...}in the preamble to only include specific\includefiles.
Because the state of the document (i.e. above mentioned counter values) was stored in an own.auxfile 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 theexcludeonlypackage which provides an\excludeonlyto exclude only certain files instead of including all other files.
- 262,582
-
13I've read several times that
\includewon't work in the preamble; @egreg in http://tex.stackexchange.com/questions/91167/why-use-sty-files even states never use\includefor packages/definitions. However, due to limited knowledge when stating with LaTeX, I did exactly that, i.e. used\includein 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\includeor\input. But I keep in mind to try input if strange things happen. – mpy Mar 02 '13 at 14:50 -
15@mpy: Please don't tell other people to use
\includein the preamble especially not to load packages! For these use\usepackage.\includecauses 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. – Martin Scharrer Mar 02 '13 at 15:13 -
9That was not my intention, I am only confused when I read
\includewon't work in preamble... Please feel free to delete my comment if you consider it as dangerous. – mpy Mar 02 '13 at 15:18 -
3What happens with the new imported equations and tables? Are there automatically renumbered? – skan Nov 19 '16 at 16:34
-
1I 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 '20 at 14:12 -
@Andyc For this I would recommend
\includeonly{filebasename,...}to only compile one or more selected chapters. Alsostandalonepackage has a feature to strip preamples from included files (using\includestandalone). – Martin Scharrer Nov 29 '20 at 14:53 -
@MartinScharrer 1) I'm still a little confused about how to write the single files to be included: say I have Chapter 1, which I want to edit on its own (compile it and look at it every time I make a change to it). For this it has to include the needed packages and have a
\begin{document}and an\end{document}if I understand correctly. Do I leave it like that when I then\include(or\includeonly) it in the main document, or do I have to manually strip something? – Andyc Nov 29 '20 at 15:20 -
1@Andyc the included file should just have the text to be included, so typically starts
\chapter{...}it can hav eno pramble pr\begin{document}– David Carlisle Oct 15 '21 at 10:53 -
@DavidCarlisle Thank you. I guess I got it this time, by reading the thread again. – Andyc Oct 16 '21 at 13:11
\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.
- 30,891
- 23
- 67
- 87
-
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. – Willie Wong Jul 27 '10 at 01: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 '10 at 01:35
-
1@WillieWong you can make forward references so long as you have previously processed chapter 5. – David Carlisle May 20 '17 at 17:05
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
\inputcommand:\input{filename}
\inputinserts the contents of another file, named filename.tex; note that the .tex extension is omitted. For all practical purposes,\inputis 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
\includecommand is different from\inputin 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\includecommand, 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
\includecommands 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
\includeonlycommand in the preamble:\includeonly{filename2,filename3}This way, only
\includecommands 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.
-
The main part of the text quoted from the wikibook on the difference between
\inputand\include"The \include command is different from \input in that it's the output that is added " is completely wrong unfortunately. – David Carlisle Jul 06 '21 at 15:07
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.
- 617
- 5
- 9
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.
- 120
There are two biggest differences between them:
- Can't nest
include, butinputcan be. For example, we can't inputinclude{file}in the file B, which had been inserted byinclude{file B}. includecan'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.
\includedoesn't seem to recognize\label's in the included file. I changed to\inputand the cross-references worked fine. – Jeff Apr 04 '14 at 03:26\@inputis used just like\inputexcept that it does not throw an error if the file does not exist. This can be very useful sometimes. – Apr 28 '16 at 14:01\included files (chapters of my notes), and use cross references all the time without a hitch. – vonbrand Sep 08 '20 at 02:38