×
点此以英语 使用 ShareLaTeX

You can open the project used in the video in ShareLaTeX by following this link: https://www.sharelatex.com/project/51f62ca03f7a0920417218de

Transcript

In the last video we looked at inserting tables and matrices into our LaTeX documents. In this final video of the series we’ll discuss creating larger documents.

In the previous videos we’ve used the document class ‘article’. This document class is useful for producing short documents, for example articles for scientific journals. If however you want to create a longer document you would be wise to use either the book or report class. Both of these classes enable you to add chapters in while this wasn’t an option in the article class.

When writing longer documents it is wise to split up your document into multiple files. This makes our projects more modular and it makes them a lot tidier. I will now demonstrate this by creating an example document with the report class. This first tex file is what is called the ‘root document’ and is the file that will draw the whole document together. We begin it just like the documents we’ve made before, with a preamble specifying what document class we want to use and which packages we want to load up. For this document I want to write five chapters. If we weren’t splitting our document up we would just start writing the chapters in-between the \begin{document} and \end{document} commands using the \chapter command. Instead we create a new tex file for each chapter. Here’s an example. Notice that in this tex file we haven’t written a preamble or even used a \begin{document} command. We have just started with a \chapter command and then composed the chapter splitting it up into sections.

I have now created all five files and you can see them and easily switch between them from the project panel on the left hand side. Now we will return to the root document. To add our individual chapters into the actual report we use the \input command followed by the filename of the file we want to add to the document. Now if we compile the document we can see that LaTeX has added in our chapters and automatically numbered them. An alternative command to \input is the \include command which would also add our chapters into the document. However there are a couple of differences between the two commands, firstly the \include command forces whatever is in the included file to be started on a new page, where as \input doesn’t. Of course in our example it won’t make any difference because all of our non-root files start with a chapter command which automatically starts a new page regardless. The second difference is that \input allows you to nest \input commands in files that are already being inputted by the root file. For example I could split chapter five into multiple tex files, draw them together in the ‘chapter05.tex’ file using \input commands and then input the completed chapter into the document in the same way as before. The \include command won’t allow this.

The next thing I want to talk about is adding a table of contents. LaTeX will automatically generate a table of contents if you include a \tableofcontents command. You will notice that LaTeX has added all the numbered sections down to the subsection level. You can change the level to which the table of contents goes down to, using a \setcounter command in the preamble followed by the keyword ‘tocdepth’ and then a number. If I want it to include sub-subsections I can use the number three. Or maybe we just want the chapter titles and so we use the number zero. At this point I should inform you that If you aren’t using ShareLaTeX as your LaTeX editor you may have to run LaTeX more than once for tables of contents to appear correctly with the right page numbers. ShareLaTeX streamlines this process into one click of the green recompile button.

When writing a longer document you may want to store your graphics in a separate folder or multiple folders. For example In this project my graphics have been stored in a folder called ‘my_images’. If I now load up the ‘graphicx’ package and add an \includegraphics command in chapter one, the compiler will give us an error. This is because the image is in a different folder to the tex files. To fix this error we use the \graphicspath command in the preamble of the main tex file and enter the folder name followed by a forward slash. Now LaTeX will know where to look for the graphic.

This brings us to the end of our discussion on writing longer documents and also to the end of our basic tutorial series.

Published on 29 Jul 2013