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
Overleaf guides
- Creating a document in Overleaf
- Uploading a project
- Copying a project
- Creating a project from a template
- Using the Overleaf project menu
- Including images in Overleaf
- Exporting your work from Overleaf
- Working offline in Overleaf
- Using Track Changes in Overleaf
- Using bibliographies in Overleaf
- Sharing your work with others
- Using the History feature
- Debugging Compilation timeout errors
- How-to guides
- Guide to Overleaf’s premium features
LaTeX Basics
- Creating your first LaTeX document
- Choosing a LaTeX Compiler
- Paragraphs and new lines
- Bold, italics and underlining
- Lists
- Errors
Mathematics
- Mathematical expressions
- Subscripts and superscripts
- Brackets and Parentheses
- Matrices
- Fractions and Binomials
- Aligning equations
- Operators
- Spacing in math mode
- Integrals, sums and limits
- Display style in math mode
- List of Greek letters and math symbols
- Mathematical fonts
- Using the Symbol Palette in Overleaf
Figures and tables
- Inserting Images
- Tables
- Positioning Images and Tables
- Lists of Tables and Figures
- Drawing Diagrams Directly in LaTeX
- TikZ package
References and Citations
- Bibliography management with bibtex
- Bibliography management with natbib
- Bibliography management with biblatex
- Bibtex bibliography styles
- Natbib bibliography styles
- Natbib citation styles
- Biblatex bibliography styles
- Biblatex citation styles
Languages
- Multilingual typesetting on Overleaf using polyglossia and fontspec
- Multilingual typesetting on Overleaf using babel and fontspec
- International language support
- Quotations and quotation marks
- Arabic
- Chinese
- French
- German
- Greek
- Italian
- Japanese
- Korean
- Portuguese
- Russian
- Spanish
Document structure
- Sections and chapters
- Table of contents
- Cross referencing sections, equations and floats
- Indices
- Glossaries
- Nomenclatures
- Management in a large project
- Multi-file LaTeX projects
- Hyperlinks
Formatting
- Lengths in LaTeX
- Headers and footers
- Page numbering
- Paragraph formatting
- Line breaks and blank spaces
- Text alignment
- Page size and margins
- Single sided and double sided documents
- Multiple columns
- Counters
- Code listing
- Code Highlighting with minted
- Using colours in LaTeX
- Footnotes
- Margin notes
Fonts
Presentations
Commands
Field specific
- Theorems and proofs
- Chemistry formulae
- Feynman diagrams
- Molecular orbital diagrams
- Chess notation
- Knitting patterns
- CircuiTikz package
- Pgfplots package
- Typesetting exams in LaTeX
- Knitr
- Attribute Value Matrices
Class files
- Understanding packages and class files
- List of packages and class files
- Writing your own package
- Writing your own class