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

Designing a book


A book can be a pretty large document, so we can take a similar approach to the one we took in the preceding recipe. Refer to that recipe to see how to split your document into handy files and how to organize the directory structure.

Commonly, books are printed two sided. They are divided into chapters, which start on right-hand side pages, and they have pretty spacy headings and often a page header showing the current chapter title. Readability and good typography are important, so you will hardly find books with an A4 paper size, double-line spacing, and similar specs, which some institutes expect of a thesis. This is why we have dedicated book classes with meaningful default settings and features.

How to do it...

As explained in the Writing a short text recipe, our choice will be a KOMA-Script class; only, this time it has the name scrbook.

Perform these steps to start off a book:

  1. Start with the scrbook class and suitable options for paper and font size:

    \documentclass[fontsize=11pt,paper=a5,
      pagesize=auto]{scrbook}
  2. Specify the input encoding, which will depend on your editor settings:

    \usepackage[utf8]{inputenc}
  3. Choose a font encoding. T1 is good for European, English, or American texts:

    \usepackage[T1]{fontenc}
  4. If you want a nondefault font, load it. Here, we chose Latin Modern:

    \usepackage{lmodern}
  5. We will load the blindtext package to get English dummy texts. It also requires you to load babel with the English setting:

    \usepackage[english]{babel}
    \usepackage{blindtext}
  6. Load the microtype package for better text justification:

    \usepackage{microtype}
  7. Using this command, you can switch off the additional space after sentence punctuation:

    \frenchspacing
  8. Begin the document:

    \begin{document}
  9. Provide a title, a subtitle, an author name, and a date. You can also set an empty value if you don't want to have something in the title field:

    \title{The Book}
    \subtitle{Some more to know}
    \author{The Author}
    \date{}
  10. Let LaTeX print the title page:

    \maketitle
  11. Print out the table of contents:

    \tableofcontents 
  12. We will divide this book in to parts, so start with the first part:

    \part{First portion}
  13. Start a chapter with a heading. It's good to have text before adding another heading, so let's insert some:

    \chapter{The beginning}
    Some introductional text comes here.
  14. As we did in our first recipe, add a section and text, and another part with a chapter and section. Using the \Blindtext command, you can generate a long dummy text, and using \blindtext, you can generate shorter dummy texts. The \appendix command switches the font to alphabetic numbering:

    \section{A first section}
    Dummy text will follow.
    \blindtext
    \section{Another section}
    \Blindtext
    \appendix
    \part{Appendix}
    \chapter{An addendum}
    \section{Section within the appendix}
    \blindtext
  15. End the document:

    \end{document}
  16. Let your editor compile the text to PDF. You will get a 13-page-long book document with A5 paper size, a title page, part pages, chapter, section headings, and filler text.

    Take a look at the following sample page:

    Note the headings in sans serif font. This is an intentional default setting in KOMA-Script classes, which makes the headings lighter than the standard LaTeX big, bold, and serif headings. You may know the standard look.

  17. Now, you can fill in your own text, add chapters and sections, and possibly add features, which will be described later in this recipe.

How it works...

At first, we loaded the class scrbook, which was specifically made for writing books. So, it is ready for two-sided printing with meaningful margins and good proportions of headings and text.

Besides the class's default settings, we chose a font size of 11 pt and A5 paper size, which is handy for a book. The pagesize=auto option is important here as it ensures that the A5 printing area will be taken over to the PDF page size.

Then, we did the following things, which will be explained in more detail at the beginning of Chapter 2, Tuning the Text:

  • We declared the input character encoding and chose UTF-8, Unicode

  • We set the font encoding to T1 by loading the fontenc package

  • We chose the high-quality T1 supporting font set Latin Modern by loading the lmodern package

  • We loaded the babel package with support for English

  • We used the microtype package to get finer typography

The last package we loaded is the blindtext. You don't need it in your final document; here, it will serve us to provide filler text. Using such dummy text, we can get a better idea of the final result before writing the actual content.

Finally, we switched to the so-called French spacing, which means that after ending a sentence, we will get a normal interword space, not a wider space.

There's more...

You can change the layout of the book in many ways. Choose your settings at the beginning, or even better—start writing your content without any hesitation—once you have a decent amount of text, you can better see the effects of layout changes. You can do this at any time. Let's take a look at some design ideas.

Changing the page layout

When the book is bound after printing, the binding can cost space, that is, there may be less of the inner margin visible. You can specify a binding correction to compensate and to preserve layout proportions. So, if you would see 5 mm less of the inner margin after binding, add BCOR=5mm as class the option at the beginning. A similarly produced book may give you an idea of a good value.

The actual text area has the same ratios as the page itself. This is automatically done by a dividing construction, described in the KOMA-Script manual. It's really worth reading. You can open it by typing the texdoc scrguien command in Command Prompt, or online at http://texdoc.net/pkg/scrguien. The abbreviation, scrguien, comes from scr for the original package name Script, gui for guide, and en for English, and, obviously, from the ancient limit of eight characters per file name in older filesystems.

