Text alignment
Introduction
By default, LaTeX typesets text as fully-justified, but occasionally left-aligned or "ragged right" text (for right-to-left languages) may be more appropriate—such as text within narrow columns. This article explains how to change text alignment for parts, or all, of your document using LaTeX's built-in features and the package ragged2e
.
The standard LaTeX commands and environments
LaTeX does have built-in commands for changing the typeset alignment of text:
- ragged-right (
\raggedright
) - ragged-left (
\raggedleft
) - centred (
\centering
)
together with corresponding environments:
- ragged-right (
flushleft
environment) - ragged-left (
flushright
environment) - centred (
centering
environment)
However, the ragged2e
package provides some refinements which improve upon these standard LaTeX commands and environments.
Using the package ragged2e
To start with, here is an example using the ragged2e
package and its [document]
option to typeset the entire document text as ragged-right (left-aligned). The sample code adds the line
\usepackage[document]{ragged2e}
to the document preamble:
\documentclass{article}
\usepackage[document]{ragged2e}
\begin{document}
\section{Heading on Level 1 (section)}
Hello, here is some text without a meaning. This text should show what a printed text will look like at this place. If you read this text, you will get no information. Really? Is there no information? Is there a difference between this text and some nonsense like not at all! A blind text like this gives you information about the selected font, how the letters are written and an impression of the look.
\end{document}
Open this ragged2e
example in Overleaf
This example produces the following output:
Comparing \raggedright (LaTeX) vs \RaggedRight (ragged2e)
The LaTeX command \raggedright
sometimes produces results that appear to look "too ragged". The package ragged2e
tackles this problem by allowing hyphenation when a line is too short, generating a more uniformly ragged text-edge. The following example shows ragged-right text produced via the LaTeX command \raggedright
and, for comparison, the same text typeset using the \RaggedRight
command provided by ragged2e
:
\documentclass{article}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{blindtext}
\begin{document}
\setlength{\hsize}{0.9\hsize}% emphasize effects
\subsection*{Left-aligned example with
\texttt{\string\raggedright}\\ (standard \LaTeX{} command)}
\raggedright\blindtext[2]\par
\subsection*{Left-aligned example with \texttt{\string\RaggedRight}\\ (\texttt{ragged2e} command)}
\RaggedRight\blindtext[2]\par
\end{document}
As discussed in the ragged2e
package documentation, ragged2e
provides alternative commands and environments for modifying text alignment and we'll take a look at some of these.
Environments for ragged-right (aligned-left) text
The default LaTeX environment for producing ragged-right text is flushleft
. The equivalent in ragged2e
is called FlushLeft
(note capitalization). The following example compares the output of those environments:
\documentclass{article}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{blindtext}
\begin{document}
\setlength{\hsize}{0.9\hsize}% emphasize effects
\subsection*{\texttt{flushleft} environment: (standard \LaTeX{})}
\begin{flushleft}
\blindtext[1]
\end{flushleft}
\subsection*{\texttt{FlushLeft} environment: (\texttt{ragged2e})}
\begin{FlushLeft}
\blindtext[1]
\end{FlushLeft}
\end{document}
Environments for ragged-left (right-aligned) text
The default LaTeX environment for producing ragged-left (right-aligned) text is flushright
. The equivalent in ragged2e
is called FlushRight
(note capitalization). The following example compares the output of those environments:
\documentclass{article}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{blindtext}
\begin{document}
\setlength{\hsize}{0.9\hsize}% emphasize effects
\subsection*{\texttt{flushright} environment: (standard \LaTeX{})}
\begin{flushright}
\blindtext[1]
\end{flushright}
\subsection*{\texttt{FlushRight} environment: (\texttt{ragged2e})}
\begin{FlushRight}
\blindtext[1]
\end{FlushRight}
\end{document}
Environments to centre text
The default LaTeX environment for producing centred text is center
. The equivalent in ragged2e
is called Center
(note capitalization). The following example compares the output of those environments:
\documentclass{article}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{blindtext}
\begin{document}
\setlength{\hsize}{0.9\hsize}% emphasize effects
\subsection*{\texttt{center} environment: (standard \LaTeX{})}
\begin{center}
\blindtext[1]
\end{center}
\subsection*{\texttt{Center} environment: (\texttt{ragged2e})}
\begin{Center}
\blindtext[1]
\end{Center}
\end{document}
Fully justified text
The package ragged2e
provides the command \justifying
which you can use as shown in the example below:
\documentclass{article}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{blindtext}
\begin{document}
\setlength{\hsize}{0.9\hsize}% emphasize effects
\Centering
\blindtext[1]
\vspace{5mm}
\justifying
\blindtext[1]
\end{document}
Reference guide
Summary of environments and commands for text alignment
Alignment | Environment | Switch command | ragged2e environment | ragged2e switch command |
---|---|---|---|---|
Left | flushleft
|
\raggedright
|
FlushLeft
|
\RaggedRight
|
Right | flushright
|
\raggedleft
|
FlushRight
|
\RaggedLeft
|
Centre | center
|
\centering
|
Center
|
\Centering
|
Fully justified | justify
|
\justifying
|
Further reading
For more information see :
- Paragraphs and new lines
- Paragraph formatting
- Bold, italics and underlining
- Font sizes, families, and styles
- Font typefaces
- Supporting modern fonts with XeLaTeX
- Line breaks and blank spaces
- Lists
- Sections and chapters
- Multiple columns
- Single sided and double sided documents
- The not so short introduction to LaTeX2ε
- The ragged2e package documentation