141

My LaTeX makes me pagebreaks after each subsection because my subsections are in separate files. I use the command \include{file} which adds a pagebreak after the use of it.

I would like to have no pagebreak caused by the use of \include{file}.

How can you no pagebreak after the use of include -command?

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

5 Answers5

197

\include always uses \clearpage, a not entirely sensible default. It is intended for entire chapters, not for subsections (why would you want subsections in separate files, anyway?).

You can fix it either by using \input{filename} or loading the newclude package and writing \include*{filename} instead.

Will Robertson
  • 62,540
  • 32
  • 99
  • 117
  • My database project needs Planning document the "Building" document to show how to set up the system. They are sections in my LaTeX -file. – Léo Léopold Hertz 준영 Aug 01 '09 at 12:26
  • 11
    I can think of a stack of reasons why to put subsections in different files. The first that comes to mind, is having a CV with different levels of detail, depending on the target and/or job application. Once can easily chop bits out or put bits in, with a single '%' symbol. – Nicholas Hamilton Dec 27 '12 at 07:46
  • 12
    It's been a while since I wrote this answer, but I believe that parenthetical was meant to be sarcastic `:)` – Will Robertson Dec 31 '12 at 01:25
  • The reason that `\include` start a new page is that `\includeonly` can work properly. – Carsten S May 08 '14 at 07:21
  • Another reason for cropping sections into different files: Consider writing exercise sheets always having a maketitle in the beginning but including always one exercise sheet only on the printout. I myself wouldn't consider every exercise sheet a chapter itself, especially since more exercise sheets refer to a single chapter in a lecture. The includeonly serves as counting the exercise sheets like sheet 1 with exercises (1.1-1.4), sheet 2 with exercises (2.1-2.3) and so on. – C-star-W-star Oct 30 '16 at 11:20
  • Will already explained that the question was not meant literal, but in case anyone *is* asking themselves, here's another reason: TeXStudio can split the editor window into multiple panes, but it can't show different parts of the same file in different panes. So splitting the document up makes it much easier to have e.g. the LaTeX for the methods and results (or results and conclusions, and the nomenclature...) sections next to each other while inserting labels/references between them and making sure that one actually explains what the other requires. – Zak Feb 27 '23 at 20:10
46

You can stop pagebreaks caused by \include by placing \let\clearpage\relax before it. So,

\let\clearpage\relax
\include{file1}
\include{file2}
\include{file3}

would put the contents of the three files (and any subsequently included files) together without a pagebreak between them. If you want to stop relaxing the \clearpage command, then wrap the files to include without pagebreaks within a group like this:

\begingroup
\let\clearpage\relax
\include{file1}
\include{file2}
\endgroup
\include{file3}

This will stop a pagebreak between file1 and file2, but insert the normal pagebreak after file2. (Note: I do not know if this interferes with referencing and page numbering, though I imagine it should be OK.)

John
  • 649
  • 5
  • 6
  • 2
    Thanks! This answer works well when you have multiple includes plus References and you don't want a new page for them. Just make a group of the last include one and the bibliography part, and no longer a clearpage. – Manuel Ferreria May 07 '12 at 14:12
  • 2
    Manuel is right: you can use the ``\begingroup\let\clearpage\relax ...\endgroup`` trick wherever you like---stop pagebreaks between particular sections or parts, if you want the bibliography to be on on the same page as your text, etc. – John Aug 28 '12 at 05:15
  • 1
    This solution appears to have caused some problems for me with references. In an included file which contained two enumerated lists, references to the items of the second list did not work. I have no idea why this should be, but switching to newclude fixed the problem. – Mike Shulman Dec 10 '12 at 03:26
  • 1
    I recommend ***against*** using this method; it will create more problems than it solves (assuming it solves some problem in the first place): see http://tex.stackexchange.com/a/185237/4427 – egreg Jun 17 '14 at 08:55
4

The newclude package suggested by Will Robertson is rather useful to avoid the clearpage. It appears, in order for \includeonly to work one has to call the package immediately after \documentclass{...}. In the complex environment of my dissertation I also ran into problems with broken references.

A good workaround, when includeonly is not needed for a final version, is to use includes only in the draft:

\newif\ifdraft\drafttrue

or

\newif\ifdraft\draftfalse

\ifdraft
  \include{...}
\fi

\ifdraft
  \include{file}
\else
  \input{file}
\fi

The first line can be easily appended by a makefile, to make draft or production version production make targets.

\includeonly{file1,file2,...} allows to specify a list of source files called with \include{file1} (where file1 is an example) that will show in the resulting document. The others will not show up, but are considered for counters, labels, tables of contents when the corresponding aux files are included.

In other words, by using include and includeonly one can keep the compile time short in a draft while having correct references. Further reading on Wikibooks.

@Will Robertson

\include is so useful because it allows through \includeonly{...} to build only needed sections. While working on longer text it can make quite a difference in compile time to include only a section of a lengthy chapter. It is also invaluably useful as one doesn't have to page through a long draft while working at one point. Lastly, smaller files of source code are easier to handle in version management, e.g. git.

gschenk
  • 827
  • 9
  • 17
  • Can you please give an example how you compile only one section of a very long working paper? I have used these methods of the following answer in `pagesel` about `\discardpagesfromhere` and `\keeppagesfromhere`. They are little overlapping each other so I am willing to learn if you can handle this draft management better. http://tex.stackexchange.com/a/267555/13173 – Léo Léopold Hertz 준영 Feb 24 '16 at 09:00
  • 1
    If I understand it correclty, _pagesel_ is to achieve more than _includeonly_ by allowing fine control which pages are to be included in a final build. Include has a much smaller scope, of including sourcecode in a way that can be switched on and off. – gschenk Mar 01 '16 at 01:35
0

The following is robust in most situations:

\cleardoublepage
\begingroup
\let\clearpage\relax
\include{./common/yourFile.tex}
\endgroup

The first \cleardoublepage ensures that your chapter ends up on an odd page for 2-sided.

The \let\clearpage\relax disables clearpage inserted by \include

The \begingroup \endgroup makes sure that the scope is just that include.

Mohsen Banan
  • 85
  • 1
  • 5
-1

Thank you, Cambridge!

use \include instead of \input, and use the \includeonly command to select sections to process

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
  • 3
    This is not the best advice. Why would you want to use `\include{...]` for a job when `\input{...}` is the actual command which is intendet for these situations? – Florian R. Klein Nov 05 '13 at 11:41
  • 2
    This answer does not seem to answer the question at all. (Even though it is otherwise a useful advice, with a good reference.) – jciloa May 15 '17 at 13:37
  • 1
    This doesn't answer the question. – EL_DON Feb 16 '18 at 20:57