Besides the page and text area ratios, the result shows a bottom margin twice as high as the top margin, and an outer margin with double the width of the inner margin. Imagine an opened book, where the inner margins together appear with the same space as an outer margin. Sometimes, people make the mistake of thinking that the inner margin should be much bigger because of the binding, but that's done by raising the BCOR, as shown previously.

If you would like to get a bigger text area, which means narrower margins, you can still keep the ratios as described. Just raise the division factor of the aforementioned internal construction and check whether or not it would suit you. For example, set the class option as DIV=10; higher values are possible. That's a safe and easy way to preserve sane layout proportions.

To sum up, our example with 5 mm binding loss and pretty narrow margins can start like this:

\documentclass[fontsize=11pt,paper=a5,pagesize=auto,
  BCOR=5mm,DIV=12]{scrbook}

Alternatively, you can freely choose text and margin dimensions, when requirements of the publisher or institute need to be met. This can be done by loading the classic geometry package with the desired measurements, like we saw in the Writing a thesis recipe:

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

Designing a title page

You can create your own title page, to present some more information in any style you desire. Let's take a look at an example that shows some handy commands for it.

Remove the \maketitle command. You can do the same thing with the commands \title, \subtitle, \author, and \date. Instead, put the titlepage environment right after the \begin{document} block:

\begin{titlepage}
  \vspace*{1cm}
  {\huge\raggedright The Book\par}
  \noindent\hrulefill\par
  {\LARGE\raggedleft The Author\par}
  \vfill
  {\Large\raggedleft Institute\par}
\end{titlepage}

The titlepage environment creates a page without a page number on it. We started with some vertical space using the \vspace* command. The \vspace command adds vertical space, which can be of a positive or negative value. Here, note the star at the end; * \vspace also works at the very beginning of a page, where a simple \vspace would be ignored. This default behavior prevents undesired vertical space at the top of a page, which originally may have been intended as space between texts.

We enclosed each line in curly braces. This is also called grouping, and it is used to keep the effect of changes, such as the font size, local within the braces. In each line, we do the following:

  • Switch to a certain font size

  • Choose left or right alignment

  • Write out the text

  • End with a paragraph break

The \par command is equivalent to an empty line in the input. Sometimes, people use it to keep the code compact, as we did here. We need to end the paragraph before the font size changes because that size defines the space between lines. Hence, we ended the paragraph before we closed the brace group. It's good to keep this in mind for when texts are longer.

Our only nontext design element is a modest horizontal line made by the \hrulefill line. The preceding \noindent line just prevents an undesired paragraph indentation, so the line really starts at the very left.

The \vfill line inserts stretching vertical space, so we get the last line pushed down to the title page bottom.

We took this occasion to show some commands for positioning text on a page. You can experiment with the \vspace and \vfill commands and their horizontal companions \hspace and \hfill. Just avoid using such commands to fix local placement issues in the actual document, when it would be better to adjust a class or package setting document wide. Use these only in the final stage to make tweaks if any are required.

Tip

The titlepages package provides 40 example title pages in various designs with full LaTeX source code. You can choose one, use it, and customize it.

Adding a cover page

The title page, which we produced previously, is actually an inner page. That's why it follows the normal page layout with the same inner and outer margin as the body text.

The cover is different, for example, it should have symmetric margins, and it can be designed according to your preference or individual choice. To get that deviating layout, it is recommended that you use a separate document for it. Another reason is that it will usually be printed on a different type of paper or cardboard.

So, you can start with an article-like class like the one we used in our first recipe, Writing a short text. Use options such as twoside=false or the equivalent oneside option to get symmetric margins. Then, you can proceed to positioning your text like we did with the title page.

Changing the document class

A very well designed book class is memoir. It is pretty complete in itself, so you don't need to load many packages as it already integrates a lot of features of other packages, providing similar interfaces. The memoir class has a monolithic, easy-to-use approach, but it needs to take care of package conflicts. It is not as flexible as choosing the package set by yourself. KOMA-Script, in contrast, provides its features mostly in packages that can also be used together with other classes. You can change to memoir this way:

  1. Start off with the memoir class by changing the first line to the following:

    \documentclass[11pt,a5paper]{memoir}
  2. Remove the unsupported \subtitle command.

  3. To have the title on its own page, surround the \maketitle line with a titlingpage environment:

    \begin{titlingpage}
    \maketitle
    \end{titlingpage}
  4. Compile, and compare.

The memoir class provides an extensive manual that can help you to customize your document. It's split in two parts. Type the texdoc memman command in Command Prompt to read the actual manual, and the texdoc memdesign command to read the part on book design, which is a great resource independent of the class. Alternatively, you can find these manuals at http://texdoc.net/pkg/memman and http://texdoc.net/pkg/memdesign respectively.

Another great start with a special beauty is the tufte-latex class. It comes with a sample-book.tex file, which you can also download from http://ctan.org/tex-archive/macros/latex/contrib/tufte-latex. You can open this book file, which contains some dummy content, and fill in your own text. One of its outstanding features is a wide margin for extensive use of side notes and small figures in the margin.

See also

A book may contain other elements, such as an index, a glossary, and a bibliography. Refer to Chapter 7, Contents, Indexes, and Bibliographies, which contains such recipes.