Book Image

LaTeX Cookbook

By : Stefan Kottwitz
Book Image

LaTeX Cookbook

By: Stefan Kottwitz

Overview of this book

LaTeX is a high-quality typesetting software and is very popular, especially among scientists. Its programming language gives you full control over every aspect of your documents, no matter how complex they are. LaTeX's huge amount of customizable templates and supporting packages cover most aspects of writing with embedded typographic expertise. With this book you will learn to leverage the capabilities of the latest document classes and explore the functionalities of the newest packages. The book starts with examples of common document types. It provides you with samples for tuning text design, using fonts, embedding images, and creating legible tables. Common document parts such as the bibliography, glossary, and index are covered, with LaTeX's modern approach.You will learn how to create excellent graphics directly within LaTeX, including diagrams and plots quickly and easily. Finally, you will discover how to use the new engines XeTeX and LuaTeX for advanced programming and calculating with LaTeX. The example-driven approach of this book is sure to increase your productivity.
Table of Contents (19 chapters)
LaTeX Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Writing a thesis


When you plan to write a large document like a thesis, you have two choices: either choose a ready-made template, or set up your own document environment. If you have got limited time and need to start off with your thesis fast, a template can come to the rescue.

Beware of outdated and questionable templates found somewhere on the Internet. Look first at the date and at user opinions, such as on web forums. The age of a template is not a problem by itself, as LaTeX can run it the same way when it's been written. However, as LaTeX has developed, better solutions have come up over time. Legacy code may not benefit from it.

Some universities provide their own templates. That may be ok, because requirements would be met for sure; just check if it can be improved; for example, by replacing an obsolete package with its recommended successors.

A very good source for checking the quality of a template is the guide to obsolete commands and packages in LaTeX2e, which is also called l2tabu. You can open the English version by typing the texdoc l2tabuen command in Command Prompt, or by visiting http://texdoc.net/pkg/l2tabuen.

To be clear, the LaTeX base is stable and solid, but there are changes in community-contributed packages.

In the previous recipe, Writing a short text, we took the bottom-up approach and built a document from scratch, adding what we desired. Now we will go top-down; let's use and understand a complete template, removing what we don't need.

As we need to choose a template now, let's take a real gem. The ClassicThesis package by Prof. André Miede is a thesis template of very good quality. The design follows the classic guide to writing, The Elements of Typographic Style by Robert Bringhurst, we will see some particular points later in this recipe. Its implementation is thoughtful and modern. Originally written in 2006, it's maintained today as well, and shipped with TeX distributions.

Even if you would not use this specific template later, it shows how we can organize a large document.

Getting ready

Though the ClassicThesis package may already be installed in your TeX system, named classicthesis.sty, the whole template is an archive of files that should go into your working directory.

Download the ClassicThesis archive from CTAN by visiting http://www.ctan.org/tex-archive/macros/latex/contrib/classicthesis/.

Instead of fetching single files, choose to download it as a .zip file. Unzip it to where you keep your personal documents, in its own directory. This directory will be your thesis directory.

This package provides a ready-made structured filesystem of the main document, style file, settings file, and document parts such as the abstract and foreword, and also chapters in dedicated files. You can edit all files and fill in your own text.

The ClassicThesis.tex file is the main document. Its filler text is the template's manual; this allows us to compile the template immediately for looking at the output design. By compiling, you can also verify that your TeX installation can handle additional packages if you need to install any.

How to do it...

After unzipping, your directory will have the following structure:

Now follow these steps:

  1. Rename or copy the file ClassicThesis.tex; choose your own name, such as MyThesis.tex, but keep it in the same directory.

  2. Open the main document, that is, MyThesis.tex, and look around to get a feeling of the document structure. Compile it for testing at least twice to get the correct referencing so you would know that this starting point works.

  3. You can review and edit the settings in the main file, MyThesis.tex, and in the configuration file, classicthesis-config.tex. Over the course of the following pages, we will examine their content.

  4. Open the existing .tex files, such as Abstract.tex and Chapter01.tex, with your editor. Remove the filler text and type in your own text. Add further chapter files as needed, and include them in the main file, MyThesis.tex, using the \include command. The structure is given; the technical part of the editing is like cloning files and copying lines, all you need to focus on is the actual thesis content now.

Don't worry if the font or margins don't please you yet. You can change the layout at any time. Let's take a closer look now, and then you will know how.

How it works...

We will take a look at the functional lines of the main file, MyThesis.tex.

The document preamble is as follows:

