Typevia
Text Formatting

Understanding Default Alignment: Justification

By default, LaTeX uses full justification for all body text. This means that text is aligned along both the left and right margins, creating a clean, rectangular block of text. LaTeX achieves this by dynamically adjusting the spaces between words and, if necessary, automatically hyphenating words at the end of lines.

Justification is the standard for professional typesetting in books, journals, and newspapers. However, there are many situations—such as titles, cover pages, poetry, or specific table cells—where you may want to override this behavior.

Note: Because LaTeX prioritizes a perfect right margin, you might occasionally see "Overfull \hbox" warnings. This happens when LaTeX cannot find a good way to break a line or hyphenate a word without pushing it into the margin.


Basic Alignment Environments

The simplest way to change text alignment in LaTeX is by using environments. These are blocks of code that apply a specific style to everything contained within them.

There are three primary environments for alignment:

  1. flushleft: Aligns text to the left margin (ragged right).
  2. flushright: Aligns text to the right margin (ragged left).
  3. center: Centers the text between both margins.

Example 1: Using Alignment Environments

\documentclass{article}
\begin{document}

\begin{flushleft}
This text is left-aligned. The right margin will be "ragged," meaning 
the lines do not end at the same horizontal position.
\end{flushleft}

\begin{center}
This text is centered. \\
It is commonly used for titles, names, or quotes.
\end{center}

\begin{flushright}
This text is right-aligned. This is often used for signatures 
at the end of a letter or for specific citations.
\end{flushright}

\end{document}

Key Difference: Environments like center and flushleft add extra vertical space above and below the text block. This makes them ideal for distinct sections of a document but less ideal for changing alignment inside a small group or a table.


Alignment Switches (Commands)

If you want to change alignment without adding extra vertical space, or if you are working inside a restricted area (like a figure or a table cell), you should use switches.

Unlike environments, switches do not use \begin{} and \end{}. Instead, they change the alignment from that point forward until the end of the current "scope" (usually the end of a {} group or a specific environment).

EnvironmentEquivalent Switch
center\centering
flushleft\raggedright
flushright\raggedleft

Example 2: Using Switches in Groups

\documentclass{article}
\begin{document}

Standard justified paragraph that spans the width of the page to show the default behavior of LaTeX.

{
\raggedleft
This paragraph is right-aligned because it is inside a group with the switch.
It does not have the extra vertical padding of the environment. \par
}

This text returns to being justified because the switch was inside the curly braces.

\end{document}

Warning: When using a switch like \centering or \raggedright inside a group (curly braces), you must include a \par command or a blank line before the closing brace }. This tells LaTeX to apply the alignment to the entire paragraph before the group ends.


Alignment in Tables and Figures

One of the most common places you will need to control alignment is inside tables and figures.

Tables

In the tabular environment, you define the alignment of each column in the column specification:

  • l: Left-aligned
  • c: Centered
  • r: Right-aligned

Figures

When inserting images, you almost always want them centered. Using the \centering switch inside the figure environment is the best practice.

Example 3: Tables and Figures

\documentclass{article}
\usepackage{graphicx}

\begin{document}

\begin{table}[h]
\centering % Centers the table on the page
\begin{tabular}{| l | c | r |}
\hline
Left Column & Center Column & Right Column \\
\hline
Alpha & Beta & Gamma \\
1 & 2 & 3 \\
\hline
\end{tabular}
\caption{A table showing different column alignments.}
\end{table}

\begin{figure}[h]
\centering % Centers the image
\includegraphics[width=0.3\textwidth]{example-image}
\caption{A centered image.}
\end{figure}

\end{document}

Improving Ragged Text with ragged2e

The standard LaTeX \raggedright command has a significant drawback: it turns off hyphenation entirely. This can lead to very "ugly" ragged edges where some lines are much shorter than others.

The ragged2e package provides improved versions of these commands that allow for hyphenation while still maintaining the desired alignment.

  • \RaggedRight (instead of \raggedright)
  • \RaggedLeft (instead of \raggedleft)
  • \Centering (instead of \centering)

Example 4: The ragged2e Advantage

\documentclass{article}
\usepackage[document]{ragged2e} % Use [document] to make the whole doc ragged-right

\begin{document}

This document uses the ragged2e package. Even though the text is 
left-aligned, LaTeX will still hyphenate long words at the end 
of lines to ensure the right margin doesn't look too uneven.

\end{document}

Best Practices and Common Pitfalls

1. Don't use manual spacing

Avoid using \hspace{} or repeated spaces to align text. This is fragile and will break if you change your font size or margins. Always use the alignment commands or environments.

2. The "Left" vs "Right" confusion

Beginners often confuse \raggedright with right-alignment.

  • Think of the "ragged" part: If the right side is ragged (uneven), the text is aligned to the left.
  • \raggedright = Left Aligned.
  • \raggedleft = Right Aligned.

3. Alignment in Fixed-Width Table Columns

When you use p{width} to create a fixed-width table column, the text inside is justified by default. If you want that specific column to be left-aligned, you must inject the command into the column definition:

\usepackage{array}
% ...
\begin{tabular}{ >{\raggedright\arraybackslash}p{3cm} l }
  Fixed width and left aligned & Standard column \\
\end{tabular}

4. When to use Justification

Stick to default justification for long-form reading (essays, reports). Use \centering for titles and \raggedright for narrow columns (like in a newsletter or a small table) where justification would create awkward, large gaps between words (often called "rivers of white").