# How to check if my child document is being loaded by parent document when using standalone?

When I use standalone package to load a child .tex file into a main.tex file, then parts of the title and table of content heading Contents still shows up in the main document.

The problem is that I need to build the child.tex on its own, and also be able to build main.tex later to collect all files into one. Hence in child.tex I also use \tableofcontents and use \title. And I do not know how to remove these when building main.tex, since these do not go to the preamble of the child, and must be in the body of the child, and standalone only strips off stuff in the preamble.

So I need some kind of logic or flag, as follows that I can add to each child document:

\begin{document}
....
\ifstandalone
% do nothing, I am being loaded by parent
\else
title{title of child document}
\author{me}
\maketitle
\tableofcontents
\fi


Now standalone does have such switch \ifstandalone, but I can't figure how to use it in the child document. Here is from the PDF of the package (the package is too advanced for me to understand it all at this time).

I tried many things, and none are working. Does any one knows how to do this? Here is a MWE, parent.tex and child.tex. Both are in the same folder.

\documentclass[12pt,notitlepage]{report}
\usepackage{standalone}%
\usepackage{import}
\begin{document}
\author{me}
\title{This is my document home parent.tex title}
\maketitle
\tableofcontents

\chapter{HWs}
now include the other document
\subimport{.}{child}   %make sure no empty line after this
\end{document}


and this is child.tex

\documentclass{article}
\begin{document}
\title{my child.tex file}  %need a way to remove this when compiling parent.
\author{me}
\maketitle
\tableofcontents

\section{problem 1}
test 1
\section{problem 2}
test 2
\end{document}


Now if I do

rm *.aux; rm *.toc; pdflatex child.tex


it works. But when I do pdflatex parent.tex I still get parts of the TOC and title from child showing up in main.tex, which messes up the layout in the parent document:

ps. Here is something I tried in the child.tex

\documentclass{article}
\usepackage{standalone}% added this so to be able to use the ifstandalone flag
\begin{document}
\ifstandalone  %but this had no effect when build the parent.tex, the title and
%part of the TOC still showed up in the parent, causing problems
%
\else
\title{child.tex title}
\author{me}
\maketitle
\tableofcontents
\fi

\section{problem 1}
test 1
\section{problem 2}
test 2
\end{document}


The \ifstandalone switch works the other way. You should put it in your child document. If it is a standalone document the contens of the \ifstandalone branch will be there, if it's included they will be omitted.

child file:

\documentclass{article}
\usepackage{standalone} \standalonetrue

\begin{document}
\ifstandalone
\title{my child.tex file}  %need a way to remove this when compiling parent.
\author{me}
\maketitle
\tableofcontents
\fi

\section{problem 1}
test 1
\section{problem 2}
test 2
\end{document}


parent file:

\documentclass[12pt,notitlepage]{report}
\usepackage{standalone}
\usepackage{import}
\begin{document}
\author{me}
\title{This is my document home parent.tex title}
\maketitle
\tableofcontents

\chapter{HWs}
now include the other document
\subimport{.}{child}   %make sure no empty line after this
\end{document}


If that's what you're trying to accomplish.

• thanks, but this does not work. Now when building the child itself, it no longer contains the TOC and a title. I want to be able the child.tex as its own document, and only strip those things when it is included in the parent. This fixed it when building the parent, but the child now no longer build OK. – Nasser Oct 31 '13 at 15:16
• @Nasser: Sorry, I should've checked that myself, I've skipped \standalonetrue. Does it work for you now? – masu Oct 31 '13 at 15:46
• Great, yes now it works. Both parent and child build as expected. Thanks so much for your help. – Nasser Oct 31 '13 at 15:56