Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
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 Nov 29 '13 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. –  Martin Schröder Nov 29 '13 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. –  Anjan Debnath Nov 29 '13 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. :-) –  Who is crazy first Nov 29 '13 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 Jan 25 at 11:23

2 Answers 2

up vote 7 down vote accepted

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.

share|improve this answer
    
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 –  Anjan Debnath Nov 29 '13 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 Nov 29 '13 at 18:50
    
@Dan: But how can \input knows when it is used in a nested \input in a nested directory ? –  Who is crazy first Nov 29 '13 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 Nov 29 '13 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.

\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{mystructuring}[2011/01/12 v0.01 LaTeX package for my own purpose]
%\RequirePackage{needspace}

\newcommand\ContentsPath{Contents/}
\newcommand\ChapterPath{\ContentsPath}
\newcommand\SectionPath{\ChapterPath}
\newcommand\SubSectionPath{\SectionPath}

\@ifclassloaded{book}
{
    \newcommand\IncludeChapter[1]{%
        \renewcommand\ChapterPath{\ContentsPath#1/}%
        \include{\ContentsPath#1}%
    }
    %
    \newcommand\IncludeOnlyChapter[1]{%
        \renewcommand\ChapterPath{\ContentsPath#1/}%
        \includeonly{\ContentsPath#1}%
    }
}{}


\newcommand\InputSection[1]{%
    \renewcommand\SectionPath{\ChapterPath#1/}%
    \input{\ChapterPath#1}%
}


\newcommand\InputSubSection[1]{%
    \renewcommand\SubSectionPath{\SectionPath#1/}%
    \input{\SectionPath#1}%
}


\newcommand\InputSubSubSection[1]{%
    \input{\SubSectionPath#1}%
}

%=====================================================

\@ifclassloaded{book}
{
    \newcommand\Chapter[1]{%
        \chapter{#1}%
        %\addcontentsline{toc}{chapter}{#1}
    }
}{}

\newcommand\Section[1]{%
    %\needspace{3\baselineskip}
    \section{#1}%
    %\addcontentsline{toc}{section}{#1}
}

\newcommand\SubSection[1]{%
    \subsection{#1}%
    %\addcontentsline{toc}{subsection}{#1}
}

\newcommand\SubSubSection[1]{%
    \subsubsection{#1}%
    %\addcontentsline{toc}{subsubsection}{#1}
}

\endinput
% mystructuring.sty

Step 2

Create directory structure for your project as follows.

/.../Projects/Project1/Main.tex
...
/.../Projects/Project1/Contents/Chapter1.tex
/.../Projects/Project1/Contents/Chapter1/Section1.tex
/.../Projects/Project1/Contents/Chapter1/Section1/SubSection1.tex
/.../Projects/Project1/Contents/Chapter1/Section1/SubSection2.tex
...
/.../Projects/Project1/Contents/Chapter1/Section2.tex
/.../Projects/Project1/Contents/Chapter1/Section2/SubSection1.tex
/.../Projects/Project1/Contents/Chapter1/Section2/SubSection2.tex
...
/.../Projects/Project1/Contents/Chapter2.tex
/.../Projects/Project1/Contents/Chapter2/Section1.tex
/.../Projects/Project1/Contents/Chapter2/Section1/SubSection1.tex
/.../Projects/Project1/Contents/Chapter2/Section1/SubSection2.tex
...
/.../Projects/Project1/Contents/Chapter2/Section2.tex
/.../Projects/Project1/Contents/Chapter2/Section2/SubSection1.tex
/.../Projects/Project1/Contents/Chapter2/Section2/SubSection2.tex

Explanation:

  • 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
    \documentclass{book}
    \usepackage{mystructuring}
    %\IncludeOnlyChapter{Introduction}
    \begin{document}
    \IncludeChapter{Introduction}
    \IncludeChapter{WhatIsTeX}
    \end{document}
    
  • Introduction.tex

    % Introduction.tex
    \Chapter{Introduction}   
    
    \InputSection{History}
    \InputSection{CurrentDevelopment}
    
  • WhatIsTeX.tex

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

    % History.tex
    \Section{History}
    
    \InputSubSection{BeforeWorldWarTwo}
    \InputSubSection{AfterWorldWarTwo}
    
  • CurrentDevelopment.tex

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

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

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

    % PSTricks.tex
    \SubSection{PSTricks}
    
  • TikZ.tex

    % TikZ.tex
    \SubSection{TikZ}
    

Output to prove that I am not lying now.

enter image description here

share|improve this answer
    
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. –  Anjan Debnath Nov 29 '13 at 18:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.