\documentclass[ twoside,openright,titlepage,
                numbers=noenddot,headinclude,
                footinclude=true,cleardoublepage=empty,
                BCOR=5mm,paper=a4,fontsize=11pt,
                ngerman,american,%
                ]{scrreprt}
\input{classicthesis-config}

The template is built on the KOMA-Script class, scrreprt. KOMA-Script as a LaTeX bundle is described in this chapter's first recipe, Writing a short text.

You can change the preset options to those you need, such as the font size or binding correction BCOR. There are many class options for adjusting the layout; you can read about them in the KOMA-Script manual. Type texdoc scrguien in Command prompt, or visit http://texdoc.net/pkg/scrguien. We will discuss some of them in depth in our next recipe, Designing a book.

Loading of packages and all the remaining settings is done in a single file, classicthesis-config.tex. We will look at it later in this recipe.

The document body starts with the following commands:

\begin{document}
\frenchspacing
\raggedbottom
\selectlanguage{american}
\pagenumbering{roman}
\pagestyle{plain}

The \frenchspacing line means that there's a single space following the punctuation after a sentence. Before the twentieth century, people stuffed extra space between sentences. LaTeX does this by default, or you can enter the \nonfrenchspacing line. The language is set to American English. Actually, you would need the \selectlanguage line only if you need to switch between languages for correct hyphenation.

We start with Roman page numbers. The plain page style means that we have no page headers for now, while page numbers are centered in the page footer.

Then, we look at the front matter, which is the part of the document where the formal parts before the actual content go:

\include{FrontBackmatter/DirtyTitlepage}
\include{FrontBackmatter/Titlepage}
\include{FrontBackmatter/Titleback}
\cleardoublepage\include{FrontBackmatter/Dedication}
%\cleardoublepage\include{FrontBackmatter/Foreword}
\cleardoublepage\include{FrontBackmatter/Abstract}
\cleardoublepage\include{FrontBackmatter/Publication}
\cleardoublepage\include{FrontBackmatter/Acknowledgments}
\pagestyle{scrheadings}
\cleardoublepage\include{FrontBackmatter/Contents}

Each commonly-required part of the front matter has its own file. Just edit the file as you need it, comment out by inserting a % symbol at the beginning of a line, or remove what you don't need. The \cleardoublepage line ends a page, but also ensures that the next page starts on the right-hand side. This can mean inserting an empty page if necessary; that is, a double page break. This will not happen if you change the twoside option to oneside, so you can keep the \cleardoublepage line, which will act like a \clearpage command when the oneside option is set.

Finally, we got the main matter:

\pagenumbering{arabic}
%\setcounter{page}{90}
% use \cleardoublepage here to avoid problems with pdfbookmark
\cleardoublepage
\part{Some Kind of Manual}
\include{Chapters/Chapter01}
\cleardoublepage
\ctparttext{You can put some informational part preamble text here...}
\part{The Showcase}
\include{Chapters/Chapter02}
\include{Chapters/Chapter03}

In the main matter, we use Arabic page numbers. The \pagenumbering line resets the page number to 0.

The thesis is divided into parts. Each one is split into chapters. You can omit the \part lines if you want your highest sectioning level to be the chapter level.

In the Chapters subdirectory, each chapter has its own .tex file; so, you can easily handle a large amount of text. Furthermore, you can use the \includeonly command to compile just the selected chapters for speeding up writing.

Finally, the main document ends with the back matter:

\appendix
\cleardoublepage
\part{Appendix}
\include{Chapters/Chapter0A}
%********************************************************************
% Other Stuff in the Back
%*******************************************************
\cleardoublepage\include{FrontBackmatter/Bibliography}
\cleardoublepage\include{FrontBackmatter/Colophon}
\cleardoublepage\include{FrontBackmatter/Declaration}
\end{document}

The command \appendix resets the sectioning counters and changes them to alphabetic numbering, that is, the following chapters will be numbered A, B, and so on. Like in the front matter, the parts of the appendix are divided into several files.

Let's take a look at the configuration file. Open the classicthesis-config.tex file. The whole thing would take too much space in the book, so let's just take a look at some sample lines:

