[Tex/LaTex] Get {standalone} to ignore blocks of text from \input files (e.g., \begin{spacing} and \maketitle)

includeinputstandalone

I've got a number of separate .tex files that are all \documentclass{article}. I'm inputting these into a \documentclass{book} file as chapters. In the book.tex file, then, I've got inputs like this:

\chapter{Chapter One!}
\input{article}

But I want to be able to compile article.tex separately when I need to. So in article.tex, I've kept the \begin{spacing}{1.5}, \end{spacing}, and \maketitle commands.

Is there any way to have these commands ignored when I compile book.tex? The \standaloneignore command seems to work only for ignoring things above the \documentclass declaration.

Best Answer

The subfile can use either \documentclass{article}, or \documentclass[preview=false]{standalone}. The class option [preview=false] for the standalone package is only there as you want the article.tex to compile by itself as it normally would. You could also use \documentclass{article} in the subfile, but the should also add \usepackage{standalone} \standalonetrue or \newif\ifstandlone \standalonetrue.

If there is specfic code \begin{document} in article.tex that you don't want processed use the \ifstandalone switch as shown below


In article.tex:

\documentclass[preview=false]{standalone}

%% If you prefer to use the `article` class, the I would recommend
%\documentclass{article}
%\usepackage{standalone} \standalonetrue

\title{Artcle.tex Title}
\author{Duck Article}

\usepackage{lipsum}

\begin{document}
\ifstandalone
   \maketitle% Test to ensure that this is not executed in book.tex
\fi

\lipsum[1]
\end{document}

In book.tex:

\documentclass{book}
\usepackage{standalone}
\usepackage{lipsum}

\title{Book.tex Title}
\author{Duck Book}

\begin{document}
%\maketitle Comment this out to ensure that that \maketitle form the article.tex does not get exectuted.
\chapter{Chapter One}
\input{article}
\end{document}
Related Question