[Tex/LaTex] How to include subfiles in to a subfiles

includesubfiles

First of all, I am a newbie here and I have no idea to post a query but I have a problem for which I require your help. I even don't know what should be the title of my problem but what my intuition said, I just titled it above. Apologize if I am wrong. Let me tell you my situation first.

I am writing a problem book in mathematics. The book (say book.tex) is divided in several chapters (say chapter1, chapter2, …, chapter 7). Each chapter will have three sections: "Notes" followed by "Exercises" and then "Solution" with not less than 100 pages.

I know how to use \input or \include in a latex document to include multiple files in a single latex document. But while I was doing so, I found that each chapters are much bigger in amount individually and the latex file itself(for the concerned chapter) is getting bigger. I mean to say that suppose I am writing chapter 1. I found even chapter 1 is getting so bigger that the parts "Notes", "Exercise" and "Solution" are making it too complicated.

So what the alternative I have chosen is that I am writing chapter 1 as another new latex document (say CH1.tex) using \input with NotesCH1.tex, ExercisesCH1.tex and SolutionsCH1.tex Once the document for chapter 1 is finished, I will have to copy-paste busyness in the actual chapter1.tex my book.tex file.

Although I am working on this way, but some times its getting too tedious. Because unless the chapters individually are completed, I can't copy them and paste them in the main latex file.

Today I came to know about the package subfiles. I have read about it. Tried also and found helpful. It helps to include subfiles in to the main files. So what I did, I created book.tex as master file, under it chapter1.tex as subfile using \include. But still my problem is not solved. I found that Notes, Exercise and Solution all must be in the same file chapter1.tex

Can't I do like this: in the chapter1.tex I use \include with Notes.tex, Exercise.tex and Solution.tex and run the master file so that every thing will be compiled together?

For the master file Book.tex

\documentclass{book}
\usepackage{subfiles}
\begin{document}
\include{ch1.tex}
\end{document}

For chapter 1

\documentclass[Book.tex]{subfiles} 
\begin{document}

\include{NotesCh1.tex}
\include{ExerciseCh1.tex}
\include{Solutions.tex}

\end{document}

For NotesCh1.tex

This is some notes

For ExerciseCh1.tex

Some exercises

For Solutions.tex

The solutions

I tried as much information about my problem to provide. Please help me. If possible please provide me the latex command or procedures how shall I overcome it. If this has been discussed before, at least provide me that link.

Thanks in advance for your valuable advise

Best Answer

I think what you want is an easy way to select which part(s) should be compiled in the master file.


The following supposes that you have two chapters in the book, and each of them has three sections as you described.

STEP 1

This is the master file:

% master file: main.tex
\documentclass{book}
\includeonly{chapter1, chapter2}
\begin{document}
\include{chapter1}
\include{chapter2}
\end{document}

Chapter 1:

% chapter 1, main file
\chapter{This is chapter one}
\input{note1}
\input{exercise1}
\input{solution1}

Chapter 2:

% chapter 2, main file
\chapter{This is chapter two}
\input{note2}
\input{exercise2}
\input{solution2}

In the six subsubfiles, there are some dummy text.

STEP 2

Compile the master file, and then, select which chapter you want to compile in \includeonly{} (separate eachother by comma).

STEP 3

Compile the master file again.


You will find that only the chapter(s) you selected was compiled, and the page numbers, chapter and section numbers remains correct.

Related Question