\newcommand{\myTitle}{A Classic Thesis Style\xspace}
\newcommand{\myName}{Andr\'e Miede\xspace}
\newcommand{\myUni}{Put data here\xspace}
\newcommand{\myLocation}{Darmstadt\xspace}
\newcommand{\myTime}{August 2012\xspace}

Here, you can fill in your own data. Besides being printed on the title page, this data will be used as metadata for the generated PDF document. There are more supported macros here, such as \mySubtitle, \myProf, and many more. The \xspace takes care of proper spacing after such a macro; that is, it inserts a space when there's no punctuation mark following it.

There's more...

As mentioned, this template contains design decisions inspired by the book The Elements of Typographical Style by Robert Bringhurst. The most notable design decisions are as follows:

  • It doesn't use bold fonts, but rather uses small caps or italics elegantly to emphasize what's important.

  • The text body is not very wide and this allows it to be read comfortably without needing the eyes to jump too far from the end of the line to the beginning of the next line. So, we have wide margins, which can be used for notes.

  • The table of contents is not stretched to get right-aligned page numbers. To quote the author, "Is your reader interested in the page number or does she/he want to sum the numbers up?". That's why the page number follows the title.

Explore the classicthesis-config.tex file further to make modifications. Like in the previous recipe, we apply document-wide changes within the preamble, and here, we do it in this file.

We will now take a look at the selected lines of that configuration file.

Changing the input encoding

You might have noticed that special characters, such as accented characters, don't work directly in the input. Scroll to this line:

\PassOptionsToPackage{latin9}{inputenc}
\usepackage{inputenc}

This is another way of setting the input encoding. Refer to the recipe Inputting accented characters in Chapter 2, Tuning the Text, to understand the topic of encoding. Here, you should change the latin9 option to utf8 if you wish to use an UTF-8-capable system and editor. The latin9 option is typically used by older editors on Windows. There are other encodings. If you would need another one, use texdoc inputenc, or visit http://texdoc.net/pkg/inputenc to take a look at the available options.

Getting a right-justified table of contents

The design is not set in stone, you may adjust it all. If you would like to see the page numbers in the table of contents right aligned, set a single option. Look at the very beginning of the classicthesis-config.tex file:

\PassOptionsToPackage{eulerchapternumbers,listings,drafting,%
  pdfspacing,%floatperchapter,%linedheaders,%
  subfig,beramono,eulermath,parts}{classicthesis}

Here, you can find the options for the actual classicthesis package. Simply add the dottedtoc option to this comma separated list. These options, among others, are documented in the template's manual. You can open it by typing texdoc classicthesis in Command Prompt, or at http://texdoc.net/pkg/classicthesis. (editor: please apply inline code formatting correctly, and URL formatting)

Changing the margins

To fulfill requirements of page margins or to implement your own layout ideas, you can specify exact page dimensions by loading the geometry package. Consider the following example:

\usepackage[inner=1.5cm,outer=3cm,top=2cm,bottom=2cm,
  bindingoffset=5mm]{geometry}

Here, you can also provide a value for the space that you leave for the binding and all margins you like. It's good practice to have a visible inner margin set to half the value of the outer margin, because margins will be added in the middle. For single-sided printing, with the oneside option, use left=... and right=... for the defining the margins instead of inner and outer.

Place such a line at the end of the classicthesis-config.tex file so that it will override the original settings.

Modifying the layout of captions

In the classicthesis-config.tex file, you can change the appearance of captions of figures and tables:

\usepackage{caption}
\captionsetup{format=hang,font=small}

The template loads the caption package. It provides a lot of features for fine-tuning captions. For now, the caption texts are indented so they will hang under the first line, and they will have a smaller font size than the normal text. By adding simple options, you can further adjust the appearance; for example, by adding the labelfont=it line, you can get italicized caption labels. Refer to the caption manual to learn more. As usual, you can open it via texdoc caption or at http://texdoc.net/pkg/caption.

Note

While the caption package is a general solution working with most classes, including KOMA-Script, the KOMA-Script now offers extended integrated caption features.

Centering displayed equations

Another option is responsible for the alignment of displayed equations:

\PassOptionsToPackage{fleqn}{amsmath} 
\usepackage{amsmath}

With these ClassicThesis lines displayed equations will be left aligned. The fleqn option allows switching to this alignment. If you would like to restore the default behavior, which is centering the equations, remove that first line that passes the option, or comment it out by a % symbol. But keep the second line, though, which loads the amsmath option, as this is the de-facto standard package for typesetting mathematics in LaTeX.

You can find further thesis templates at http://www.latextemplates.com.

See also

In the following chapters, you can find a lot of recipes for content elements in your thesis. For a beautiful thesis, elegant tables are of great value, so you may also want to take a look at the Advanced table design recipe in Chapter 5, Beautiful Designs.