Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
LaTeX Beginner's Guide

You're reading from   LaTeX Beginner's Guide Create visually appealing texts, articles, and books for business and science using LaTeX

Arrow left icon
Product type Paperback
Published in Oct 2021
Publisher Packt
ISBN-13 9781801078658
Length 354 pages
Edition 2nd Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Stefan Kottwitz Stefan Kottwitz
Author Profile Icon Stefan Kottwitz
Stefan Kottwitz
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Chapter 1: Getting Started with LaTeX 2. Chapter 2: Formatting Text and Creating Macros FREE CHAPTER 3. Chapter 3: Designing Pages 4. Chapter 4: Creating Lists 5. Chapter 5: Including Images 6. Chapter 6: Creating Tables 7. Chapter 7: Using Cross-References 8. Chapter 8: Listing Contents and References 9. Chapter 9: Writing Math Formulas 10. Chapter 10: Using Fonts 11. Chapter 11: Developing Large Documents 12. Chapter 12: Enhancing Your Documents Further 13. Chapter 13: Troubleshooting 14. Chapter 14: Using Online Resources 15. Other Books You May Enjoy

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.

You have been reading a chapter from
LaTeX Beginner's Guide - Second Edition
Published in: Oct 2021
Publisher: Packt
ISBN-13: 9781801078658
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime