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)

Turning off full justification

Though commonly your text will look fine if full justification is used, there may be occasions when it's not optimum. For instance, full justification could be displeasing if the text lines are short; in such a case, it could be sufficient to justify only to the left side. We shall see how to put this into practice, plus how to right-justify and how to get centered lines.

Creating ragged-right text

Remember the first parbox example, which was fully justified but had those big gaps between the words? In this example, we shall give up justification to the right side to avoid such gaps:

  1. Create a new document containing these lines:
    \documentclass{article}
    \begin{document}
    \parbox{3cm}{\raggedright
        TUG is an acronym. It means \TeX\ Users Group.}
    \end{document}
  2. Click on Typeset and look at the output:
Figure 2.18 – Left-justified text

Figure 2.18 – Left-justified text

We inserted the \raggedright declaration. From this point onward, the text will be ragged-right. In other words, the text will be moved to the left margin—"flushed-left." There won't be hyphenation.

Because we used this declaration inside a box, it's only valid there, like inside environments. After the box, the text will be fully justified again.

If we want the whole document to be ragged-right, we just need to use \raggedright in our preamble.

Creating ragged-left text

There might be occasions when we would like to achieve the opposite effect: flushing the text to the right margin. We can do this similarly by inserting the \raggedleft declaration. You're able to control where lines are broken by inserting \\.

Centering text

Text can also be horizontally centered in the middle of the page. We will try centering with a few example lines.

We will manually create a nice-looking title for our document; it should contain the title, the author, and the date, all of which will be centered:

  1. Write a document containing this code:
    \documentclass{article}
    \pagestyle{empty}
    \begin{document}
    {\centering
        \huge\bfseries Centered text \\
        \Large\normalfont written by me \\
        \normalsize\today
    }
    \end{document}
  2. Click on Typeset to see the output:
Figure 2.19 – Centered text

Figure 2.19 – Centered text

Because only the title should be centered, we opened a group to limit the centering. With the \centering declaration, the remaining text of this group will be horizontally aligned to the center. We also inserted a paragraph break with an empty line; it's recommended to do this before ending the group to apply our centering to the fully contained paragraph. By using the closing brace, we ended the group. If you complement some text after the closing brace, it will be typeset normally, not centered.

\centering is commonly used when pictures or tables are inserted, or further on title pages and sometimes for headings, but rather as part of logical command definitions.

Using environments for justification

There's a predefined center environment that centers text and prints it in a displayed paragraph at the same time.

Let's test it. We will reuse the fragment of the Edgar Allen Poe poem. This time, we shall center all verses:

  1. Start a new document:
    \documentclass{article}
  2. Now, let's load the url package so that we can also print a hyperlink at the end:
    \usepackage{url}
  3. Begin the document with some text:
    \begin{document}
    \noindent This is the beginning of a poem
    by Edgar Allan Poe:
  4. Now, write text in a center environment:
    \begin{center}
        \emph{Annabel Lee}
    \end{center}
  5. Again, write text for the body of the poem:
    \begin{center}
        It was many and many a year ago,\\
        In a kingdom by the sea,\\
        That a maiden there lived whom you may know\\
        By the name of Annabel Lee
    \end{center}
  6. Add some text, including a URL pointing to the poem on the internet, and finish:
    The complete poem can be read on
    \url{http://www.online-literature.com/poe/576/}.
    \end{document}
  7. Click on Typeset and see the output:
Figure 2.20 – A centered poem within text

Figure 2.20 – A centered poem within text

We began with \noindent again, avoiding the paragraph indentation. \begin{center} started the center environment, which begins a new paragraph, leaving some space for the preceding text. \end{center} ended this environment. We used the center environment a second time, where we inserted \\ to end the verses. After the center environment ended, some space followed, and the next paragraph began at the left margin.

There's not only an environment for centering. The corresponding environment for ragged-right text is called flushleft; that is, everything within the environment is pushed to the left and ragged at the right side, and, similarly, for ragged-left text, it's flushright.

Centering, as previously, is one way to emphasize some text. Another way is to indent it a bit and to add some vertical space before and after the text. This is a common way to display a quotation. Let's see how to do that.