Book Image

LaTeX Beginner's Guide - Second Edition

By : Stefan Kottwitz
4 (1)
Book Image

LaTeX Beginner's Guide - Second Edition

4 (1)
By: Stefan Kottwitz

Overview of this book

LaTeX is high-quality open source typesetting software that produces professional prints and PDF files. It's a powerful and complex tool with a multitude of features, so getting started can be intimidating. However, once you become comfortable with LaTeX, its capabilities far outweigh any initial challenges, and this book will help you with just that! The LaTeX Beginner's Guide will make getting started with LaTeX easy. If you are writing mathematical, scientific, or business papers, or have a thesis to write, this is the perfect book for you. With the help of fully explained examples, this book offers a practical introduction to LaTeX with plenty of step-by-step examples that will help you achieve professional-level results in no time. You'll learn to typeset documents containing tables, figures, formulas, and common book elements such as bibliographies, glossaries, and indexes, and go on to manage complex documents and use modern PDF features. You'll also get to grips with using macros and styles to maintain a consistent document structure while saving typing work. By the end of this LaTeX book, you'll have learned how to fine-tune text and page layout, create professional-looking tables, include figures, present complex mathematical formulas, manage complex documents, and benefit from modern PDF features.
Table of Contents (16 chapters)

Using boxes to limit the width of paragraphs

We won't always write text just from left to right over the complete text width. Sometimes, we'd like a paragraph to have a smaller width, for instance, when we would like to put text and a picture side by side.

In the following sections, we will look at how to work with paragraph boxes in LaTeX.

Creating a narrow text box

For our example, we would like to explain the acronym TUG in a text column of only 3 cm width. To do this, follow these steps:

  1. Create a new document containing these four lines:
    \documentclass{article}
    \begin{document}
    \parbox{3cm}{TUG is an acronym. It means
    \TeX\ Users Group.}
    \end{document}
  2. Click on Typeset and take a critical look at the output with the too-wide spacing:
Figure 2.12 – A narrow justified paragraph

Figure 2.12 – A narrow justified paragraph

We used the \parbox command in the highlighted code line to create a column. The first argument to the \parbox command sets the width to 3 cm, and the second argument to \parbox contains the text.

\parbox takes the argument text and formats the output to fit the specified width. We see that the text is fully justified; however, our example shows an obvious problem: insisting on full justification could lead to undesirable big gaps in the text. Possible solutions are as follows:

  • We introduce hyphenation; the word acronym could easily be hyphenated.
  • We improve justification in general.
  • We give up on full justification; narrow text could look better when it's only left-justified.

We will check out all of these options in the Breaking lines and paragraphs and Turning off full justification sections.

But first, let's understand how \parbox works.

Producing common paragraph boxes

Usually, we just need a text box with a certain width; occasionally, we would like to have some additional alignment to the surrounding text. So, the common definition of the \parbox command is as follows:

\parbox[alignment]{width}{text}

The meanings of the parameters are as follows:

  • alignment: That's the optional argument for vertical alignment. State t to align at the baseline of the top line of the box; write b to align at the baseline of its bottom line. The default behavior is to place the box such that its center is in line with the center of the current text line.
  • width: That's the width of the box. It can be given in ISO units, such as 3 cm, 44 mm, or 2 in.
  • text: That's the text that you want to put in that box. It should be a short piece of common text. For complicated or longer content, we can use the minipage environment, which we will use in the next section.

Here's a demonstration of the effect of the alignment parameters:

\documentclass{article}
\begin{document}
Text line
\quad\parbox[b]{1.8cm}{this parbox is aligned
at its bottom line}
\quad\parbox{1.5cm}{center-aligned parbox}
\quad\parbox[t]{2cm}{another parbox aligned at its top line}
\end{document}

The \quad command produces some space; we used it to separate the boxes a bit. Here's the output:

Figure 2.13 – Aligned paragraph boxes

Figure 2.13 – Aligned paragraph boxes

The output in Figure 2.13 shows how the alignment works. Text line is our base line, and referring to that base line, the following boxes are aligned at the bottom, the center, or the top line, respectively.

Exploring further features of paragraph boxes

\parbox is capable of doing even more. If you need advanced positioning, here's the complete \parbox definition:

\parbox[alignment][height][inner alignment]{width}{text}

The meanings of the parameters are as follows:

  • height: If this optional argument isn't given, the box will have just the natural height of the text inside. Use this argument if you want to change the height of the box to make it bigger or smaller.
  • inner alignment: If the height of the box is different from the natural height of the contained text, you might want to adjust the text position. You can add the following values:

    c: Vertically centers the text in the box

    t: Places text at the top of the box

    b: Places text at its bottom

    s: Stretches the text vertically (if possible)

    If you omit this argument, the alignment argument will be used here as the default value.

Take our previous demonstration example and try the effect of the optional arguments. Use the \fbox command, which helps to visualize the effect; if you write \fbox{\parbox[...]{...}{text}}, the complete parbox will be framed.

Using mini pages

Paragraph boxes are suitable for boxes with only a little text inside. In the case of a box containing a large amount of text, the closing brace could easily be forgotten or overlooked. The minipage environment would then be a better choice.

In this example, we will use the minipage environment instead of \parbox to get a sample of text with a width of just 3 cm:

  1. Modify the parbox example to get the following code:
    \documentclass{article}
    \begin{document}
    \begin{minipage}{3cm}
    TUG is an acronym. It means \TeX\ Users Group.
    \end{minipage}
    \end{document}
  2. Click on Typeset and look at the output:
Figure 2.14 – A minipage example

Figure 2.14 – A minipage example

By using \begin{minipage}, we started a "page in a page." We specified that the width of 3 cm will be the mandatory argument. From this point onward, the text lines will have a width of 3 cm; they will be automatically wrapped and fully justified. We ended this restriction with \end{minipage}. Any text typed afterward would run over the complete body text width.

Preventing page breaks

There will never be a page break within a minipage environment, so that's a way to prevent page breaks within a text area. If that text within the minipage environment doesn't fit on the page, it will move to the next page.

The minipage environment accepts all arguments similar to \parbox with the same meanings.

If text is wrapped in a box or just in a normal line, it may automatically fit well. However, we may need to consider completing line breaking and justification manually. Let's see how to do this in the next sections.