I am writing technical documents with math/diagrams/plots/code etc. I want to compile them into a single document and also must be able to run each of them independently. These technical documents belonging to each topic are in their floders. Can someone guide me how to manage it better and also make it portable? Like I work on the same documents on Mac OSX/MS Windows.
1 Answer
If I understand the question correct, then a solution could be using the package subfiles
.
Say that main.tex
is your main file, and file1.tex
, file2.tex
, and file3.tex
are the ones you want to include. Then your main document would look something like:
main.txt:
\documentclass[a4paper]{article}
<your preamble>
\usepackage{subfiles}
\begin{document}
\subfile{file1}
\subfile{file2}
\subfile{file3}
\end{document}
where you are allowed to put text everywhere you like, just as if you where using \include
or \input
instead of \subfile
. The contents of file1.tex
would then look something like
file1.tex
\documentclass[main.tex]{subfiles}
\begin{document}
<your document goes here>
\end{document}
The same structure should apply to the files file2.tex
and file3.tex
.
Now when you compile main.tex
then the contents of the files, that is, what I indicated by <your document goes here> will be included in your document, and if you compile, say file1.tex
, then the contents of file2.tex
will be compiled as if it had the documentclass specifications and preamble of main.tex
.
So now all the files in the project can be compiled seperately, but to compile one the subfiles you will need to have main.tex
in the same directory.
documentation and installation of the package
I am not sure whether subfiles
comes as a standart package. It did not for me, so depending on how experienced you are with installing packages I wanted to say that the pacakges can be found at
http://www.ctan.org/tex-archive/macros/latex/contrib/subfiles/
where there also is a documentation of the package, namely the pdf document subfiles.pdf
. If you don't know how to handle .ins
and .dtx
files then there is a small guide at
http://en.wikibooks.org/wiki/LaTeX/Packages/Installing_Extra_Packages
and if you want you can put the files subfiles.cls
and subfiles.sty
which are generated while building the package in the same directory as main.tex
, so that you are not depending on the fact that the next system on which the files need to be compiled has the package subfiels
installed.
EDIT
To answer
I have three levels of directory, 1 - toplevel which has rootfile.tex, 2- chapter level which has the main file of each chapter, 3- section level which has the actual contents of that chapter. I am instantiating 3 in 2 using \include{./Sections/part1}. How can manage such a system using subfiles? – avlsi
Suppose that you have the files
./rootfile.tex
./chapter/chap1.tex
./chapter/chap1/section1.1.tex
./chapter/chap1/section1.2.tex
./chapter/chap2.tex
./chapter/chap2/section2.1.tex
./chapter/chap2/section2.2.tex
./chapter/chap3.tex
./chapter/chap3/section3.1.tex
./chapter/chap3/section3.2.tex
and you want to include the files sectionX.1.tex
and sectionX.2.tex
in the file chapX.tex
for X=1,2 and still be able to compile both your section files, your chapterfiles and your root file. This can be done by letting your files look something like:
rootfile.tex:
\documentclass[a4paper]{article}
<your preamble>
\usepackage{subfiles}
\def\dirlevel{../}
\begin{document}
\def\dirlevel{}
\subfile{./chapter/chap1}
\subfile{./chapter/chap2}
\subfile{./chapter/chap3}
\end{document}
chapX.tex:
\documentclass[./../rootfile.tex]{subfiles}
\begin{document}
\subfile{./\dirlevel chap/chapX/sectionX.1}
\subfile{./\dirlevel chap/chapX/sectionX.2}
\end{document}
sectionX.Y.tex:
\documentclass[./../../rootfile.tex]{subfiles}
\begin{document}
<your section goes here>
\end{document}
The trick is that \def\dirlevel{}
is only read if you compile rootfile.tex
, whilst \def\dirlevel{../}
is read no matter which of the files you compile, so no matter which file you compile LaTeX will still be able to find your files.
-
I have three levels of directory, 1 - toplevel which has rootfile.tex, 2- chapter level which has the main file of each chapter, 3- section level which has the actual contents of that chapter. I am instantiating 3 in 2 using \include{./Sections/part1}. How can manage such a system using subfiles?– avlsiAug 18, 2012 at 12:27
-
that really worked like a charm. But I have issue with the image file paths. All my images are in ./chap1/images and they are included in ./chap1/sections/part1.tex. When i run ./chap1/chap1.tex, I get correct output. But when i run ./rootfile.tex it is not able to find the image file. The following is the way I am including the images in the sections. \includegraphics[width=\ScaleIfNeeded]{./Images/basic.ps}– avlsiAug 19, 2012 at 7:04
-
@avlsi Do you actually need to compile your section files individually? If not then I belive that you can just replace the path
./images/basic.ps
with./\dirleve chap1/images/basic.ps
, but I don't believe that you will be able to compile your section files correctly then.– KristianAug 19, 2012 at 9:00 -
my documents are not really a thesis. But individual independent documents. So I was thinking a best way to consolidate them with proper table of contents etc. Is there a way to give a master path to these image paths, so that they donot get disturbed when compiling? Like $IMAGEFILE_ROOT/chap1/images/basic.ps– avlsiAug 19, 2012 at 9:47
-
@avlsi: I don't understand what your setup is, which files you have, which ones you want to be able to compile and what you mean by disturbing your files.– KristianAug 19, 2012 at 13:23