Wednesday, July 15, 2009

How to write very long documents with LaTeX

I eventually started writing my thesis--with LaTeX. Since it will become a rather long document (approx. 200 pages) I decided to split the document into its chapters (and maybe into more parts). Splitting a document is very easy with latex and it typically looks like that:

\documentclass{someClass}
... some definitions, use packages etc ...
\begin{document}
...
\maketitle
...
\input{abstract}
\input{introduction}
...
\input{relatedwork}
\input{conclusion}
...
\end{document}
This works pretty well, and I always use this pattern for writing smaller papers. Unfortunately I found two severe problems:
  1. The latex compiler is unaware of folders.
  2. I cannot compile the parts separately, which leads to new problems

Chapterfolder

The first problem is further described and solved by the latex package "chapterfolder". Instead of simply including a chapter, it is included and the current folder is changed accordingly. The main file will now look like this:

...
\begin{document}
...
\maketitle
...
\cfchapter{Introduction}{chapters/introduction}{introduction.tex}
...
\cfchapter{Conclusion}{chapters/conclusion}{conclusion.tex}
In my case I can now use chapter-relative figure folders, i.e. my folder structure looks like that:

/Main
- main.tex
+ chapters
 + introduction
     - introduction.tex
     + fig
         - figOfChapter1.png
+ conclusion
     - conclusion.tex
     + fig
         - figOfChapter2.png
Without "chapterfolder", I had to include a figure in chapter 1 (Introduction) like this:
\includegraphics{chapters/introduction/fig/figOfChapter1}
However, I want to be able to simply include figures by specifying a relative path to the chapter document, that is
\includegraphics{fig/figOfChapter1}
In order to achieve, this, we have to rewrite the includegraphics command. We can do that in the preamble, next to the usepackage statement:
\usepackage{chapterfolder}
% and we re-write includegraphics
\let\includegraphicsWithoutCF\includegraphics
\renewcommand{\includegraphics}[2][]{\includegraphicsWithoutCF[#1]{\cfcurrentfolder#2}}
This makes life much easier, especially if figures are moved from one chapter to another (I have only to move the file of the figure and can simply copy the code). And we are also able of compiling documents separately, as explained further on!

Compile Separately and Embedded

Although I'm using a wonderful LaTeX editor (Texshop) and my computer is pretty fast, working with a split document has some tradeoffs:
  1. The "goto error" function in my editor is not working with included documents, which makes bug fixing really annoying.
  2. Compiling a (currently) 100 page long document with lots of images takes quite some time
For that reason, I tried to figure out how to split my document and use a main file including all parts, while at the same time the parts could be compiled standalone. Here is my solution: In the main file, I define a command for letting the included files know that they are embedded:

\newcommand{\isEmbedded}{true}
In my parts (chapters), I can now test whether this command is defined or not, and include a preamble if required:

\ifx\isEmbedded\undefined
..
\documentclass{../../styles/myStyle}
..
other settings
...
\begin{document}
\maketitle
...
% -------+---------+---------+---------+---------+---------+---------+---------+
\else
\fi
% ******************************************************************************

Here comes the text of the chapter or part.

% ******************************************************************************
\ifx\isEmbedded\undefined
\bibliography{myBib}
...
\end{document}
\else
\fi
I can now compile every chapter separately, and finding a bug is very simple thanks to Texshops "Goto Error" function -- which is now working. Without any modifications, I can also compile the whole text, i.e. my main.tex file.

4 comments:

Anonymous said...

Thank you!

Son Nguyen said...

Dear friend,

Please help to solve problem about chapterfoder.sty

IN main.tex, when I use:
\cfchapter{Who I am?}{chapters/chapter_01}{chapter_01.tex}

In chapter_01.tex:
\includegraphics{fig_00.jpg}
or:
\includegraphics{fig/fig_00.jpg}
or:
\includegraphics{./fig/fig_00.jpg}

I cannnot compile successfully.
The directory structure is the same your structure.

I only compile successful when I place fig_00.jpg in the same directory with main.tex.

==> When I use \cfchapter, the current is not change.

Please help me.
Thanks and Best regard.

Jens v.P. said...

Son,

I'm sorry for your inconvenience. I simply forgot a rather important part in my blog post. I have updated the post accordingly. The trick is to rewrite the includegraphics command. I hope this will solve your problem.

Cheers,
Jens

Son Nguyen said...

Dear Jens,

Thank you for your support.
During working and learning, I resolved the first your problem as:
I don't use "chapterfolder.sty". I use:
\include{/path.../chapter1/chapter1.tex}
\include{/path.../chapter2/chapter2.tex}
..................

About figures:
In each file: chapter1.tex, chapter2.tex,... I use "/graphicspath{path/chapter1figs}",... to guide for latex the path of figures of each chapter.

About the second your problem, I very like how to compile each file separately.

Thanks & Best regards.
Son Nguyen