# Include without pagebreak

I really like the \input command, but what I really like more is the \includeonly command. Is there a way to have the best of these worlds? (i.e. an \inputonly or an \include without page-breaks?)

-
The way that \include works is tied very much to forcing a new page (to ensure that items go to the correct .aux file at shipout). –  Joseph Wright Aug 26 '10 at 12:51

You won't get the behaviour that \include gives, where it saves the missing page numbers, chapter/footnote/equation/theorem/etc counters, bibliographic citations, etc, but if all you want is the ability to conditionally select which pieces of the document you want to compile, then there are several options, as discussed in the FAQ. The comment, version, optional, versions packages allow this.

-
Thanks for that. Unfortunately what I'd really like is the behaviour of \include. Just like to avoid those nasty pagebreaks! –  slackenerny Aug 26 '10 at 7:47
Which behavior of \include is it that you want? –  TH. Aug 26 '10 at 9:28
I guess references are the thing I'd care most about. Of course counters would also be very much appreciated. –  slackenerny Aug 26 '10 at 13:31
Is it even possible? That \include keeps tracks of counters and references is tied to the fact that it generates a separate .aux for each included file. Perhaps the easiest thing it to just modify your work flow: forget about page breaks in the drafting stage and use \include and once the content is set, recompile using \input? –  Willie Wong Aug 26 '10 at 14:52
@Willie: That is my current strategy. But since I was hanging around here I thought I might ask for a different solution. –  slackenerny Aug 26 '10 at 15:16

If your really want to suppress the page break, which is caused by a \clearpage at the end of \include, then your can temporarily redefine this macro.

The filehook package can be used for this:

\usepackage{filehook}
\AtEndOfIncludes{%
\global\let\savedclearpage\clearpage
\global\let\clearpage\relax
}
\AfterIncludes{%
\global\let\clearpage\savedclearpage
}


I'm not sure right now if \global is required here but it will not hurt.

As already mentioned it is not guaranteed anymore that \includeonly gives correct results, e.g. page numbers, because the last page wasn't flushed.

If you only want to suppress certain input files, then you need a \inputonly macro:

% You can put this into a .sty file and load it with \usepackage{ }
\makeatletter
\newcommand*{\inputonly}[1]{\gdef\@inputonly{,#1,}}
\@onlypreamble\inputonly % Make it only valid in the preamble
\newcommand*{\oinput}[1]{%
\begingroup
\edef\@tempa{{,#1,}{\@inputonly}}%
\expandafter\in@\@tempa
\ifin@
\endgroup
\input{#1}%
% Dump counter values here
\else
\endgroup
% maybe add a \message{ } here
\expandafter\ignorespaces
\fi
}


Usage:

\documentclass{article}
% Insert or load above code

% Use \oinput instead of \input:
\inputonly{aa,bb,cc}
\begin{document}
\oinput{aa}
\oinput{bb}
\oinput{ccc}% will not be \inputed
\oinput{cc}
\end{document}


The \input macro is also used by packages to read a lot of other files, so it cannot safely be overwritten. A different name, e.g. \oinput, must be used instead.

If you want to keep counter values you have to dump and load them by yourself. See the code of \include how it is done.

-
\documentclass{article}
\makeatletter
\def\inputonly#1{\inputonly@i#1,,\@nil}
\def\inputonly@i#1,#2,#3\@nil{%
\global\@namedef{#1input}{#1}
\ifx\relax#2\relax\else\inputonly@i#2,#3\@nil\fi}
\def\Input#1{\expandafter\ifx\csname#1input\endcsname\relax\else\input{#1}\fi}
\makeatother
\inputonly{aa,bb,cc}

\begin{document}
\Input{aa}
\Input{bb}

The openany class option does not prevent page breaks before chapters - it only allows that new chapters start on left-hand (even) pages. –  lockstep Aug 26 '10 at 12:52