\input vs. \include in latex - \include only in latex



  • In Latex, there are two different commands to incorporate another file into the source of some document, \input and \include

\Input Command in latex :

  • \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.

Important properties of \input in latex :

  • We can use \input basically everywhere with any content.
  • It is usable in the preamble, inside packages and in the document.
  • We can nest \input macros.
  • We can use \input inside a file which is read using \input.
  • The only thing \input does is to input the file. It doesn’t have any side effects, but don't get any extra features.
  • In crisp, \input command in latex effectively replaces the command with the contents of the input file. \input's can be nested. So, you can write:

Sample code:

\documentclass{article}

\begin{document}

WIKITECHY AAA

\input{b}

WIKITECHY AAA

\end{document}

where b.tex is:

WIKITECHY BBB

\input{c}

BBB

c.tex is:

WIKITECHY CCC

output like:

WIKITECHY AAA
WIKITECHY BBB
WIKITECHY CCC
WIKITECHY BBB
WIKITECHY AAA

include function in latex:

  • Without Argumenet : An \includeonly without the filename in the argument. This is primarily helped 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.
  • With Argument : \include{filename} gets you the fast peformance,
    • it also can't be nested,
    • can't appear in the preamble,
    • forces page breaks around the included text.

Important properties of \include command in latex:

  • \include command 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.
  • \include command can't nest \include files.
  • \include command 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.
  • \include command 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.
  • 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.

\includeonly command in latex :

  • 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.

Related Searches to \input vs. \include in latex - \include only in latex