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


For chapter 1




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

  • Welcome to TeX.SX! You can indent code by 4 spaces, and use backticks to emphasize code inline (\input). See Markdown help.
    – Ch'en Meng
    Commented Nov 29, 2013 at 6:48
  • Welcome to TeX.SX! Usually, we don't put a greeting or a “thank you” in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say “thank you” to users who helped you. Commented Nov 29, 2013 at 7:55
  • @Martin Schroder I apologize for my mistake. I forgot to greet all as my mind was very blank during the morning to solve this problem and didn't realize to do it. Thanks for advise sir.
    – KON3
    Commented Nov 29, 2013 at 17:37
  • If you want to adopt the best practice principle, I think you need to put subfiles in a subdirectory and subsubfiles in subsubdirectory, etc. See my answer how I managed a huge project neatly. :-) Commented Nov 29, 2013 at 19:21
  • In the main file you write \include{ch1.tex} while you claim to use subfiles. You need to write \subfile{ch1.tex} to properly use subfiles :)
    – Snicksie
    Commented Jan 25, 2014 at 11:23

2 Answers 2


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.


This is the master file:

% master file: main.tex
\includeonly{chapter1, chapter2}

Chapter 1:

% chapter 1, main file
\chapter{This is chapter one}

Chapter 2:

% chapter 2, main file
\chapter{This is chapter two}

In the six subsubfiles, there are some dummy text.


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


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.

  • Thank you so much! I have a doubt. Sir, I did as you suggested me. \input has helped me a lot. I did some extra research work and am willing to know this: suppose that in the exercise also I have some three sub exercises. In that case what would you suggest me to do? I extended your idea and find success. Is there any thing would you like to advise me? Thank you
    – KON3
    Commented Nov 29, 2013 at 18:06
  • @AnjanDebnath, while in standard latex \include may not be nested, \input may be nested rather deep. How many levels deep generally depends your TeX system, but in mine (TeXLive13 on Windows7) I was able run latex on files that \input files thet \input files, etc., down to 15th level subfiles. Thus you can easily \include chapters that \input sections that \input exercises that \input subexercises, etc.
    – Dan
    Commented Nov 29, 2013 at 18:50
  • @Dan: But how can \input knows when it is used in a nested \input in a nested directory ? Commented Nov 29, 2013 at 19:09
  • @DonutE.Knot: Neither this question nor this answer mentions nested directories, only nesting of \input in subfiles. Without additional tools, if subfiles are in subdirectories, one must give \input the directory name also (relative to the current directory). Or else the files have unique names and reside in a recursively searchable directory tree in TeX's input path.
    – Dan
    Commented Nov 29, 2013 at 19:20

Just another approach without loading any package. The feature that I have to emphasize here is that

\input can be nested as long as the files being referenced are all in the same directory. It means that nesting \input in nested directory is not possible without additional tricks.

My answer below does the trick so you can have nested \inputs in nested directories. It is awesome, isn't it? Saving all input files (including sub files) in a single directory seems to be a bad practice as it makes your life tedious, cumbersome, boring in maintaining them.

Step 1

Create a package named mystructuring.sty as follows.

\ProvidesPackage{mystructuring}[2011/01/12 v0.01 LaTeX package for my own purpose]











% mystructuring.sty

Step 2

Create directory structure for your project as follows.



  • Each project should be in its own directory.
  • In project directory there are a main input file named Main.tex and a subdirectory Contents. These names should not be changed unless you really want to get dirty by modify the package mystructuring.sty.
  • There is an input file and a subdirectory for every chapter. For example, Chapter1 (this name may be changed), its corresponding input file and subdirectory are Chapter1.tex and Chapter1. Both file name and directory name must be exactly the same but again you may use your own name.
  • As the third point said, the rule is also applied to the section.
  • In most cases partitioning up to subsections should be more than enough so there is no need to create subsubsection input file. As a result, no subsection subdirectory is needed to create in section directory.

Step 3

For the sake of simplicity, just download a dummy project here and compile with pdflatex. No virus attached. The directory and file structure are shown as follows.

enter image description here

The contents of each input file are given as follows.

  • Main.tex

    % Main.tex
  • Introduction.tex

    % Introduction.tex
  • WhatIsTeX.tex

    % WhatIsTeX.tex
    \Chapter{What is \TeX}
    This chapter is intentionally left just for fun!
  • History.tex

    % History.tex
  • CurrentDevelopment.tex

    % CurrentDevelopment.tex
    \Section{Current Development}
  • BeforeWorldWarTwo.tex

    % BeforeWorldWarTwo.tex
    \SubSection{Before World War Two}
  • AfterWorldWarTwo.tex

    % AfterWorldWarTwo.tex
    \SubSection{After World War Two}
  • PSTricks.tex

    % PSTricks.tex
  • TikZ.tex

    % TikZ.tex

Output to prove that I am not lying now.

enter image description here

  • Its great sir. Thank you so much. I am really grateful to you that you spend your valuable time to figure out my problem. Thank you so much sir.
    – KON3
    Commented Nov 29, 2013 at 18:04
  • This solution does not allow for compiling individual files, does it?
    – FlorianL
    Commented May 15, 2018 at 13:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .